Julia: map! with general iterators after first two arguments

Created on 30 Jul 2017  路  4Comments  路  Source: JuliaLang/julia

In contrast to map, map! doesn't seem to support the direct usage of partition iterators.
While the following piece of code works as expected

a = rand(2, 2)
it = Iterators.partition(eachindex(a), 2)
b = map(x -> a[x], it)

(b is an 2-element Array{Array{Float64,1},1}),

this variation

a = rand(2, 2)
it = Iterators.partition(eachindex(a), 2)
b = [zeros(2), zeros(2)]
map!(x -> a[x], b, it)

fails with the method error

ERROR: MethodError: no method matching map!(::##49#50, ::Array{Array{Float64,1},1}, ::Base.Iterators.PartitionIterator{Base.OneTo{Int64}})
Closest candidates are:
  map!(::F, ::AbstractArray) where F at deprecated.jl:56
  map!(::F, ::AbstractArray, ::AbstractArray) where F at abstractarray.jl:1858
  map!(::F, ::AbstractArray, ::AbstractArray, ::AbstractArray) where F at abstractarray.jl:1892

I'm running Julia Version 0.6.0, Commit 903644385b* (2017-06-19 13:05 UTC). Operating systems is Windows 10.

Most helpful comment

That's not specific of the partition iterator, but for any iterable that is not an AbstractArray. I don't see why the inputs to map! would have to be restricted to AbstractArray. That is

map!(f::F, dest::AbstractArray, As...) where {F} = map!(f, dest, collect.(As)...)

seems to be semantically ok, but of course calls for a more efficient implementation.

All 4 comments

That's not specific of the partition iterator, but for any iterable that is not an AbstractArray. I don't see why the inputs to map! would have to be restricted to AbstractArray. That is

map!(f::F, dest::AbstractArray, As...) where {F} = map!(f, dest, collect.(As)...)

seems to be semantically ok, but of course calls for a more efficient implementation.

It's unclear to me that assigning the return value to the first array argument is the obvious thing to do. Wouldn't having the function return a tuple and assigning back to all the arguments be just as natural?

You could, but traditionally multi-argument map passes multiple arguments to f but still takes only one output.

Fair enough. Carry on then.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omus picture omus  路  3Comments

TotalVerb picture TotalVerb  路  3Comments

helgee picture helgee  路  3Comments

wilburtownsend picture wilburtownsend  路  3Comments

sbromberger picture sbromberger  路  3Comments