Infinite range of nullable elements

Roland Hadinger via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 17 06:38:53 PDT 2015


On Friday, 17 July 2015 at 12:44:57 UTC, anonymous wrote:
> The building blocks are there. You're `map`ping the original 
> range to `Nullable`, and then you're `chain`ing an infinite 
> range (`cycle`) of nulls behind.
>
> ----
> import std.range: isInputRange;
>
> auto cushion(R)(R r)
>     if (isInputRange!R)
> {
>     import std.algorithm: map;
>     import std.range: chain, cycle, ElementType, only;
>     import std.typecons: Nullable;
>
>     alias E = ElementType!R;
>     alias NE = Nullable!E;
>
>     return chain(r.map!NE, NE().only.cycle);
> }
> ----

Nice! I didn't think of using 'chain'.



More information about the Digitalmars-d-learn mailing list