www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Recent improvements

reply "bearophile" <bearophileHUGS lycos.com> writes:
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
next sibling parent reply "Bienlein" <jeti789 web.de> writes:
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
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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
parent reply "Bienlein" <jeti789 web.de> writes:
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
next sibling parent "Dicebot" <public dicebot.lv> writes:
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 :-)

 -- Bienlein
This works right now: immutable s = ["red", "blue"].join("");
Feb 19 2014
prev sibling next sibling parent reply Philippe Sigaud <philippe.sigaud gmail.com> writes:
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
parent reply "Bienlein" <jeti789 web.de> writes:
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:
 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.
Then how can you add a sum method to a parameterized type if it only works for numbers?
Feb 19 2014
parent "Dicebot" <public dicebot.lv> writes:
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
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling next sibling parent reply "Suliman" <evermind live.ru> writes:
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
parent reply "Dicebot" <public dicebot.lv> writes:
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
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 02/19/2014 02:21 PM, Dicebot wrote:
 2 issues in your snippet:

 1) need to define separator for join
No, a custom separator is not mandatory.
Feb 19 2014
parent "Dicebot" <public dicebot.lv> writes:
On Wednesday, 19 February 2014 at 13:52:06 UTC, Timon Gehr wrote:
 On 02/19/2014 02:21 PM, Dicebot wrote:
 2 issues in your snippet:

 1) need to define separator for join
No, a custom separator is not mandatory.
Ah, my bad, was expecting a default parameter, not an extra overload. See it now in docs.
Feb 19 2014
prev sibling parent reply "Gary Willoughby" <dev nomad.so> writes:
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,
 bearophile
What is q{a + b} ?
Feb 19 2014
parent "Dicebot" <public dicebot.lv> writes:
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