return types of std.functional functions
bearophile via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Oct 12 08:30:13 PDT 2014
yawniek:
> i found two snippets from the functional docs that do not work
> (anymore?)
> http://dlang.org/phobos/std_functional.html
>
>
> assert(compose!(map!(to!(int)), split)("1 2 3") == [1, 2, 3]);
> and
> int[] a = pipe!(readText, split, map!(to!(int)))("file.txt");
>
> throwing a std.array.array into the mix works fine.
>
> did this use to work?
I think those were little used, and today with UFCS they are even
less useful.
> is there any other way of doing it?
Untested:
"1 2 3".split.to!(int[]) == [1, 2, 3]
int[] a = "file.txt".readText.split.to!(int[]);
Once to!() accepts a lazy iterable you can save some GC activity
with:
"1 2 3".splitter.to!(int[]) == [1, 2, 3]
int[] a = "file.txt".readText.splitter.to!(int[]);
Currently you have to write this to do the same:
"1 2 3".splitter.map!(to!int).array == [1, 2, 3]
int[] a = "file.txt".readText.splitter.map!(to!(int)).array;
But you can also omit the latest .array:
"1 2 3".splitter.map!(to!int).equal([1, 2, 3])
Bye,
bearophile
More information about the Digitalmars-d-learn
mailing list