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.