Why Ruby?

Andrej Mitrovic andrej.mitrovich at gmail.com
Wed Dec 15 08:37:54 PST 2010


On 12/15/10, Stephan Soller <stephan.soller at helionweb.de> wrote:
>
> So, _if_ we can figure out a way to do some nice chaining of such calls
> we can get:
>
> 	[1, 2, 3, 4, 5].filter!((e){ return e > 3; })
> 		.map!((e){ return e*e; });
>
> Or if you don't like delegates and don't mind obvious compile time black
> magic:
>
> 	[1, 2, 3, 4, 5].filter!("a > 3").map!("a * a");
>

You might use pipe! as an alternative:

import std.algorithm;
import std.functional;
import std.stdio;

void main()
{
    int[] arr = [1, 2, 3, 4, 5];
    auto result = pipe!( filter!("a>3"), map!("a * a") )(arr);
    writeln(result);
}

But I can't seem to do the same with delegates:

import std.algorithm;
import std.functional;
import std.stdio;

void main()
{
    int[] arr = [1, 2, 3, 4, 5];
    pipe!( filter!( (e){ return e > 3; } ),
             map!( (e){ return e*e; } ) )(arr);
}

chain.d(9): Error: expression template filter(Range) is not a valid
template value argument
chain.d(9): Error: expression template map(Range) is not a valid template
value argument


More information about the Digitalmars-d mailing list