-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
add method for foreach that takes no function #45152
Comments
Just found this, related, comment: #19198 (comment) |
Another idea, that I like julia> (g::Base.Generator)(f=identity) = foreach(f, g);
julia> (println(x) for x in 1:3)()
1
2
3 But, that's a bit weird. There is also the question of whether/how to extend this, or the original idea for |
The most stylish way to do this is I do think it would be neat if |
I think I kinda like discard(v[i] = w[i] + 1 for i in eachindex(w)) If you are thinking about the argument as an iterable, then it's pretty clear what this does. But, if you are a casual user, you might think, "it does that thing I want just as I wrote, but they told me I have to write 'discard', too. What's that about? Does it throw away
Another possibility is |
We export Splat now |
People (in general) are familiar and comfortable with comprehensions. For example
[f(k, v) for (k, v) in dict]
.But, if I only want to call
f(k, v)
for side effects, using a comprehension is slow/wasteful. It would be nice if there were a way to write something as close as possible to a comprehension, but that does not store results.I can do this.
Or this
foreach(((k,v),) -> f(k, v), dict)
. The latter is OK, but I avoid it; for example when sharing code with people new to Julia. (although using a trailing comma to mean "create a tuple" is also known in Python.)So far, the best solution I see is this
This would work with the following method.
Arguments in favor:
foreach(f(k, v) for (k, v) in dict)
does. (Maybe a few would expect this to be equivalent to a comprehension)foreach
. It fits very well withforeach
's mission.Base
. It adds very little complexity.foreach(::Generator)
should mean.Some people don't like
foreach
. There are reasonable arguments against it. So if your goal is to move towards deprecation and eventually removingforeach
(maybe in v2) then extending its use would not be attractive.For me, it's not
foreach
that is so attractive iteself. I'm just looking for a way to write it that is as close to comprehension/generator syntax as possible. This means how can you most simply iterate over a generator while discarding values ?The text was updated successfully, but these errors were encountered: