Possible improvement of phobos map?
Timon Gehr
timon.gehr at gmx.ch
Wed Jul 6 18:29:39 UTC 2022
On 04.07.22 21:44, 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.
>
> With some slight changes to map (which wouldn't be needed if pattern
> matching exists), one can do this:
>
> ```d
> import std;
>
> struct MapPrototype(alias f, R) {
> R r;
> bool empty() { return r.empty; }
> auto front() { return f(r.front.tupleof); }
> void popFront() { r.popFront; }
> auto save() { return typeof(this)(r.save); }
> }
>
> auto mapPrototype(alias f, R)(R r) {
> return MapPrototype!(f,R)(r);
> }
>
> struct Point {
> int x;
> int y;
> }
>
> void main() {
> auto r = iota(1,10);
> auto gcds = cartesianProduct(r,r).mapPrototype!(gcd);
> gcds.writeln;
> auto a = ["a":5, "b":6];
> a.byPair.mapPrototype!((name,value)=>format!"%s=%s"(name,value)).writeln;
> [Point(1,2),Point(3,4)].mapPrototype!((x,y)=>x*x+y*y).writeln;
> }
> ```
>
> Thoughts?
>
>
I want this as well, but it is probably better to improve the language
than to add additional behavior to Phobos.
The fundamental issue is that there is a difference between functions
with multiple arguments and functions that accept a single tuple
argument. Argument lists are basically tuples, but we need to make them
first-class values. Unfortunately, this is not so easy to retrofit into
existing language semantics.
More information about the Digitalmars-d
mailing list