www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - return types of std.functional functions

reply "yawniek" <dlang srtnwz.com> writes:
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? is there any other way of doing it?
Oct 12 2014
next sibling parent =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
On 10/12/2014 08:04 AM, yawniek wrote:
 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? is there any other way of doing it?
The proper way is to call std.algorithm.equal, which compares ranges element-by-element: assert(compose!(map!(to!(int)), split)("1 2 3").equal([1, 2, 3])); Ali
Oct 12 2014
prev sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Oct 12 2014