www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Chaining std.algorithm functions

reply "DH" <arcticuno gmail.com> writes:
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
Apr 12 2012
next sibling parent "Brad Anderson" <eco gnuk.net> writes:
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
Apr 12 2012
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Thu, Apr 12, 2012 at 07:00:35PM +0200, 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...
[...] The second syntax requires dmd 2.059, which is currently on beta. Earlier versions of dmd do not support this syntax. T -- Two wrongs don't make a right; but three rights do make a left...
Apr 12 2012