what is the offical way to handle multiple list in map() ?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Feb 17 11:49:24 PST 2015


On 02/16/2015 01:51 AM, Baz wrote:

 > For each language there is a column about handing multiple
 > list, i thought it could be a good idea to see how D handle
 > this:

I've updated the page with my understanding:

   http://en.wikipedia.org/wiki/Map_(higher-order_function)

I think they mean walking the lists in sync:

import std.stdio;
import std.algorithm;
import std.typecons;
import std.range;

int func(int a)
{
     return a * 2;
}

auto func(Tuple!(int, int) t)
{
     return t[0] + t[1];
}

void main()
{
     {
         auto list = [ 1, 10, 100 ];
         auto result = list.map!func;

         writeln(result);
     }

     {
         auto list1 = [ 1, 10, 100 ];
         auto list2 = [ 2, 20, 200 ];
         auto result = zip(list1, list2).map!func;  // <-- HERE

         writeln(result);
     }
}

Ali



More information about the Digitalmars-d-learn mailing list