Bug 11572 – eager apply for ranges

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-21T12:43:30Z
Last change time
2020-03-21T03:56:36Z
Assigned to
No Owner
Creator
Martin Nowak

Comments

Comment #0 by code — 2013-11-21T12:43:30Z
The use-case foreach (c; "foobar".retro()) writeln(c); "foobar".retro.apply!writeln(); This is not a functional component because it's mostly useful for void functions with side effect. Currently one needs to mix foreach and UFCS. Not sure about the name, maybe sink or apply. Or we could modify std.algorithm.reduce to allow void functions. NB: The function is not supposed to produce a result (we have http://dlang.org/phobos/std_array.html#.array for that).
Comment #1 by bearophile_hugs — 2013-11-21T13:30:11Z
(In reply to comment #0) > The use-case > > foreach (c; "foobar".retro()) > writeln(c); > > "foobar".retro.apply!writeln(); This is a common need, perhaps such request is already in Bugzilla. > Not sure about the name, maybe sink or apply. "sink" is probably better left for output ranges, etc. "apply" function usually has another meaning: http://en.wikipedia.org/wiki/Apply So I suggest names like "forEach", that underlines its imperative nature. > Or we could modify std.algorithm.reduce to allow void functions. Reduce has a different meaning. Here you are not "reducing". > Currently one needs to mix foreach and UFCS.
Comment #2 by dmitry.olsh — 2013-11-21T13:37:11Z
(In reply to comment #1) > (In reply to comment #0) > > The use-case > > > > foreach (c; "foobar".retro()) > > writeln(c); > > > > "foobar".retro.apply!writeln(); > > This is a common need, perhaps such request is already in Bugzilla. > > > > Not sure about the name, maybe sink or apply. > > "sink" is probably better left for output ranges, etc. > > "apply" function usually has another meaning: > http://en.wikipedia.org/wiki/Apply > > So I suggest names like "forEach", that underlines its imperative nature. Simply each is enough and has precedents in other languages like Groovy and Ruby.
Comment #3 by code — 2013-11-21T20:36:37Z
(In reply to comment #2) > Simply each is enough and has precedents in other languages like Groovy and > Ruby. Yep, 'each' is good.
Comment #4 by justin — 2014-06-06T17:26:36Z
Comment #5 by b2.temp — 2015-11-21T14:07:08Z