How to implement filterMap

Christian Köstlin christian.koestlin at gmail.com
Sat Dec 30 21:07:57 UTC 2023


On Saturday, 30 December 2023 at 18:08:55 UTC, Alexandru Ermicioi 
wrote:
> On Friday, 29 December 2023 at 23:10:47 UTC, Christian Köstlin 
> wrote:
>> Is there a way to implement filterMap (meaning do mapping of a 
>> range, but if something happens during the map, leave this 
>> element out of the resulting range).
>
> Perhaps try map to null cases that exception is thrown then 
> filter out those, and then unwrap from nullable type:
> ```d
> myRange.map!(e => e.to!T.asNullForException).filter!(e => 
> !e.isNull).map!(e => e.get).myOtherAlgorithm()
> ```
>
> where asNullForException will return empty `Nullable` in case 
> conversion fails.

asNullForException could be nicely implemented with lazy 
parameters i guess.
then its back to the map/filter/map chain that motivated 
filter_map in rust ...
thanks for this idea...

kind regards,
Christian



More information about the Digitalmars-d-learn mailing list