[Issue 8715] New: zipWith
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Sep 23 15:51:13 PDT 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8715
Summary: zipWith
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: enhancement
Priority: P2
Component: Phobos
AssignedTo: nobody at puremagic.com
ReportedBy: bearophile_hugs at eml.cc
--- Comment #0 from bearophile_hugs at eml.cc 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]
[11,22,33]
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: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list