digitalmars.D - Recent improvements
- bearophile (23/23) Feb 16 2014 In the last days of beta3 D+Phobos is getting better in small but
- Bienlein (9/23) Feb 19 2014 There is the nice old Smalltalk-80 inject:into: method in the
- bearophile (5/13) Feb 19 2014 In D there's std.algorithm.reduce.
- Philippe Sigaud (7/11) Feb 19 2014 Oh, but D has `reduce`, for years now. It was maybe one of the very
- Bienlein (7/10) Feb 19 2014 I see. Unhappily, I don't have a D compiler handy. Would this
- Dicebot (2/7) Feb 19 2014 This works right now: immutable s = ["red", "blue"].join("");
- Philippe Sigaud (3/6) Feb 19 2014 It won't work, since binary + is not defined for strings. I don't
- Bienlein (4/16) Feb 19 2014 Then how can you add a sum method to a parameterized type if it
- Dicebot (3/5) Feb 19 2014 As far as I understand it will work on anything that has binary
- bearophile (6/9) Feb 19 2014 This could work, but join() is (or should be) asymptotically more
- Suliman (20/20) Feb 19 2014 Hm, I got next error on this code
- Dicebot (12/20) Feb 19 2014 This works:
- Timon Gehr (2/4) Feb 19 2014 No, a custom separator is not mandatory.
- Dicebot (3/10) Feb 19 2014 Ah, my bad, was expecting a default parameter, not an extra
- Gary Willoughby (2/25) Feb 19 2014 What is q{a + b} ?
- Dicebot (3/4) Feb 19 2014 http://dlang.org/lex.html#TokenString
In the last days of beta3 D+Phobos is getting better in small but significant ways: immutable s = ["red", "blue"]; auto js = s.join; This is very handy because you can join arrays from constant function arguments, or the result of a map that yields const items, etc. ----------------------- And I am finding the optional column number in error messages very handy, my editor/IDE often jumps at the right column, saving me tiny amounts of time that adds up making the debugging nicer. The experience is just better than before. ----------------------- auto r = [10, 20, 30].sum; This has replaced me tens of usages of: alias sum = reduce!q{a + b}; Or: alias sum = curry!(reduce!q{a + b}, 0); But I have found problems because currently sum(int[]) returns a long, see the discussion so far: https://d.puremagic.com/issues/show_bug.cgi?id=12169 Bye, bearophile
Feb 16 2014
On Sunday, 16 February 2014 at 13:22:09 UTC, bearophile wrote:In the last days of beta3 D+Phobos is getting better in small but significant ways: immutable s = ["red", "blue"]; auto js = s.join; This is very handy because you can join arrays from constant function arguments, or the result of a map that yields const items, etc. ----------------------- And I am finding the optional column number in error messages very handy, my editor/IDE often jumps at the right column, saving me tiny amounts of time that adds up making the debugging nicer. The experience is just better than before. ----------------------- auto r = [10, 20, 30].sum;There is the nice old Smalltalk-80 inject:into: method in the Collection class: | list sum | list := OrderedCollection new add: 1; add: 2; add: 3; yourself. sum := list inject: 0 into: [ :a :b | a + b ]. Transcript cr; show: sum. "prints 6" A little more general ;-). The Scala guys have also cloned it where it is called foldLeft.
Feb 19 2014
Bienlein:There is the nice old Smalltalk-80 inject:into: method in the Collection class: | list sum | list := OrderedCollection new add: 1; add: 2; add: 3; yourself. sum := list inject: 0 into: [ :a :b | a + b ]. Transcript cr; show: sum. "prints 6" A little more general ;-). The Scala guys have also cloned it where it is called foldLeft.In D there's std.algorithm.reduce. I think Scala guys have copied something older than Smalltalk. Bye, bearophile
Feb 19 2014
On Wed, Feb 19, 2014 at 1:29 PM, Bienlein <jeti789 web.de> wrote:There is the nice old Smalltalk-80 inject:into: method in the Collection class:A little more general ;-). The Scala guys have also cloned it where it is called foldLeft.Oh, but D has `reduce`, for years now. It was maybe one of the very first range algorithms to be put into Phobos, with map and filter. So we have generality already :-) What bearophile wanted was, on the contrary, to have a specialized-for-summing function, if only to make the intended behavior more clear.
Feb 19 2014
On Wednesday, 19 February 2014 at 12:43:10 UTC, Philippe Sigaud wrote:What bearophile wanted was, on the contrary, to have a specialized-for-summing function, if only to make the intended behavior more clear.I see. Unhappily, I don't have a D compiler handy. Would this compile: immutable s = ["red", "blue"].sum If not, it would be interesting to understand how that works :-) -- Bienlein
Feb 19 2014
On Wednesday, 19 February 2014 at 13:18:15 UTC, Bienlein wrote:I see. Unhappily, I don't have a D compiler handy. Would this compile: immutable s = ["red", "blue"].sum If not, it would be interesting to understand how that works :-) -- BienleinThis works right now: immutable s = ["red", "blue"].join("");
Feb 19 2014
On Wed, Feb 19, 2014 at 2:18 PM, Bienlein <jeti789 web.de> wrote:I see. Unhappily, I don't have a D compiler handy. Would this compile: immutable s = ["red", "blue"].sum If not, it would be interesting to understand how that works :-)It won't work, since binary + is not defined for strings. I don't understand what you want, here.
Feb 19 2014
On Wednesday, 19 February 2014 at 13:53:17 UTC, Philippe Sigaud wrote:On Wed, Feb 19, 2014 at 2:18 PM, Bienlein <jeti789 web.de> wrote:Then how can you add a sum method to a parameterized type if it only works for numbers?I see. Unhappily, I don't have a D compiler handy. Would this compile: immutable s = ["red", "blue"].sum If not, it would be interesting to understand how that works :-)It won't work, since binary + is not defined for strings. I don't understand what you want, here.
Feb 19 2014
On Wednesday, 19 February 2014 at 14:02:38 UTC, Bienlein wrote:Then how can you add a sum method to a parameterized type if it only works for numbers?As far as I understand it will work on anything that has binary operator "+" defined.
Feb 19 2014
Bienlein:Would this compile: immutable s = ["red", "blue"].sum If not, it would be interesting to understand how that works :-)This could work, but join() is (or should be) asymptotically more efficient: reduce!q{a ~ b}("", ["red", "blue"]) Bye, bearophile
Feb 19 2014
Hm, I got next error on this code import std.stdio; import std.array; void main() { immutable s = ["red", "blue"]; auto js = s.join; } D:\Project\2014\App1\main.d(7): Error: template std.array.join cannot deduce function from argument types !()(immutable(char[][])), candidates are: C:\D\dmd2\windows\bin\..\..\src\phobos\std\array.d(1526): std.array.join(RoR, R)(RoR ror, R sep) if (isInputRange!RoR && isInputRange!(ElementType!RoR) && isInputRange!R && is(Unqual!(ElementType!(ElementType!RoR)) == Unqual!(ElementType!R))) C:\D\dmd2\windows\bin\..\..\src\phobos\std\array.d(1573): std.array.join(RoR)(RoR ror) if (isInputRange!RoR && isInputRange!(ElementType!RoR)) [Finished in 0.3s]
Feb 19 2014
On Wednesday, 19 February 2014 at 13:07:39 UTC, Suliman wrote:Hm, I got next error on this code import std.stdio; import std.array; void main() { immutable s = ["red", "blue"]; auto js = s.join; }This works: auto s = ["red", "blue"]; auto js = s.join(""); 2 issues in your snippet: 1) need to define separator for join 2) fully immutable array can't act is InputRange as one can't popFront from it Arguably join should have made constraint check on tail-qualified copy of s instead though (as it is legal to copy imutable(char[]) into immutable(char)[]). I bet there is even bugzilla entry for it :)
Feb 19 2014
On 02/19/2014 02:21 PM, Dicebot wrote:2 issues in your snippet: 1) need to define separator for joinNo, a custom separator is not mandatory.
Feb 19 2014
On Wednesday, 19 February 2014 at 13:52:06 UTC, Timon Gehr wrote:On 02/19/2014 02:21 PM, Dicebot wrote:Ah, my bad, was expecting a default parameter, not an extra overload. See it now in docs.2 issues in your snippet: 1) need to define separator for joinNo, a custom separator is not mandatory.
Feb 19 2014
On Sunday, 16 February 2014 at 13:22:09 UTC, bearophile wrote:In the last days of beta3 D+Phobos is getting better in small but significant ways: immutable s = ["red", "blue"]; auto js = s.join; This is very handy because you can join arrays from constant function arguments, or the result of a map that yields const items, etc. ----------------------- And I am finding the optional column number in error messages very handy, my editor/IDE often jumps at the right column, saving me tiny amounts of time that adds up making the debugging nicer. The experience is just better than before. ----------------------- auto r = [10, 20, 30].sum; This has replaced me tens of usages of: alias sum = reduce!q{a + b}; Or: alias sum = curry!(reduce!q{a + b}, 0); But I have found problems because currently sum(int[]) returns a long, see the discussion so far: https://d.puremagic.com/issues/show_bug.cgi?id=12169 Bye, bearophileWhat is q{a + b} ?
Feb 19 2014
On Wednesday, 19 February 2014 at 14:03:53 UTC, Gary Willoughby wrote:What is q{a + b} ?http://dlang.org/lex.html#TokenString
Feb 19 2014