Philippe Sigaud <[email protected]> wrote:

That's fun to see, the syntax is cool. I never thought of using operator
overloading for this.

I just had a look at Wikipedia's article on it, found that the syntax was
somewhat approachable in D's operator overloading, and thought
'hey, that's neat'. Just wish I could use curly brackets around it, but
parentheses work:

{ 2·x | x ∈ N, x² > 3 }
=>
( list!"2*a" | iota(1,int.max) * where!"x^^2 > 3" )


I did a range comprehension also, a few months ago. It's called comp in
http://svn.dsource.org/projects/dranges/trunk/dranges/docs/algorithm2.html

Like yours, it takes any range and is lazy, the main difference it that it's multirange: you can give it any number of range as input, it will create the
combinations of elements (aka product of ranges), filter it with the
predicate and map the generative function on it.
[snip]
auto pyth(int n)
{
    return comp ! ("tuple(a,b,c)", "a*a+b*b==c*c && a<b")
(iota(1,n),iota(1,n), iota(1,n));
}

This reminded me that Phobos lacks a combinatorial range, taking two or
more (forward) ranges and giving all combinations thereof:

combine([0,1],[2,3])
=>
(0,2), (0,3), (1,2), (1,3)

Work has commenced on implementing just that.

--
Simen

Reply via email to