Summary: Handy mapping with n-adic functions
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos

--- Comment #0 from 2011-05-18 18:20:16 PDT ---
A very common operation is to map a n-adic function (like a triadic one) on n
iterables. To do it in Haskell you use zipWith, example:

Prelude> let arr1 = [1, 2, 3]
Prelude> let arr2 = [10, 20, 30]
Prelude> let arr3 = [100, 200, 300]
Prelude> let foo x y z = x + y * z
Prelude> zipWith3 foo arr1 arr2 arr3

In Python2.6 the normal map function is enough, it optionally accepts more than
one iterable (in Python3 map does the same, but it's lazy):

def foo(x, y, z):
    return x + y * z

arr1 = [1, 2, 3]
arr2 = [10, 20, 30]
arr3 = [100, 200, 300]
print map(foo, arr1, arr2, arr3)

But in D2+Phobos you need to write awkward code:

import std.stdio, std.algorithm, std.range;

int foo(int x, int y, int z) {
    return x + y * z;

void main() {
    auto arr1 = [1, 2, 3];
    auto arr2 = [10, 20, 30];
    auto arr3 = [100, 200, 300];
    auto r = map!((t){ return foo(t.tupleof); })(zip(arr1, arr2, arr3));

[1001, 4002, 9003]

Unlike Python currently is designed to optionally accept
more than one function, and return a tuple. It's sometimes useful to do this,
but in my experience the semantics of the Python map is useful more often. So
I'd like the semantics of D map to become similar to the semantics of the
Python3 map. In this case the D code becomes something like:

auto r = map!foo(arr1, arr2, arr3);

If this is not possible, or not desired (or it leads to too much complex Phobos
code), then I suggest to introduce a new function in std.algorithm (or even in
std.range) that acts like the Haskell zipWith. In this case the D code becomes
something like:

auto r = zipWith!foo(arr1, arr2, arr3);

Configure issuemail:
------- You are receiving this mail because: -------

Reply via email to