Re: Why is std.algorithm so complicated to use?

2012-07-17 Thread Jonathan M Davis
On Tuesday, July 17, 2012 11:47:07 Christophe Travert wrote: > The compiler could stop displaying at about 10 failed constrains and > claim there are more. It would be best if it could figure out what are > the 10 most interesting constrains, but that may not be easy! That seems like a good idea.

Re: Why is std.algorithm so complicated to use?

2012-07-17 Thread Christophe Travert
Jonathan M Davis , dans le message (digitalmars.D:172564), a écrit : > They're likely to contain a lot of stuff negation of other template > constraints. For instance, > > auto func(R)(R range) > if(isForwardRange!R && !isBidirectionalRange!R) > {} > > auto func(R)(R range) > if(isBidire

Re: Why is std.algorithm so complicated to use?

2012-07-16 Thread Jonathan M Davis
On Monday, July 16, 2012 22:53:36 Timon Gehr wrote: > On 07/16/2012 05:22 PM, Don Clugston wrote: > > On 10/07/12 16:59, Andrei Alexandrescu wrote: > >> On 7/10/12 9:59 AM, H. S. Teoh wrote: > >>> On Tue, Jul 10, 2012 at 09:28:51AM -0400, Andrei Alexandrescu wrote: > On 7/10/12 2:50 AM, Jacob

Re: Why is std.algorithm so complicated to use?

2012-07-16 Thread Timon Gehr
On 07/16/2012 05:22 PM, Don Clugston wrote: On 10/07/12 16:59, Andrei Alexandrescu wrote: On 7/10/12 9:59 AM, H. S. Teoh wrote: On Tue, Jul 10, 2012 at 09:28:51AM -0400, Andrei Alexandrescu wrote: On 7/10/12 2:50 AM, Jacob Carlborg wrote: On 2012-07-09 22:16, Andrei Alexandrescu wrote: So f

Re: Why is std.algorithm so complicated to use?

2012-07-16 Thread Don Clugston
On 10/07/12 16:59, Andrei Alexandrescu wrote: On 7/10/12 9:59 AM, H. S. Teoh wrote: On Tue, Jul 10, 2012 at 09:28:51AM -0400, Andrei Alexandrescu wrote: On 7/10/12 2:50 AM, Jacob Carlborg wrote: On 2012-07-09 22:16, Andrei Alexandrescu wrote: So foo is a range of strings, because each elemen

Re: Why is std.algorithm so complicated to use?

2012-07-16 Thread Jonathan M Davis
On Monday, July 16, 2012 10:08:11 Jacob Carlborg wrote: > Just _because_ of how "remove" works in std.algorithm and "erase" in > C++, I though "sort" behaved similar. Well, it does in that it sorts in place and returns the result, but the result and the original are more in line with one another

Re: Why is std.algorithm so complicated to use?

2012-07-16 Thread Jacob Carlborg
On 2012-07-16 09:18, Jonathan M Davis wrote: We do have InPlace in function names when one version of a function operates in place and another doesn't, but we haven't generally done that on functions which only have one version, and I don't expect that to change now (at least not for existing fu

Re: Why is std.algorithm so complicated to use?

