Possible improvement of phobos map?
JG
someone at simewhere.com
Tue Jul 5 21:38:18 UTC 2022
On Monday, 4 July 2022 at 19:55:21 UTC, Paul Backus wrote:
> On Monday, 4 July 2022 at 19:44:11 UTC, JG wrote:
>> Hi,
>>
>> There was a recent discussion regarding suggesting some
>> special algorithms for associative arrays, which made we
>> wonder if the problem isn't actually that it is a slightly
>> annoying when working with ranges of tuples or structs.
>>
>> For instance you might have code like this
>>
>> ```d
>> auto a = ["a":5, "b":6];
>> a.byPair.map!(t=>format!"%s=%s"(t[0],t[1])).writeln;
>> ```
>> or
>> ```d
>> auto r = iota(1,10);
>> auto gcds = cartesianProduct(r,r).map!(t=>gcd(t[0],t[1]));
>>
>> ```
>> which while not too bad isn't as readable as it could be, and
>> it gets worse when the chain gets longer.
>
> You can clean these up using the recently-added
> [`std.functional.bind`][1]:
>
> ```d
> a.byPair.map!(bind!((k, v) => format!"%s=%s"(k,
> v))).writeln;
> ```
>
> ```d
> auto gcds = cartesianProduct(r,r).map!(bind!gcd);
> ```
>
> [1]: https://phobos.dpldocs.info/std.functional.bind.html
Thanks that is cleaner. I am not sure if it as clean as if map
implicitly did it, but perhaps it could also be confusing (and
plenty of other range based algorithms would need similar
changes).
More information about the Digitalmars-d
mailing list