next_permutation and cartesian product for ranges?

Timon Gehr timon.gehr at gmx.ch
Tue Oct 9 17:06:42 PDT 2012


On 10/10/2012 01:13 AM, H. S. Teoh wrote:
> On Tue, Oct 09, 2012 at 11:24:27PM +0200, Timon Gehr wrote:
> [...]
>> That's cute. =)
>> flatten is in std.algorithm and is called joiner.
>
> Ahahahaha... I knew joiner existed; why didn't I think of it. :P
>
>
>> Also, you need to be careful with index types.
>> I'd suggest:
>>
>> import std.algorithm, std.range;
>>
>> auto cartesianProd(R1,R2)(R1 range1, R2 range2)
>> if (isForwardRange!R1 && isInfinite!R1 &&
>>      isBidirectionalRange!(typeof(take(range2,1))) && isInfinite!R2)
>> {
>> 	return sequence!"n"(cast(size_t)1).map!(
>> 		(size_t n)=>range1.save.take(n) // need to specify param type. DMD bug.
>> 		.zip(range2.save.take(n).retro),
>> 	).joiner;
>> }
> [...]
>
> Ack! UFCS overload! ;-) (just kidding... it's actually a lot easier to
> read with UFCS.)
>

I usually use standard function call notation for zip. (but the mail
client wraps around the code, so I inserted a manual line break.)




More information about the Digitalmars-d mailing list