Why is std.algorithm so complicated to use?
bearophile
bearophileHUGS at lycos.com
Mon Jul 9 13:46:45 PDT 2012
Jacob Carlborg:
> import std.algorithm;
> import std.range;
>
> struct Foo {}
>
> auto f = Foo();
> auto foos = [f];
> auto foo = foos.map!(x => "foo");
> auto bar = foo.chain("bar");
I suggest to always compile with "-wi -property".
chain() takes two iterables. This means both arguments need to
yield the same type. But in your code foo is an iterable of
strings, and you try to chain it an iterable of chars. In Python
that works just because there are no chars, only single-char
strings.
It works if you turn the second argument of chain in an iterable
of strings:
import std.stdio, std.algorithm, std.range;
void main() {
import std.algorithm;
import std.range;
struct Foo {}
auto f = Foo();
auto foos = [f];
auto foo = foos.map!(x => "foo")();
auto bar = foo.chain(["bar"]);
writeln(bar);
}
> This simple example result in the follow error:
>
> http://pastebin.com/E4LV2UBE
>
> Another example:
>
> auto str = ["foo", "bar"].map!(x => x);
> auto f = str.sort();
Map returns a lazy iterable. Generally you need an eager
random-access iterable to sort (but it seems there are some
exceptions like when you sort a zip...).
Bye,
bearophile
More information about the Digitalmars-d
mailing list