std.algorithm issue

Dee Girl deegirl at noreply.com
Wed May 21 20:12:49 PDT 2008


bearophile Wrote:

> Brian Myers:
> > Is it possible to map! or reduce! a member function or delegate against a
> > range? If so how would I do that?
> 
> Do you mean something like this?
> 
> import d.func: map, xrange, putr;
> 
> class Foo {
>     int sqr(int x) { return x * x; }
> }
> 
> class Baz {
>     float div2(int x) { return x / 2; }
> }
> 
> void main() {
>     auto f1 = new Foo;
>     putr( map(&f1.sqr, xrange(10)) );
> 
>     auto b1 = new Baz;
>     putr( map(&b1.div2, xrange(10)) );
> }
> 
> Output:
> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
> [0.0, 0.0, 1.0, 1.0, 2.0, 2.0, 3.0, 3.0, 4.0, 4.0]
> 
> Bye,
> bearophile

import std.algorithm;

struct A
{
    int Merge(int) { return 0; }
}

void main(string[] args)
{
    A a;
    map!((int x) { return a.Merge(x); })([1, 2, 3]);
}

I did not install bearophile library. It would be interesting to see what the speed is of std.algorithm and d.func. Dee Girl



More information about the Digitalmars-d mailing list