Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 07:58:42 UTC, Jonathan M Davis wrote: [...] Then you can clearly do something here in C++ that I don't understand, because I have absolutely no idea how you could do anything the exception if you did catch(...), because there's no variable to work with. Check this

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 09:39:12 Rob T wrote: > That's OK for 20 try catch but specifying catch(Exception E) over > and over adds up when you have to write a lot more of those. Copy > paste helps, but not always. So, the only difference is that C++ is catch(...) { throw trace(); } whereas

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jacob Carlborg
On 2013-03-08 08:34, Rob T wrote: One more thing, we finally got __FUNCTION__ (and more) added to MASTER so that's another missing item that was sorely missed. Now we can easily log what functions are catching and throwing exceptions, and more. The big question is if Throwable will be expanded t

Re: Rethrow an exception like in C++?

2013-03-08 Thread Chris Cain
On Friday, 8 March 2013 at 09:01:14 UTC, Jonathan M Davis wrote: If that's the case, I really don't see what the problem is. It's just a few characters difference. To be honest, the way I look at it, D does this _better_. "throw;" looks like magic and it's accessing global (potentially mutab

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 10:07:58 Chris Cain wrote: > On Friday, 8 March 2013 at 09:01:14 UTC, Jonathan M Davis wrote: > > If that's the case, I really don't see what the problem is. > > It's just a few > > characters difference. > > To be honest, the way I look at it, D does this _better_. > "th

how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
I wonder if exists a way to get top n distinct elements from a range (infinite too!) A (not efficient) way to to this is range.array.sort.uniq.take(n) but it's a bit overkill, it sorts elements, and of course doesn't work with infinite ranges. Am i missing any function?

Re: how to get top N distinct elements from range?

2013-03-08 Thread Ary Borenszweig
On 3/8/13 10:33 AM, Andrea Fontana wrote: I wonder if exists a way to get top n distinct elements from a range (infinite too!) Top from an infinite range? o_O

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
Andrea Fontana: I wonder if exists a way to get top n distinct elements from a range (infinite too!) A (not efficient) way to to this is range.array.sort.uniq.take(n) but it's a bit overkill, it sorts elements, and of course doesn't work with infinite ranges. Am i missing any function? Wh

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Friday, 8 March 2013 at 14:10:55 UTC, bearophile wrote: Andrea Fontana: I wonder if exists a way to get top n distinct elements from a range (infinite too!) A (not efficient) way to to this is range.array.sort.uniq.take(n) but it's a bit overkill, it sorts elements, and of course doesn't

Re: how to get top N distinct elements from range?

2013-03-08 Thread jerro
On Friday, 8 March 2013 at 13:33:24 UTC, Andrea Fontana wrote: I wonder if exists a way to get top n distinct elements from a range (infinite too!) It's impossible to do that for infinite ranges A (not efficient) way to to this is range.array.sort.uniq.take(n) but it's a bit overkill, it sort

Re: how to get top N distinct elements from range?

2013-03-08 Thread Ivan Kazmenko
I wonder if exists a way to get top n distinct elements from a range (infinite too!) A (not efficient) way to to this is range.array.sort.uniq.take(n) but it's a bit overkill, it sorts elements, and of course doesn't work with infinite ranges. Am i missing any function? For an arbitrary inf

Re: how to get top N distinct elements from range?

2013-03-08 Thread Chris Cain
On Friday, 8 March 2013 at 15:04:32 UTC, Ivan Kazmenko wrote: For a finite range, you can iterate over the range while maintaining a collection of the top n elements and discarding duplicates as you find them. For example, using a RedBlackTree as the collection, you will get O(log(n) * range

Re: Reading bigger file

2013-03-08 Thread Chris Cain
On Friday, 8 March 2013 at 15:25:02 UTC, bioinfornatics wrote: why when reading a huge file more i advance into the file more that take time to get a line ? StopWatch sw; while( !fastq1.empty ){ sw.start(); autoq1 = fastq1.next(); sw.stop(

Re: Reading bigger file

2013-03-08 Thread Marco Leise
Am Fri, 08 Mar 2013 16:31:45 +0100 schrieb "Chris Cain" : > On Friday, 8 March 2013 at 15:25:02 UTC, bioinfornatics wrote: > > why when reading a huge file more i advance into the file more > > that take time to get a line ? > > > StopWatch sw; > while( !fastq1.empty ){ >

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Friday, 8 March 2013 at 14:43:29 UTC, jerro wrote: On Friday, 8 March 2013 at 13:33:24 UTC, Andrea Fontana wrote: I wonder if exists a way to get top n distinct elements from a range (infinite too!) It's impossible to do that for infinite ranges Why? sequence!"n*2".myTopDistinct!"a==b"(3

Re: Reading bigger file

2013-03-08 Thread bioinfornatics
On Friday, 8 March 2013 at 15:31:46 UTC, Chris Cain wrote: On Friday, 8 March 2013 at 15:25:02 UTC, bioinfornatics wrote: why when reading a huge file more i advance into the file more that take time to get a line ? StopWatch sw; while( !fastq1.empty ){ sw.start();

Re: how to get top N distinct elements from range?

2013-03-08 Thread Ivan Kazmenko
I wonder if exists a way to get top n distinct elements from a range (infinite too!) It's impossible to do that for infinite ranges Why? sequence!"n*2".myTopDistinct!"a==b"(3); will give [2,4,6] I don't quite get it, do you want the least n elements? What result would you want if the seq

Re: how to get top N distinct elements from range?

2013-03-08 Thread Ivan Kazmenko
For a finite range, you can iterate over the range while maintaining a collection of the top n elements and discarding duplicates as you find them. For example, using a RedBlackTree as the collection, you will get O(log(n) * range.length) total time and O(n) total memory used. If n is small

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Friday, 8 March 2013 at 16:12:37 UTC, Ivan Kazmenko wrote: I wonder if exists a way to get top n distinct elements from a range (infinite too!) It's impossible to do that for infinite ranges Why? sequence!"n*2".myTopDistinct!"a==b"(3); will give [2,4,6] I don't quite get it, do you wan

Re: how to get top N distinct elements from range?

2013-03-08 Thread jerro
On Friday, 8 March 2013 at 15:53:56 UTC, Andrea Fontana wrote: On Friday, 8 March 2013 at 14:43:29 UTC, jerro wrote: On Friday, 8 March 2013 at 13:33:24 UTC, Andrea Fontana wrote: I wonder if exists a way to get top n distinct elements from a range (infinite too!) It's impossible to do that f

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
Andrea Fontana: So, is there any lazy way to do it? Take my code and put it inside a struct, add empty, popFront and front. Otherwise return an impure take of a filter closure from a function that keeps the set. Something like (untested): auto firstDistinct(Range)(Range r, size_t n) {

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 09:07:59 UTC, Chris Cain wrote: On Friday, 8 March 2013 at 09:01:14 UTC, Jonathan M Davis wrote: If that's the case, I really don't see what the problem is. It's just a few characters difference. To be honest, the way I look at it, D does this _better_. "throw;" lo

Re: Rethrow an exception like in C++?

2013-03-08 Thread Ali Çehreli
On 03/08/2013 12:39 AM, Rob T wrote: > In C++ you can do this > > std::exception Trace() > { > try > { > // key item missing from D > throw; // <= rethrow last exception This idiom is know as a Lippincott Function. Ali

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 09:01:29 UTC, Jacob Carlborg wrote: On 2013-03-08 08:34, Rob T wrote: One more thing, we finally got __FUNCTION__ (and more) added to MASTER so that's another missing item that was sorely missed. Now we can easily log what functions are catching and throwing exception

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
Otherwise return an impure take of a filter closure from a function that keeps the set. Something like (untested): It seems to work: import std.stdio, std.range, std.algorithm, std.traits; auto firstDistinct(Range)(Range r, in size_t n) { bool[ForeachType!Range] mySet; return r.filt

Re: how to get top N distinct elements from range?

2013-03-08 Thread Chris Cain
On Friday, 8 March 2013 at 18:17:22 UTC, bearophile wrote: Otherwise return an impure take of a filter closure from a function that keeps the set. Something like (untested): It seems to work: import std.stdio, std.range, std.algorithm, std.traits; auto firstDistinct(Range)(Range r, in size_

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
It needs a constraint: auto firstDistinct(Range)(Range r, in size_t n) if (isInputRange!Range) { bool[ForeachType!Range] mySet; return r.filter!((k) { if (k in mySet) return false; mySet[k] = true; return true; }).take(n); } Bye, bearophile

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 18:32:43 Rob T wrote: > The point I'm trying to illustrate is difficult to understand > unless you have to personally implement 1000 try/catch statements > in your code base. You'll never know how a seemingly trivial item > like re-specifying the same pointless catch state

Re: Rethrow an exception like in C++?

2013-03-08 Thread Andrej Mitrovic
On 3/8/13, Jonathan M Davis wrote: > In both cases, you're telling it to catch everything. Also, catch points should be rare, especially the ones which catch base types like Exception or even Error or Throwable. The problem is not the syntax, but the way people use exceptions. Pokemon exception h

Re: randomAccessRange.sort() vs randomAccessRange.array.sort()

2013-03-08 Thread monarch_dodra
On Monday, 4 March 2013 at 23:55:06 UTC, deed wrote: Meaning sortable ranges are actually a narrow subset of random access ranges? Why aren't the constraints listed in the docs? Are the source files and error messages the only way to get this info? Unless I'm mistaken, this was recently fixed

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 18:49:53 UTC, Jonathan M Davis wrote: Except that the C++ one is just as pointless. In both cases, you're telling it to catch everything. It's just that syntax is slightly different, because D doesn't allow you to throw without an explicit variable. And it's only a ha

Re: Appender cannot add struct with immutable members

2013-03-08 Thread monarch_dodra
On Monday, 4 March 2013 at 13:14:43 UTC, Namespace wrote: Is this intended behavior or just a bug / missing feature? http://dpaste.1azy.net/21f100d3 I ask because it would be very easy to solve, with memcpy / memmove. http://dpaste.1azy.net/ac2b38e5 (line 192) AFAIK, the problem is actually

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 18:56:38 UTC, Andrej Mitrovic wrote: On 3/8/13, Jonathan M Davis wrote: In both cases, you're telling it to catch everything. Also, catch points should be rare, especially the ones which catch base types like Exception or even Error or Throwable. The problem is no

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 17:40:38 UTC, Ali Çehreli wrote: On 03/08/2013 12:39 AM, Rob T wrote: > In C++ you can do this > > std::exception Trace() > { > try > { > // key item missing from D > throw; // <= rethrow last exception This idiom is know as a Lippincott Function. Ali I'm having tro

Throw in a pre-condition of a nothrow function

2013-03-08 Thread bearophile
Currently this code compiles: void foo() nothrow in { throw new Exception(null); } body { } void main() { foo(); } Then at runtime throws: object.Exception@test.d(3) - 0x00402044 in nothrow void test.foo() at ... ... Is this good? Isn't the "nothrow" tag used by the compile

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
auto firstDistinct(Range)(Range r, in size_t n) if (isInputRange!Range) { bool[ForeachType!Range] mySet; return r.filter!((k) { if (k in mySet) return false; mySet[k] = true; return true; }).take(n); } I think the standard library of Ada2012 has b

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 06:05:02 UTC, Maxim Fomin wrote: catch (Exception e) { if (typeid(e) == typeid(myException1)) throw e; // may be downcasted, if necessary // to work with specific fields } Isn't it better to check identity in this way?

Re: Throw in a pre-condition of a nothrow function

2013-03-08 Thread Maxim Fomin
That's a bug. Interesting point here is whether it is a regression or not.

Re: Rethrow an exception like in C++?

2013-03-08 Thread H. S. Teoh
On Fri, Mar 08, 2013 at 08:52:21PM +0100, Rob T wrote: > On Friday, 8 March 2013 at 06:05:02 UTC, Maxim Fomin wrote: > >catch (Exception e) { > >if (typeid(e) == typeid(myException1)) > >throw e; // may be downcasted, if necessary > > // to work with spec

Re: Throw in a pre-condition of a nothrow function

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 19:39:47 UTC, bearophile wrote: Currently this code compiles: [...] Nice edge case to test the compiler with. Definitely looks like a bug. It should not compile given the nothrow attrib. If it was throwable, what to do about throwing a null reference is an intere

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 19:58:10 UTC, H. S. Teoh wrote: On Fri, Mar 08, 2013 at 08:52:21PM +0100, Rob T wrote: On Friday, 8 March 2013 at 06:05:02 UTC, Maxim Fomin wrote: >catch (Exception e) { >if (typeid(e) == typeid(myException1)) >throw e; // may be downcasted, if

Re: Throw in a pre-condition of a nothrow function

2013-03-08 Thread bearophile
Rob T: Definitely looks like a bug. It should not compile given the nothrow attrib. http://d.puremagic.com/issues/show_bug.cgi?id=9669 Bye, bearophile

Re: Appender cannot add struct with immutable members

2013-03-08 Thread Namespace
AFAIK, the problem is actually within emplace, that tries to call opAssign when it should really be just (post)blitting. This is just one of the many emplace-related bugs. I have an open pull for fixing emplace that should fix this. I'll add your snippet to its unittests to confirm that it als

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 19:56:28 Andrej Mitrovic wrote: > Pokemon exception handling LOL. I'll have to remember that term! - Jonathan M Davis

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 20:16:13 Rob T wrote: > If you know of a better way to implement an exception handler in > D, then I'd like to know about it. For example I do know that D's > system allows you to insert callback functions, but I don't yet > know how to make use out of it, so perhaps there

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 20:32:23 UTC, Jonathan M Davis wrote: On Friday, March 08, 2013 20:16:13 Rob T wrote: If you know of a better way to implement an exception handler in D, then I'd like to know about it. For example I do know that D's system allows you to insert callback functions, bu

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 11:56:12 H. S. Teoh wrote: > On Fri, Mar 08, 2013 at 08:52:21PM +0100, Rob T wrote: > > On Friday, 8 March 2013 at 06:05:02 UTC, Maxim Fomin wrote: > > > catch (Exception e) { > > > > > > if (typeid(e) == typeid(myException1)) > > > > > > throw e; // may be downcasted, i

Re: how to get top N distinct elements from range?

2013-03-08 Thread Jacob Carlborg
On 2013-03-08 14:33, Andrea Fontana wrote: I wonder if exists a way to get top n distinct elements from a range (infinite too!) A (not efficient) way to to this is range.array.sort.uniq.take(n) but it's a bit overkill, it sorts elements, and of course doesn't work with infinite ranges. Am i miss

Re: Rethrow an exception like in C++?

2013-03-08 Thread Rob T
On Friday, 8 March 2013 at 20:46:42 UTC, Jonathan M Davis wrote: On Friday, March 08, 2013 11:56:12 H. S. Teoh wrote: On Fri, Mar 08, 2013 at 08:52:21PM +0100, Rob T wrote: > On Friday, 8 March 2013 at 06:05:02 UTC, Maxim Fomin wrote: > > catch (Exception e) { > > > > if (typeid(e) == typeid(my

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 15:46:30 Jonathan M Davis wrote: > That's the way that it's supposed to be done. I don't know why anyone would > mess around with typeid that. "with typeid _like_ that." My fingers don't seem to keep up with my brain very well... - Jonathan M Davis

Re: Rethrow an exception like in C++?

2013-03-08 Thread Andrej Mitrovic
On 3/8/13, Rob T wrote: > So this is more efficient or has some other advantages than using > typeid? Benchmark! :) Also you might find this useful some day: http://wiki.dlang.org/Dispatching_an_object_based_on_its_dynamic_type

Re: Rethrow an exception like in C++?

2013-03-08 Thread Maxim Fomin
On Friday, 8 March 2013 at 21:04:06 UTC, Rob T wrote: On Friday, 8 March 2013 at 20:46:42 UTC, Jonathan M Davis wrote: On Friday, March 08, 2013 11:56:12 H. S. Teoh wrote: On Fri, Mar 08, 2013 at 08:52:21PM +0100, Rob T wrote: > On Friday, 8 March 2013 at 06:05:02 UTC, Maxim Fomin wrote: > > ca

Re: Appender cannot add struct with immutable members

2013-03-08 Thread monarch_dodra
On Friday, 8 March 2013 at 20:19:48 UTC, Namespace wrote: AFAIK, the problem is actually within emplace, that tries to call opAssign when it should really be just (post)blitting. This is just one of the many emplace-related bugs. I have an open pull for fixing emplace that should fix this. I'

Re: Tid between classes

2013-03-08 Thread Mike Wey
On 03/07/2013 11:07 AM, Stephen Jones wrote: As far as I can see gtkD no longer supports openGL so I was thinking it might be possible to run a gtkD GUI on the main thread and openGL/Derelict on another. The Gl thread is derived; extends Thread. With threads having separate memory is this assumpt

Re: Rethrow an exception like in C++?

2013-03-08 Thread Ali Çehreli
On 03/08/2013 11:26 AM, Rob T wrote: On Friday, 8 March 2013 at 17:40:38 UTC, Ali Çehreli wrote: On 03/08/2013 12:39 AM, Rob T wrote: > In C++ you can do this > > std::exception Trace() > { > try > { > // key item missing from D > throw; // <= rethrow last exception This idiom is know as a Lipp

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Friday, 8 March 2013 at 19:36:04 UTC, bearophile wrote: auto firstDistinct(Range)(Range r, in size_t n) if (isInputRange!Range) { bool[ForeachType!Range] mySet; return r.filter!((k) { if (k in mySet) return false; mySet[k] = true; return true; }).take(

typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya
Hi! Explain me please,what's wrong with this code: struct NDimensionalArray(T,alias size) if(is(typeof(size) _ == int[n],int n) && n > 0) { static if(n > 1) NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]]; else T m_array[s

Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread bearophile
This is an answer to just your title question. A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [2,2] is a heap-allocated dynamic array of type int[]. Some persons have askes for a fixed-sized array litera, like

Re: Appender cannot add struct with immutable members

2013-03-08 Thread Namespace
Wow, very large. I'm going to read this tomorrow in a rested state in more detail. But it arouses my interest to write something yourself from scratch. But a quick question: Why is the internal array in the separate struct 'Data' encapsulated? To enable a performant postblit, since only a point

Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya
On Friday, 8 March 2013 at 22:59:40 UTC, bearophile wrote: This is an answer to just your title question. A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [2,2] is a heap-allocated dynamic array of type int[]. S

Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya
On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote: On Friday, 8 March 2013 at 22:59:40 UTC, bearophile wrote: This is an answer to just your title question. A lot of time ago typeof([2,2]) was int[2]. This was efficient, but in most cases this was a source of troubles and bugs. So now a [

Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread cal
On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote: Your constraint could be: if(is(typeof(size) _ == int[]) && size.length > 0) Also it looks like you are passing 1 too many template args? static if(n > 1) NDimensionalArray!(T,n - 1,size[1..$]) m_array[size[0]];

Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya
On Friday, 8 March 2013 at 23:09:07 UTC, cal wrote: On Friday, 8 March 2013 at 23:03:47 UTC, Zhenya wrote: Your constraint could be: if(is(typeof(size) _ == int[]) && size.length > 0) Also it looks like you are passing 1 too many template args? static if(n > 1) NDimensionalArray!(T,n - 1,si

Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread cal
On Friday, 8 March 2013 at 23:15:30 UTC, Zhenya wrote: Yes,it's a typo.But it seems to be ugly pass dynamically allocated array through a template parameter,because it's size should be compile-time constant. Its size is a compile-time constant because it is an array literal. The size is known

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 20:16:13 Rob T wrote: > On Friday, 8 March 2013 at 18:49:53 UTC, Jonathan M Davis wrote: > > Except that the C++ one is just as pointless. In both cases, > > you're telling it > > to catch everything. It's just that syntax is slightly > > different, because D > > doesn't a

Re: Rethrow an exception like in C++?

2013-03-08 Thread Jonathan M Davis
On Friday, March 08, 2013 22:04:03 Rob T wrote: > So this is more efficient or has some other advantages than using > typeid? > > if ( cast(myException1)e !is null ) > { > // do stuff with myException1 > } > else > if ( cast(myException2)e !is null ) > { > // do stuff with myException2 > } > else

Re: typeof([2,2]) !=? int[2]

2013-03-08 Thread Zhenya
On Friday, 8 March 2013 at 23:18:52 UTC, cal wrote: On Friday, 8 March 2013 at 23:15:30 UTC, Zhenya wrote: Yes,it's a typo.But it seems to be ugly pass dynamically allocated array through a template parameter,because it's size should be compile-time constant. Its size is a compile-time consta

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
Andrea Fontana: Something goes wrong by the way. I removed "n" template params and "take(n)" (i can do after distinct() call, isn't it the same?). I think take() is not the cause. See this program: import std.stdio, std.range, std.algorithm, std.traits, std.random; auto distinct(Range)

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
You expect that output to contain only 5 stars. Better seen with this code: import std.stdio, std.range, std.algorithm, std.traits, std.random; auto distinct(Range)(Range r) if (isInputRange!Range) { bool[ForeachType!Range] mySet; return r.filter!((k) { if (k in mySet)

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Friday, 8 March 2013 at 22:36:35 UTC, Andrea Fontana wrote: On Friday, 8 March 2013 at 19:36:04 UTC, bearophile wrote: auto firstDistinct(Range)(Range r, in size_t n) if (isInputRange!Range) { bool[ForeachType!Range] mySet; return r.filter!((k) { if (k in mySet) return fa

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
Simpler: import std.stdio, std.range, std.algorithm; void main() { iota(5).map!((x) { write("*"); return x; }).filter!(_ => true).array; }

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Friday, 8 March 2013 at 23:37:20 UTC, Andrea Fontana wrote: On Friday, 8 March 2013 at 22:36:35 UTC, Andrea Fontana wrote: On Friday, 8 March 2013 at 19:36:04 UTC, bearophile wrote: auto firstDistinct(Range)(Range r, in size_t n) if (isInputRange!Range) { bool[ForeachType!Range] mySet; re

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
Andrea Fontana: But i think "front" was "cached", but it seems not... The caching of map front was recently removed. More test code: import std.stdio, std.range, std.algorithm, std.traits, std.random; void main() { 5.iota.map!((_) { auto x = uniform(0, 10); wr

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
Andrea Fontana: Here the answer: auto r=iota(100).map!((x) => uniform(0,10))(); writeln(r.front," ", r.front, " ", r.front, " ", r.front); I think that's not the answer. I think the answer is a bug in filter(). Bye, bearophile

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Friday, 8 March 2013 at 23:51:48 UTC, bearophile wrote: Andrea Fontana: Here the answer: auto r=iota(100).map!((x) => uniform(0,10))(); writeln(r.front," ", r.front, " ", r.front, " ", r.front); I think that's not the answer. I think the answer is a bug in filter(). Bye, bearophile M

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Friday, 8 March 2013 at 23:44:40 UTC, bearophile wrote: Simpler: import std.stdio, std.range, std.algorithm; void main() { iota(5).map!((x) { write("*"); return x; }).filter!(_ => true).array; } Chech this: 5.iota.map!((_) { auto x = uniform(0, 100

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
This is a shortened version of the filter(), it calls pred only n times, but it calls _input.front n*2 times. If _input.front is not deterministic (that means it's not pure, as in the case of distinct filtering function) it gives wrong results: struct FilterResult(alias pred, Range) { Ran

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
from sys import stdout def fun(x): stdout.write("*") return x list(filter(lambda x: True, map(fun, xrange(5 Sorry, I meant: from itertools import imap, ifilter from sys import stdout def fun(x): stdout.write("*") return x list(ifilter(lambda x: True, imap(fun, xrange(5

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
With this modified filter (that caches _input.front) it seems to work: import std.stdio, std.range, std.algorithm, std.traits, std.random; struct FilterResult2(alias pred, Range) { import std.traits: ForeachType; Range _input; ForeachType!Range rFront; this(Range r) {

Re: how to get top N distinct elements from range?

2013-03-08 Thread Andrea Fontana
On Saturday, 9 March 2013 at 01:00:26 UTC, bearophile wrote: This is a shortened version of the filter(), it calls pred only n times, but it calls _input.front n*2 times. If _input.front is not deterministic (that means it's not pure, as in the case of distinct filtering function) it gives wron

Re: how to get top N distinct elements from range?

2013-03-08 Thread bearophile
Andrea Fontana: Not funny btw :) It's not so easy to find out. And it's not easy to write a "cached map" function if you use classes or some pointers as ElementType... Not caching _input.front in filter() means that filter() assumes the front() of its argument range to act as a pure function