2012-07-16 Thread Jonathan M Davis
On Monday, July 16, 2012 09:07:06 Jacob Carlborg wrote: > On 2012-07-16 00:03, Jonathan M Davis wrote: > > Iterators have exactly the same problem for exactly the same reasons as > > std.algorithm.remove (e.g. take a look at C++'s erase function). The only > > way to do this in place would be to cr

Re: Why is std.algorithm so complicated to use?

2012-07-16 Thread Jacob Carlborg
On 2012-07-16 00:03, Jonathan M Davis wrote: Iterators have exactly the same problem for exactly the same reasons as std.algorithm.remove (e.g. take a look at C++'s erase function). The only way to do this in place would be to create a separate removeInPlace function specifically for arrays. But

Re: Why is std.algorithm so complicated to use?

2012-07-15 Thread Jonathan M Davis
On Monday, July 16, 2012 01:47:15 Mehrdad wrote: > On Sunday, 15 July 2012 at 22:39:47 UTC, Jonathan M Davis wrote: > > On Sunday, July 15, 2012 18:24:47 Andrei Alexandrescu wrote: > >> On 7/15/12 6:22 PM, Mehrdad wrote: > >> > On Sunday, 15 July 2012 at 22:03:33 UTC, Jonathan M Davis > >> > > >>

Re: Why is std.algorithm so complicated to use?

2012-07-15 Thread Mehrdad
On Sunday, 15 July 2012 at 22:39:47 UTC, Jonathan M Davis wrote: On Sunday, July 15, 2012 18:24:47 Andrei Alexandrescu wrote: On 7/15/12 6:22 PM, Mehrdad wrote: > On Sunday, 15 July 2012 at 22:03:33 UTC, Jonathan M Davis > wrote: >> auto arr = [10, 22, 19, 4, 6]; >> arr = remove(arr, 3); >> as

Re: Why is std.algorithm so complicated to use?

2012-07-15 Thread Jonathan M Davis
On Sunday, July 15, 2012 18:24:47 Andrei Alexandrescu wrote: > On 7/15/12 6:22 PM, Mehrdad wrote: > > On Sunday, 15 July 2012 at 22:03:33 UTC, Jonathan M Davis wrote: > >> auto arr = [10, 22, 19, 4, 6]; > >> arr = remove(arr, 3); > >> assert(arr == [10, 22, 19, 6]); > > > > Yeah, the problem is th

Re: Why is std.algorithm so complicated to use?

2012-07-15 Thread Andrei Alexandrescu
On 7/15/12 6:22 PM, Mehrdad wrote: On Sunday, 15 July 2012 at 22:03:33 UTC, Jonathan M Davis wrote: auto arr = [10, 22, 19, 4, 6]; arr = remove(arr, 3); assert(arr == [10, 22, 19, 6]); Yeah, the problem is that this reallocates... doesn't

Re: Why is std.algorithm so complicated to use?

2012-07-15 Thread Andrei Alexandrescu
On 7/15/12 5:42 PM, Mehrdad wrote: Another example of how std.algorithm is so hard to use (it's almost tempting me to start swearing...): How do you remove an item from an array in place? It seems so darn simple, and yet it's not in std.algorithm (or std.range). It makes something so easy so te

Re: Why is std.algorithm so complicated to use?

2012-07-15 Thread Mehrdad
On Sunday, 15 July 2012 at 22:03:33 UTC, Jonathan M Davis wrote: auto arr = [10, 22, 19, 4, 6]; arr = remove(arr, 3); assert(arr == [10, 22, 19, 6]); Yeah, the problem is that this reallocates... (not claiming remove() itself is supposed to be efficient, but this is making it even

Re: Why is std.algorithm so complicated to use?

2012-07-15 Thread Jonathan M Davis
On Sunday, July 15, 2012 23:42:29 Mehrdad wrote: > Another example of how std.algorithm is so hard to use (it's > almost tempting me to start swearing...): > > How do you remove an item from an array in place? > > It seems so darn simple, and yet it's not in std.algorithm (or > std.range). It mak

Re: Why is std.algorithm so complicated to use?

2012-07-15 Thread Mehrdad
Another example of how std.algorithm is so hard to use (it's almost tempting me to start swearing...): How do you remove an item from an array in place? It seems so darn simple, and yet it's not in std.algorithm (or std.range). It makes something so easy so tedious.

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Ali Çehreli
On 07/10/2012 10:48 AM, Simen Kjaeraas wrote: > On Tue, 10 Jul 2012 16:15:09 +0200, Jacob Carlborg wrote: >> How do you store your ranges in a struct or class? Most of them are >> voldemort types. > > Well, there is std.range.inputRangeObject, but as the name indicates As the name "misleads"...

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 22:13, Christophe Travert wrote: Then, if the purpose is to make the code efficient, I would use the loop and append everything to the result without creating the params array, and even without creating the string p. Appender is made to append everything directly to it efficiently.

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Dmitry Olshansky
On 11-Jul-12 00:07, Tobias Pankrath wrote: Given: @property int delegate() foo(){ ... } @property int bar(){ ... } int baz(){ ... } foo(); // calls the delegate. bar(); // illegal baz; // ok, call baz dmd -property gets every single one of these wrong. -property does _not_ enforce @property

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Jacob Carlborg , dans le message (digitalmars.D:171769), a écrit : > On 2012-07-10 20:04, Andrei Alexandrescu wrote: > >> Then store an array. "No one's put a gun to yer head." >> http://youtu.be/CB1Pij54gTw?t=2m29s > > That's what I'm doing. > And that's what you should do. Algorithm are not m

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Daniel Murphy
"Jonathan M Davis" wrote in message news:mailman.249.1341951069.31962.digitalmar...@puremagic.com... > On Tuesday, July 10, 2012 21:07:51 Jacob Carlborg wrote: >> Second, it would be much easier if it would be possible to better name >> these ranges, something like interfaces but without polymorp

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Jacob Carlborg , dans le message (digitalmars.D:171725), a écrit : >> To make the best implementation would require to know how the String >> context works. >> > String is a wrapper around str.array.Appender. Then, if the purpose is to make the code efficient, I would use the loop and append ever

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jonathan M Davis
On Tuesday, July 10, 2012 21:07:51 Jacob Carlborg wrote: > Second, it would be much easier if it would be possible to better name > these ranges, something like interfaces but without polymorphism. This > as been mentioned several times before. They're templated types. They have to be templated ty

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jonathan M Davis
On Tuesday, July 10, 2012 22:04:17 Timon Gehr wrote: > @property int delegate() foo(){ ... } > @property int bar(){ ... } > int baz(){ ... } > > foo(); // calls the delegate. > bar(); // illegal > baz; // ok, call baz > > dmd -property gets every single one of these wrong. -property does _not_ >

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Tobias Pankrath
Given: @property int delegate() foo(){ ... } @property int bar(){ ... } int baz(){ ... } foo(); // calls the delegate. bar(); // illegal baz; // ok, call baz dmd -property gets every single one of these wrong. -property does _not_ enforce @property semantics. It only adds a silly rule that

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Daniel Murphy
"Andrei Alexandrescu" wrote in message news:jthr4a$3f3$1...@digitalmars.com... > > I swear I'd just call it "range". > > Andrei > Sure, why not. http://d.puremagic.com/issues/show_bug.cgi?id=8371

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Timon Gehr
On 07/10/2012 09:36 PM, Jesse Phillips wrote: On Tuesday, 10 July 2012 at 00:17:12 UTC, Timon Gehr wrote: I have heard that rumour, but TDPL wisely specifies the behaviour of the @property attribute as follows: 'In particular "property" is recognized by the the compiler and signals the fact tha

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Brad Anderson
On Tue, Jul 10, 2012 at 1:22 PM, Jacob Carlborg wrote: > On 2012-07-10 20:53, Brad Anderson wrote: > > For what it's worth: >> >> e = 10_000_000 >> a = ((1..e).to_a + (1..e).to_a).sort.uniq.map{ |e| e } >> >> Runs in 21,320 ms on my machine with Ruby 1.9.3 whereas: >> >> auto end

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jesse Phillips
On Tuesday, 10 July 2012 at 00:17:12 UTC, Timon Gehr wrote: I have heard that rumour, but TDPL wisely specifies the behaviour of the @property attribute as follows: 'In particular "property" is recognized by the the compiler and signals the fact that the function bearing such an attribute must

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 20:53, Brad Anderson wrote: For what it's worth: e = 10_000_000 a = ((1..e).to_a + (1..e).to_a).sort.uniq.map{ |e| e } Runs in 21,320 ms on my machine with Ruby 1.9.3 whereas: auto end = 10_000_000; auto a = chain(iota(1, end), iota(1, end)).array()

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 20:04, Andrei Alexandrescu wrote: Then store an array. "No one's put a gun to yer head." http://youtu.be/CB1Pij54gTw?t=2m29s That's what I'm doing. -- /Jacob Carlborg

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 19:30, Jonathan M Davis wrote: typeof(bar()) x; works. But regardless, given that you're dealing with a return type which is auto, there's really no other choice. Even if we weren't using voldemort types for stuff like map, the return type would still be templated and depend on th

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 19:55, Andrei Alexandrescu wrote: Clearly this and any documentation can be improved, but I'd say if it says "range" there there's no assumption "sorted range" there. I think the main thing that could be expressed better is "unique consecutive". Fair enough. I think it would b

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 19:23, Daniel Murphy wrote: writeln([1, 2, 3].map!"a"()[2]); Sort is in place, and therefore requires more than random access, you need to be able to modify the data you're operating on. Something like [3, 4].map!func().selectionSort() could work lazily without needing to modify

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Brad Anderson
On Tue, Jul 10, 2012 at 3:35 AM, Jacob Carlborg wrote: > On 2012-07-10 08:59, Dmitry Olshansky wrote: > > Can you do it in other languages? >> > > Sure, in Ruby, but that only works on arrays: > > p [5, 3, 5, 6, 8].uniq.map{ |e| e.to_s }.sort > > Prints: > > ["3", "5", "6", "8"] > > -- > /Jacob

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Daniel Murphy
"Andrei Alexandrescu" wrote in message news:jthr4a$3f3$1...@digitalmars.com... > On 7/10/12 2:08 PM, Jonathan M Davis wrote: >>> That does seem good to have. What would be a better name than makeRange? >> >> I see no problem with makeRange. It seems like a sensible name to me. >> You're >> takin

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Timon Gehr
On 07/10/2012 08:09 PM, Andrei Alexandrescu wrote: On 7/10/12 2:08 PM, Jonathan M Davis wrote: That does seem good to have. What would be a better name than makeRange? I see no problem with makeRange. It seems like a sensible name to me. You're taking a sequence of elements and making a range

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Simen Kjaeraas
On Tue, 10 Jul 2012 16:27:31 +0200, Andrei Alexandrescu wrote: On 7/10/12 8:52 AM, Simen Kjaeraas wrote: bearophile (who else? :p) has suggested the addition of eager and in-place versions of some ranges, and I think he has a very good point. Where would good old loops not work for eager

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Simen Kjaeraas
On Tue, 10 Jul 2012 20:09:15 +0200, Andrei Alexandrescu wrote: On 7/10/12 2:08 PM, Jonathan M Davis wrote: That does seem good to have. What would be a better name than makeRange? I see no problem with makeRange. It seems like a sensible name to me. You're taking a sequence of elements

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 2:02 PM, Simen Kjaeraas wrote: On Tue, 10 Jul 2012 17:21:14 +0200, Christophe Travert wrote: "Simen Kjaeraas" , dans le message (digitalmars.D:171678), a écrit : Well, I haven't been able to use a single function from std.algorithm without adding a lot of calls to "array" or "to!(s

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 2:08 PM, Jonathan M Davis wrote: That does seem good to have. What would be a better name than makeRange? I see no problem with makeRange. It seems like a sensible name to me. You're taking a sequence of elements and making a range out of them. I swear I'd just call it "range". An

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jonathan M Davis
On Tuesday, July 10, 2012 13:58:50 Andrei Alexandrescu wrote: > On 7/10/12 1:17 PM, Daniel Murphy wrote: > > "Christophe Travert" wrote in message > > news:jthmu8$2s5b$1...@digitalmars.com... > > > >> "Daniel Murphy" , dans le message (digitalmars.D:171720), a écrit : > >>> Could it be extended to

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 1:05 PM, Jacob Carlborg wrote: On 2012-07-10 16:22, Andrei Alexandrescu wrote: It may be the case you're trying to write ruby in D. I rarely need to convert stuff to arrays. Mostly I receive an array, do some operations on it and then want to store the result in an instance variabl

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Simen Kjaeraas
On Tue, 10 Jul 2012 17:21:14 +0200, Christophe Travert wrote: "Simen Kjaeraas" , dans le message (digitalmars.D:171678), a écrit : Well, I haven't been able to use a single function from std.algorithm without adding a lot of calls to "array" or "to!(string)". I think the things I'm trying to

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 12:57 PM, Jacob Carlborg wrote: On 2012-07-10 16:36, Dmitry Olshansky wrote: Dunno, to me it says SORTED in one big scary thought. Again it should ether check constraint or put some more useful description. e.g. "(functionality akin to the uniq system utility)" doesn't strike me as he

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 1:17 PM, Daniel Murphy wrote: "Christophe Travert" wrote in message news:jthmu8$2s5b$1...@digitalmars.com... "Daniel Murphy" , dans le message (digitalmars.D:171720), a écrit : Could it be extended to accept multiple values? (sort of like chain) eg. foreach(x; makeRange(23, 7, 1990)

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 12:38 PM, Jacob Carlborg wrote: Can't "map" and "filter" return a random-access range if that's what they receive? (replied to by others) Now I understand if you come from a place where there's no concern over hidden allocations and extra work for the benefit of convenience, you ma

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Simen Kjaeraas
On Tue, 10 Jul 2012 16:15:09 +0200, Jacob Carlborg wrote: On 2012-07-10 14:53, Dmitry Olshansky wrote: Too bad, as there is no need to make an array when you map something. How do you store your ranges in a struct or class? Most of them are voldemort types. Well, there is std.range.inpu

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Daniel Murphy
"Christophe Travert" wrote in message news:jthp14$30em$1...@digitalmars.com... > "Daniel Murphy" , dans le message (digitalmars.D:171741), a écrit : >> "Christophe Travert" wrote in message >> news:jthmu8$2s5b$1...@digitalmars.com... >>> "Daniel Murphy" , dans le message (digitalmars.D:171720),

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
"Daniel Murphy" , dans le message (digitalmars.D:171741), a écrit : > "Christophe Travert" wrote in message > news:jthmu8$2s5b$1...@digitalmars.com... >> "Daniel Murphy" , dans le message (digitalmars.D:171720), a écrit : >>> Could it be extended to accept multiple values? (sort of like chain) >>

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jonathan M Davis
On Tuesday, July 10, 2012 14:27:14 Simen Kjaeraas wrote: > On Tue, 10 Jul 2012 09:04:31 +0200, Jonathan M Davis > > wrote: > > The problem is that map is lazy, so it can't be a random access range, > > Sure it can. If the underlying range is random-access or bidirectional, > so can map be. What

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jonathan M Davis
On Tuesday, July 10, 2012 12:22:30 Andrei Alexandrescu wrote: > On 7/10/12 12:01 PM, Christophe Travert wrote: > > Andrei Alexandrescu , dans le message (digitalmars.D:171717), a écrit : > >> auto singletonRange(E)(E value) > >> { > >> > >> return repeat(value).takeExactly(1); > >> > >> } > > >

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jonathan M Davis
On Tuesday, July 10, 2012 18:57:25 Jacob Carlborg wrote: > On 2012-07-10 16:36, Dmitry Olshansky wrote: > > typeof to the rescue. In fact I'm not so proud of voldemort types. As > > when you need to sotre range somewhere they stand in your way for no > > benefit. > > I'm not exactly sure how you m

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Daniel Murphy
"Jacob Carlborg" wrote in message news:jthnlo$2tjg$1...@digitalmars.com... > On 2012-07-10 18:42, Daniel Murphy wrote: >> "Jacob Carlborg" wrote in message >> news:jthlpf$2pnb$1...@digitalmars.com... >>> >>> Can't "map" and "filter" return a random-access range if that's what >>> they >>> recei

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Jacob Carlborg , dans le message (digitalmars.D:171739), a écrit : > On 2012-07-10 18:42, Daniel Murphy wrote: >> "Jacob Carlborg" wrote in message >> news:jthlpf$2pnb$1...@digitalmars.com... >>> >>> Can't "map" and "filter" return a random-access range if that's what they >>> receive? >>> >> map

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Daniel Murphy
"Christophe Travert" wrote in message news:jthmu8$2s5b$1...@digitalmars.com... > "Daniel Murphy" , dans le message (digitalmars.D:171720), a écrit : >> Could it be extended to accept multiple values? (sort of like chain) >> eg. >> foreach(x; makeRange(23, 7, 1990)) // NO allocations! >> { >>

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 18:42, Daniel Murphy wrote: "Jacob Carlborg" wrote in message news:jthlpf$2pnb$1...@digitalmars.com... Can't "map" and "filter" return a random-access range if that's what they receive? map can, and does. It doesn't seem to: auto a = [3, 4].map!(x => x); auto b = a.sort; Re

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 16:22, Andrei Alexandrescu wrote: It may be the case you're trying to write ruby in D. I rarely need to convert stuff to arrays. Mostly I receive an array, do some operations on it and then want to store the result in an instance variable in a class or struct. It's quite awkwar

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Jacob Carlborg , dans le message (digitalmars.D:171725), a écrit : > On 2012-07-10 17:11, Christophe Travert wrote: > >> What is wrong with foo.chain(["bar"])? > > I think it conceptually wrong for what I want to do. I don't know if I > misunderstood ranges completely but I'm seeing them as an a

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
"Daniel Murphy" , dans le message (digitalmars.D:171720), a écrit : > Could it be extended to accept multiple values? (sort of like chain) > eg. > foreach(x; makeRange(23, 7, 1990)) // NO allocations! > { > > } > I would use this in a lot of places I currently jump through hoops to get a

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 16:36, Dmitry Olshansky wrote: typeof to the rescue. In fact I'm not so proud of voldemort types. As when you need to sotre range somewhere they stand in your way for no benefit. I'm not exactly sure how you mean but this is what I came up with: import std.algorithm; import std.

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Andrei Alexandrescu , dans le message (digitalmars.D:171723), a écrit : >> auto emptyRange(E)(E value) >> { >>return repeat(value).takeNone; >> } > That also seems to answer Jonathan's quest about defining emptyRange. > Just use takeNone(R.init). err, that should be more like: auto singleto

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Timon Gehr
On 07/10/2012 06:40 PM, Jacob Carlborg wrote: On 2012-07-10 16:54, Andrei Alexandrescu wrote: Actually I take that back. One may use uniq to remove duplicates in unsorted ranges. How does that work? It didn't work in my example. It works. It removes consecutive duplicates. 'uniq' in ruby i

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Daniel Murphy
"Jacob Carlborg" wrote in message news:jthlpf$2pnb$1...@digitalmars.com... > > Can't "map" and "filter" return a random-access range if that's what they > receive? > map can, and does. filter cannot, because it doesn't know which element is number 4 until it knows if element 3 is included or no

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Timon Gehr
On 07/10/2012 06:38 PM, Jacob Carlborg wrote: On 2012-07-10 17:30, Andrei Alexandrescu wrote: It would be possible to chain a single element to the end of a range. One related thing to do is to define singletonRange(x) that returns a range with exactly one element, namely x. Ok, I saw that su

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 16:54, Andrei Alexandrescu wrote: Actually I take that back. One may use uniq to remove duplicates in unsorted ranges. How does that work? It didn't work in my example. -- /Jacob Carlborg

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 17:30, Andrei Alexandrescu wrote: It would be possible to chain a single element to the end of a range. One related thing to do is to define singletonRange(x) that returns a range with exactly one element, namely x. Ok, I saw that suggestion in another post. Ranges and std.algo

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 17:11, Christophe Travert wrote: What is wrong with foo.chain(["bar"])? I think it conceptually wrong for what I want to do. I don't know if I misunderstood ranges completely but I'm seeing them as an abstraction over a collection. With most mutable collection you can add/appen

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 16:49, Dmitry Olshansky wrote: I'm not sure how map can fit there. If anything you need a foreach (and you have it;) ) to build a _string_. I'd rather output ',' right there inside of loop. Thus avoiding costly join and appending to a new buffer on each iteration. Sure if remove

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 12:01 PM, Christophe Travert wrote: Andrei Alexandrescu , dans le message (digitalmars.D:171717), a écrit : auto singletonRange(E)(E value) { return repeat(value).takeExactly(1); } It would be much better to use: auto singletonRange(E)(E value) { return repeat(value).ta

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Andrei Alexandrescu , dans le message (digitalmars.D:171717), a écrit : > On 7/10/12 11:11 AM, Christophe Travert wrote: >> If you do not want the heap allocation of the array, you can create a >> one-element range to feed to chain (maybe such a thing could be placed >> in phobos, next to takeOne).

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Daniel Murphy
"Andrei Alexandrescu" wrote in message news:jthiba$2cg0$1...@digitalmars.com... > On 7/10/12 11:11 AM, Christophe Travert wrote: >> If you do not want the heap allocation of the array, you can create a >> one-element range to feed to chain (maybe such a thing could be placed >> in phobos, next to

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jonathan M Davis
On Tuesday, July 10, 2012 15:21:14 Christophe Travert wrote: > "Simen Kjaeraas" , dans le message (digitalmars.D:171678), a écrit : > >> Well, I haven't been able to use a single function from std.algorithm > >> without adding a lot of calls to "array" or "to!(string)". I think the > >> things I'm

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 11:11 AM, Christophe Travert wrote: If you do not want the heap allocation of the array, you can create a one-element range to feed to chain (maybe such a thing could be placed in phobos, next to takeOne). struct OneElementRange(E) { E elem; bool passed; @property ref E front

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Jacob Carlborg , dans le message (digitalmars.D:171690), a écrit : >> int[] arr = [ 1, 2, 2, 2, 2, 3, 4, 4, 4, 5 ]; >> assert(equal(uniq(arr), [ 1, 2, 3, 4, 5 ][])); > > How should I know that from the example? Maybe there should be an example with an unsorted range, and a better explanation:

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 11:17 AM, Jonathan M Davis wrote: On Tuesday, July 10, 2012 10:21:23 Andrei Alexandrescu wrote: On 7/10/12 5:35 AM, Jacob Carlborg wrote: On 2012-07-10 08:59, Dmitry Olshansky wrote: Can you do it in other languages? Sure, in Ruby, but that only works on arrays: p [5, 3, 5, 6, 8]

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 9:56 AM, Jacob Carlborg wrote: On 2012-07-10 15:28, Andrei Alexandrescu wrote: We can arrange things in the library that a custom message is issued, or in the compiler to do it once for all. At any rate whenever there's an error pointing somewhere in the library's code that's an insu

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
"Simen Kjaeraas" , dans le message (digitalmars.D:171678), a écrit : >> Well, I haven't been able to use a single function from std.algorithm >> without adding a lot of calls to "array" or "to!(string)". I think the >> things I'm trying to do seems trivial and quite common. I'm I overrating >

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Dmitry Olshansky , dans le message (digitalmars.D:171679), a écrit : > Because uniq work only on sorted ranges? Have you tried reading docs? > " > Iterates unique consecutive elements of the given range (functionality > akin to the uniq system utility). Equivalence of elements is assessed by > us

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jonathan M Davis
On Tuesday, July 10, 2012 10:21:23 Andrei Alexandrescu wrote: > On 7/10/12 5:35 AM, Jacob Carlborg wrote: > > On 2012-07-10 08:59, Dmitry Olshansky wrote: > >> Can you do it in other languages? > > > > Sure, in Ruby, but that only works on arrays: > > > > p [5, 3, 5, 6, 8].uniq.map{ |e| e.to_s }.

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Christophe Travert
Jacob Carlborg , dans le message (digitalmars.D:171685), a écrit : > I mean, is it possible to have the original code work? > > auto bar = foo.chain("bar"); > > Or perhaps more appropriate: > > auto bar = foo.append("bar"); What is wrong with foo.chain(["bar"])? If you do not want the heap all

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Dmitry Olshansky
On 10-Jul-12 19:00, Timon Gehr wrote: On 07/10/2012 02:53 PM, Dmitry Olshansky wrote: On 10-Jul-12 15:37, Jacob Carlborg wrote: On 2012-07-10 12:05, Dmitry Olshansky wrote: and what type has the return of map ? Let me guess - array. Yes, and that is what I _want_. Too bad, as there is no

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Timon Gehr
On 07/10/2012 02:53 PM, Dmitry Olshansky wrote: On 10-Jul-12 15:37, Jacob Carlborg wrote: On 2012-07-10 12:05, Dmitry Olshansky wrote: and what type has the return of map ? Let me guess - array. Yes, and that is what I _want_. Too bad, as there is no need to make an array when you map some

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 9:59 AM, H. S. Teoh wrote: On Tue, Jul 10, 2012 at 09:28:51AM -0400, Andrei Alexandrescu wrote: On 7/10/12 2:50 AM, Jacob Carlborg wrote: On 2012-07-09 22:16, Andrei Alexandrescu wrote: So foo is a range of strings, because each element of it is a string. Then you want to chain a

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 10:26 AM, Andrei Alexandrescu wrote: On 7/10/12 8:50 AM, Nick Treleaven wrote: On 10/07/2012 12:37, Jacob Carlborg wrote: The corresponding D version would be: auto a = [5, 3, 5, 6, 8].uniq.map!(x => x.to!(string)).array.sort.array; writeln(a); I'm guessing that's three allocations

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Dmitry Olshansky
On 10-Jul-12 17:59, H. S. Teoh wrote: On Tue, Jul 10, 2012 at 09:28:51AM -0400, Andrei Alexandrescu wrote: On 7/10/12 2:50 AM, Jacob Carlborg wrote: On 2012-07-09 22:16, Andrei Alexandrescu wrote: So foo is a range of strings, because each element of it is a string. Then you want to chain a

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Dmitry Olshansky
On 10-Jul-12 17:56, Jacob Carlborg wrote: On 2012-07-10 15:28, Andrei Alexandrescu wrote: We can arrange things in the library that a custom message is issued, or in the compiler to do it once for all. At any rate whenever there's an error pointing somewhere in the library's code that's an insu

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Dmitry Olshansky
On 10-Jul-12 18:15, Jacob Carlborg wrote: On 2012-07-10 14:53, Dmitry Olshansky wrote: Too bad, as there is no need to make an array when you map something. How do you store your ranges in a struct or class? Most of them are voldemort types. typeof to the rescue. In fact I'm not so proud o

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 8:52 AM, Simen Kjaeraas wrote: bearophile (who else? :p) has suggested the addition of eager and in-place versions of some ranges, and I think he has a very good point. Where would good old loops not work for eager stuff? Andrei

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 8:50 AM, Nick Treleaven wrote: On 10/07/2012 12:37, Jacob Carlborg wrote: The corresponding D version would be: auto a = [5, 3, 5, 6, 8].uniq.map!(x => x.to!(string)).array.sort.array; writeln(a); I'm guessing that's three allocations. But that doesn't even work, it prints: ["3", "

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 8:27 AM, Simen Kjaeraas wrote: On Tue, 10 Jul 2012 09:04:31 +0200, Jonathan M Davis wrote: The problem is that map is lazy, so it can't be a random access range, Sure it can. If the underlying range is random-access or bidirectional, so can map be. What can't be (as easily) done i

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 5:41 AM, Jacob Carlborg wrote: Well, I haven't been able to use a single function from std.algorithm without adding a lot of calls to "array" or "to!(string)". I think the things I'm trying to do seems trivial and quite common. I'm I overrating std.algorithm or does it not fit my needs

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 5:35 AM, Jacob Carlborg wrote: On 2012-07-10 08:59, Dmitry Olshansky wrote: Can you do it in other languages? Sure, in Ruby, but that only works on arrays: p [5, 3, 5, 6, 8].uniq.map{ |e| e.to_s }.sort This is very inefficient. I agree that if efficiency wasn't a concern for st

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 14:53, Dmitry Olshansky wrote: Too bad, as there is no need to make an array when you map something. How do you store your ranges in a struct or class? Most of them are voldemort types. Then you need something like transform. Anyway the result of map should be sortable it's a

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 14:50, Nick Treleaven wrote: uniq needs sorted input: auto r = [5, 3, 5, 6, 8].sort.uniq.map!(x => x.to!string); writeln(r); Tested with dmd 2.059. I think the above should be one allocation (except for the strings). But I want the result to be an array, which would require an

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 14:55, Dmitry Olshansky wrote: Maybe uniq should require a SortedRange? And say so explicitly in the docs. Than it should be enforced with a constraint (if possible). -- /Jacob Carlborg

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Jacob Carlborg
On 2012-07-10 15:28, Andrei Alexandrescu wrote: We can arrange things in the library that a custom message is issued, or in the compiler to do it once for all. At any rate whenever there's an error pointing somewhere in the library's code that's an insufficient template constraint that should be

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread H. S. Teoh
On Tue, Jul 10, 2012 at 09:28:51AM -0400, Andrei Alexandrescu wrote: > On 7/10/12 2:50 AM, Jacob Carlborg wrote: > >On 2012-07-09 22:16, Andrei Alexandrescu wrote: > > > >>So foo is a range of strings, because each element of it is a > >>string. Then you want to chain a range of strings with a str

Re: Why is std.algorithm so complicated to use?

2012-07-10 Thread Andrei Alexandrescu
On 7/10/12 3:48 AM, Jonathan M Davis wrote: On Monday, July 09, 2012 16:16:42 Andrei Alexandrescu wrote: Another example: auto str = ["foo", "bar"].map!(x => x); auto f = str.sort(); Results in: http://pastebin.com/BeePWQk9 The first error message is at clear as it goes: Error: r[i2] is n

  1   2   >