Bug 12409 – Add "each" function as found in Ruby and jQuery

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-03-18T23:13:00Z
Last change time
2015-02-18T03:42:14Z
Keywords
pull
Assigned to
nobody
Creator
dlang-bugzilla

Comments

Comment #0 by dlang-bugzilla — 2014-03-18T23:13:51Z
"each" is like "map", but iterates over the range eagerly, and returns void.
Comment #1 by dlang-bugzilla — 2014-03-18T23:34:51Z
Comment #2 by dlang-bugzilla — 2014-03-18T23:36:10Z
Comment #3 by lt.infiltrator — 2014-03-18T23:37:10Z
What's the difference between this and using foreach, though?
Comment #4 by dlang-bugzilla — 2014-03-18T23:38:59Z
You can put it at the end of UFCS chains. See e.g. the example included with the pull. It is essentially a small bit of sugar, but seeing how it's present in other languages, I think it makes sense to add.
Comment #5 by dlang-bugzilla — 2014-03-18T23:39:58Z
Oh, and foreach doesn't have "auto ref", does it? It's one reason to use fun(r.front) + popFront instead of foreach.
Comment #6 by github-bugzilla — 2015-01-11T14:02:57Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/bf14b8bd44d61d1136ec6ba64bd3f032704a1526 fix Issue 12409 - Add "each" function as found in Ruby and jQuery https://github.com/D-Programming-Language/phobos/commit/f3604ad2e27873146e227038c05fa51b0c2fa6bd Merge pull request #2024 from CyberShadow/fix12409 fix Issue 12409 - Add "each" function as found in Ruby and jQuery
Comment #7 by bearophile_hugs — 2015-01-11T14:54:28Z
Reopened because I think the current design of "each" is too much bug-prone: void main() { import std.stdio, std.algorithm; int[10] a; a.each!((ref x) => x++); a.writeln; a[].each!((ref x, ref i) => x = i); a.writeln; a[].each!((ref x, int i) => x = i); a.writeln; a[].each!q{ a = i }; a.writeln; } [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 0, 0, 0, 0, 0, 0, 0, 0, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Most Phobos functions don't accept fixed-size arrays. "each" accepts them, but takes them by value... And "each" accepts functions with a second "i" argument (and offers the index "i" as in the last example), but it ignores it. This is a mess that will cause many bugs. I suggest to tighten the semantics: - For uniformity with Phobos it's better to not accept a fixed-size array. If you want "each" to accept them, then take them by reference only. - How do you use the "i" index with a lambda? - Wrong functions probably should give a compile-time error.
Comment #8 by github-bugzilla — 2015-02-18T03:40:58Z