Thread overview | |||||
---|---|---|---|---|---|
|
October 12, 2014 return types of std.functional functions | ||||
---|---|---|---|---|
| ||||
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? |
October 12, 2014 Re: return types of std.functional functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to yawniek | 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 |
October 12, 2014 Re: return types of std.functional functions | ||||
---|---|---|---|---|
| ||||
Posted in reply to yawniek | 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 |
Copyright © 1999-2021 by the D Language Foundation