D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 11572 - eager apply for ranges
Summary: eager apply for ranges
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-11-21 12:43 UTC by Martin Nowak
Modified: 2020-03-21 03:56 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Martin Nowak 2013-11-21 12:43:30 UTC
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 bearophile_hugs 2013-11-21 13:30:11 UTC
(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 Dmitry Olshansky 2013-11-21 13:37:11 UTC
(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 Martin Nowak 2013-11-21 20:36:37 UTC
(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 Justin Whear 2014-06-06 17:26:36 UTC
Pull request & discussion: https://github.com/D-Programming-Language/phobos/pull/2024
Comment 5 basile-z 2015-11-21 14:07:08 UTC
solved since each() implemented 

https://github.com/D-Programming-Language/phobos/pull/2024