Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: add foreach function #13774

Merged
merged 2 commits into from
Oct 26, 2015
Merged

RFC: add foreach function #13774

merged 2 commits into from
Oct 26, 2015

Conversation

JeffBezanson
Copy link
Member

A few times I have seen map used with a function called only for side effects, for example map(println, x). This returns a noisy and unnecessary array of nothings. I think we should have a foreach function (ala Scheme) for cases like this.

@Keno
Copy link
Member

Keno commented Oct 26, 2015

+1. I frequently define this function locally

@StefanKarpinski
Copy link
Member

Yeah, seems good to me too.

@StefanKarpinski
Copy link
Member

Should it be called foreach!? Technically no, I guess...

@tbreloff
Copy link

+1. I think it should be foreach, not foreach!. I normally define
this myself.

Related... are there methods to map many functions to one input? Should
there be? I frequently define a mapf(funcs, val) = [f(val) for f in funcs]... a no-return-value version would be nice as well.

On Mon, Oct 26, 2015 at 10:54 AM, Stefan Karpinski <notifications@github.com

wrote:

Should it be called foreach!? Technically no, I guess...


Reply to this email directly or view it on GitHub
#13774 (comment).

@jakebolewski
Copy link
Member

+1, for the doc maybe a note when you should use foreach over map?

@malmaud
Copy link
Contributor

malmaud commented Oct 26, 2015

+1, I would use this. But it does mean that map, list comprehensions, for loops, and now foreach are all constructs for running a function on each element of an array.

@JeffBezanson
Copy link
Member Author

Those syntax differences seem to matter a lot to people. foreach(println, x) is much shorter than for y in x; println(y); end. In any case, in functional programming everything is just another way of running functions on things, so this doesn't worry me.

@malmaud
Copy link
Contributor

malmaud commented Oct 26, 2015

Agreed all around.

JeffBezanson added a commit that referenced this pull request Oct 26, 2015
@JeffBezanson JeffBezanson merged commit 4b2bc22 into master Oct 26, 2015
@tkelman tkelman deleted the jb/foreach branch October 26, 2015 19:56
@sbromberger
Copy link
Contributor

I'm glad to see foreach but the order of the arguments is not intuitive to me. That is, I parse this (internally) as "for each (item in) X, apply (function) F". Having the function first seems awkward.

@tkelman
Copy link
Contributor

tkelman commented Nov 2, 2015

Function first is consistent with map and allows do block notation.

@sbromberger
Copy link
Contributor

The do block notation is a compelling reason to have it (if do blocks require function first in order to work), but consistency with map isn't. Map reads "map this function to these items".

@johnmyleswhite
Copy link
Member

I don't think debating the subjectively best order of arguments is likely to prove productive.

@sbromberger
Copy link
Contributor

@johnmyleswhite you're right. Sorry for following up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants