Reputation: 4482
In 1d, I can use either of these:
[i for i in 1:5]
or
map(1:5) do i
i
end
both produce
[1,2,3,4,5]
Is there a way to use map
in higher dimensions? e.g. to replicate
[x + y for x in 1:5,y in 10:13]
which produces
5×4 Array{Int64,2}:
11 12 13 14
12 13 14 15
13 14 15 16
14 15 16 17
15 16 17 18
Upvotes: 4
Views: 211
Reputation: 7674
This, of course, is not the map
equivalent that you are looking for, but in some cases like this you can use broadcasting with a vector and a transposed vector:
x = 1:5
y = (10:13)'
x .+ y
At the REPL:
julia> (1:5) .+ (10:13)'
5×4 Array{Int64,2}:
11 12 13 14
12 13 14 15
13 14 15 16
14 15 16 17
15 16 17 18
Upvotes: 5
Reputation: 2580
You can do this:
julia> map(Iterators.product(1:3, 10:15)) do (x,y)
x+y
end
3×6 Array{Int64,2}:
11 12 13 14 15 16
12 13 14 15 16 17
13 14 15 16 17 18
The comprehension you wrote is I think just collect(x+y for (x,y) in Iterators.product(1:5, 10:13))
, . Note the brackets (x,y)
, as the do
function gets a tuple. Unlike x,y
when it gets two arguments:
julia> map(1:3, 11:13) do x,y
x+y
end
3-element Array{Int64,1}:
12
14
16
Upvotes: 7