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

Timothee Cour via Digitalmars-d digitalmars-d at puremagic.com
Mon Mar 21 04:50:06 PDT 2016


On Mon, Mar 21, 2016 at 4:34 AM, Nick Treleaven via Digitalmars-d <
digitalmars-d at puremagic.com> wrote:

> 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]]));
>
>
>

see my proposal [+implementation] for emit
http://forum.dlang.org/post/mailman.538.1458560190.26339.digitalmars-d@puremagic.com


emit is more powerfull, and generalizes map,filter,joiner

auto res = src.mapFilter!(e=>nullFilter(cast(T)e));
with emit:
auto res = src.emit!((put,e){if(cast(T)e) put(e);});
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20160321/b1229b61/attachment.html>


More information about the Digitalmars-d mailing list