[Challenge] implementing the ambiguous operator in D

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Sep 5 09:38:41 PDT 2010


On 09/05/2010 09:30 AM, Philippe Sigaud wrote:
> On Sun, Sep 5, 2010 at 15:56, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org <mailto:SeeWebsiteForEmail at erdani.org>>
> wrote:
>
>
>         equivalent to the problem:
>
>         auto solution = comp!("[a,b]", "a*b == 8")([1,2,3], [4,5,6]);
>
>         solution is a range, in this case a one element range,
>         containing only
>         [2,4].
>
>     I did a range comprehension maybe one year ago, which would be partially
>
>     Here we need to have a crossProduct range, as we discussed in the
>     thread "Combining infinite ranges". Then it's easy to combine it
>     with filter:
>
>     auto solutions = filter!"a*b == 8"(crossProduct([1,2,3], [4,5,6]));
>
>
> Not exactly: crossProduct is a range, and the only element type that
> makes sense is Tuple!(A,B).
> Unless of course you're interested in adding binary predicates to
> filter, which would be understood as automatically opening tuples (and
> of course, would statically check the length of the tuple and the arity
> of the passed function). I can add this to Phobos.

Oh indeed. I think it's not onerous to require the client to write:

auto solutions = filter!"a._0*a._0 == 8"(crossProduct([1,2,3], [4,5,6]));

> What you call crossProduct can be seen as zipWith!tuple(range, range2),
> which

Yah, per your follow-up post, it's a different problem. It's also a much 
more difficult one. I convinced myself crossProduct is impossible to 
implement if one input range and one infinite forward range are 
simultaneously present. It works with any number of infinite forward 
ranges, and also with other combinations. I couldn't formalize the exact 
requirements yet.

> If there is interest in this, I also suggest having a n-range version of
> map that takes a n-ary function and maps them in parallel.
>
> auto m = map!"a < b ? a : c"(range1, range2, range3);
>
> OK, I definitely need to take some time to assemble all this and propose
> it for a formal Phobos review. I'll bump it up higher in my TODO list.
>
> Philippe

Yah, definitely. Note that I have a functioning Zip in my upcoming 
commit to std.range.


Andrei


More information about the Digitalmars-d mailing list