Summary: zipWith
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos

--- Comment #0 from 2012-09-23 15:52:06 PDT ---
I suggest to add the higher order function "zipWith" to Phobos, similar to the
Haskell function present in the Prelude:

Prelude> zipWith (+) [1,2,3] [10,20,30]
Prelude> data Vec = Vec Int Int deriving (Show)
Prelude> zipWith Vec [1,2,3] [10,20,30]
[Vec 1 10,Vec 2 20,Vec 3 30]

As you see it's handy in two use cases: the first case is a zip that applies a
given function to the pairs. The other important use case is when you don't
need zip() to build an iterable of generic tuples, but you want it to build a
range of a specific given struct/tuple.

The Haskell code in D using the current Phobos:

import std.algorithm, std.range;
void main() {
    auto r1 = zip([1,2,3], [10,20,30]).map!(p => p[0] + p[1])();
    static struct Vec { int x, y; }
    auto r2 = zip([1,2,3], [10,20,30]).map!(p => Vec(p.tupleof))();

With a zipWith() the code becomes simpler and more readable:

import std.range;
void main() {
    auto r1 = zipWith!q{a + b}([1,2,3], [10,20,30]);
    static struct Vec { int x, y; }
    auto r2 = zipWith!Vec([1,2,3], [10,20,30]);

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

Reply via email to