Thread overview | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 16, 2014 Recent improvements | ||||
---|---|---|---|---|
| ||||
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 |
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.
|
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bienlein | 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
|
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bienlein | 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. |
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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] |
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Philippe Sigaud | 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
|
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suliman | 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 :)
|
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bienlein | 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("");
|
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dicebot | 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.
|
February 19, 2014 Re: Recent improvements | ||||
---|---|---|---|---|
| ||||
Posted in reply to Bienlein | 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.
|
Copyright © 1999-2021 by the D Language Foundation