Possible improvement of phobos map?
Paul Backus
snarwin at gmail.com
Mon Jul 4 19:55:21 UTC 2022
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
More information about the Digitalmars-d
mailing list