[Issue 5550] std.range.enumerate()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 4 17:48:15 PST 2013


http://d.puremagic.com/issues/show_bug.cgi?id=5550



--- Comment #7 from bearophile_hugs at eml.cc 2013-03-04 17:48:13 PST ---
An alternative idea is to introduce the "imap" and "ifilter" functions, similar
to the "mapi" function of F# language:


import std.stdio: writeln;
import std.algorithm: imap;
void main() {
    "abcd".imap!(t => t).writeln;
}


Should print:

[Tuple!(uint, dchar)(0, 'a'), Tuple!(uint, dchar)(1, 'b'), Tuple!(uint,
dchar)(2, 'c'), Tuple!(uint, dchar)(3, 'd')]

This means that imap gives to the mapping function a tuple that contains the
index and the item. ifilter works similarly (unfortunately D doesn't yet have a
syntax for tuple unpacking in function signatures).

This is nice and handy, but enumerate() can be used in more situations. On the
other hand imap is a little shorter:

"abcd".imap!(t => t).writeln;

"abcd".enumerate.imap!(t => t).writeln;

In the end I prefer enumerate.

-- 
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