[Request] A way to extract all instance of X from a range

Nick Treleaven via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 21 04:34:15 PDT 2016


On 14/03/2016 11:32, thedeemon wrote:
>
> filter_map : ('a -> 'b option) -> 'a t -> 'b t
>
> "filter_map f e returns an enumeration over all elements x such as f y
> returns Some x, where y is an element of e."
>
> It is really convenient and comes handy in many situations. However it
> requires some common Option/Maybe type that different libraries could use.

There is a pull for Option:
https://github.com/D-Programming-Language/phobos/pull/3915

We could have:

// fun takes r.front and produces an Option of that type
auto mapFilter(alias fun, R)(R r);

// turn a possibly null value into an Option
Option!T nullFilter(T)(T v) if (isNullable!T);

auto src = [new Object(), new T(), null];
auto res = mapFilter!(e => nullFilter(cast(T)e));
assert(res.equal([src[1]]));




More information about the Digitalmars-d mailing list