digitalmars.D.learn - return types of std.functional functions
- yawniek (8/8) Oct 12 2014 i found two snippets from the functional docs that do not work
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (5/12) Oct 12 2014 The proper way is to call std.algorithm.equal, which compares ranges
- bearophile (17/26) Oct 12 2014 I think those were little used, and today with UFCS they are even
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
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
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