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

Matthias Bentrup via Digitalmars-d digitalmars-d at puremagic.com
Tue Mar 22 13:09:51 PDT 2016


On Monday, 21 March 2016 at 11:50:06 UTC, Timothee Cour wrote:
> 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);});

Why don't you go for the well-established monadic bind function ?
A rangified bind would take a range of inputs, a lambda returning 
a range of results for one input and return a range of all 
results.

It is logically just a combination of map and concat (which turns 
a range of ranges into a combined range, but I think that one is 
missing in the std lib too).



More information about the Digitalmars-d mailing list