Non-collecting iteration (one-liner for)

proposal

#1

Looking at source code in Base I notice a lot of one liners that look like this:

for i in 1:n; do_something(i) end

and a rarer

for ignored_index in 1:n; do_this_n_times() end

A simple grep 'for.*end' **/*.jl gives 150 matches on current master. In 39 cases, there is a ; before the end. I wonder if it would make sense to have a one-liner syntax equivalent to for. Perhaps analogously to a comprehension syntax, eg

Void[do_this_and_dont_collect(i) for i in 1:n]

Similarly, there is no equivalent of COMMON-LISP:MAPC for more functional style.

I found this older, somewhat related issue:


#2

We have foreach since 0.5 for this purpose.


#3

@nalimilan: good point, I haven’t thought of that. So disregard the part about mapc, but an iterator-based version would be nice.


#4

You can use generators for this:

julia> iterate(itr) = (for x in itr; end)
iterate (generic function with 1 method)

julia> iterate(println(x^2) for x in 1:10)
1
4
9
16
25
36
49
64
81
100