Chaining std.algorithm functions

Brad Anderson eco at gnuk.net
Thu Apr 12 10:04:56 PDT 2012


On Thursday, 12 April 2012 at 17:00:37 UTC, DH wrote:
> Hi. I was wondering why the following works:
>
>     filter!(a => a % 2 == 0)(map!(a => a + 1)([1,2,3,4,5]))
>
> but the following does not:
>
>     [1,2,3,4,5]
>         .map!(a => a + 1)()
>         .filter!(a => a % 2 == 0)()
>
> ...giving me the error `Error: no property 'filter' for type 
> 'Result'`
>
> The dot syntax works fine if I'm just doing one 
> map/filter/etc., though. Is
> there a better way to do chaining like this? Am I 
> misunderstanding how the dot
> syntax sugar works or how map/filter/etc work in D? I'd prefer 
> not to use the
> first style as it can get a bit unwieldy...
>
> Thanks for your time!
> ~DH

It works for the first argument because UFCS (that is, calling 
free functions as members) works for arrays but not for other 
types (ranges in this case) in DMD <=2.058.  Thanks to the 
intrepid Kenji it works for all types in the upcoming 2.059. You 
can try out the 2.059 beta here: 
http://ftp.digitalmars.com/dmd2beta.zip  Your second example 
should work fine in it.

Regards,
Brad Anderson


More information about the Digitalmars-d-learn mailing list