Skip to content

Commit

Permalink
Document map on multi-dimensional arrays (JuliaLang#40999)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed Jun 16, 2021
1 parent c593eeb commit c366be5
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2747,6 +2747,10 @@ end
concatenate_setindex!(R, v, I...) = (R[I...] .= (v,); R)
concatenate_setindex!(R, X::AbstractArray, I...) = (R[I...] = X)

## 0 arguments

map(f) = f()

## 1 argument

function map!(f::F, dest::AbstractArray, A::AbstractArray) where F
Expand All @@ -2763,7 +2767,6 @@ map(f, A::AbstractArray) = collect_similar(A, Generator(f,A))
mapany(f, A::AbstractArray) = map!(f, Vector{Any}(undef, length(A)), A)
mapany(f, itr) = Any[f(x) for x in itr]

# default to returning an Array for `map` on general iterators
"""
map(f, c...) -> collection
Expand All @@ -2787,7 +2790,7 @@ julia> map(+, [1, 2, 3], [10, 20, 30, 400, 5000])
33
```
"""
map(f, A) = collect(Generator(f,A))
map(f, A) = collect(Generator(f,A)) # default to returning an Array for `map` on general iterators

map(f, ::AbstractDict) = error("map is not defined on dictionaries")
map(f, ::AbstractSet) = error("map is not defined on sets")
Expand Down Expand Up @@ -2856,7 +2859,31 @@ function map!(f::F, dest::AbstractArray, As::AbstractArray...) where {F}
map_n!(f, dest, As)
end

map(f) = f()
"""
map(f, A::AbstractArray...) -> N-array
When acting on multi-dimensional arrays of the same [`ndims`](@ref),
they must all have the same [`axes`](@ref), and the answer will too.
See also [`broadcast`](@ref), which allows mismatched sizes.
# Examples
```
julia> map(//, [1 2; 3 4], [4 3; 2 1])
2×2 Matrix{Rational{$Int}}:
1//4 2//3
3//2 4//1
julia> map(+, [1 2; 3 4], zeros(2,1))
ERROR: DimensionMismatch
julia> map(+, [1 2; 3 4], [1,10,100,1000], zeros(3,1)) # iterates until 3rd is exhausted
3-element Vector{Float64}:
2.0
13.0
102.0
```
"""
map(f, iters...) = collect(Generator(f, iters...))

# multi-item push!, pushfirst! (built on top of type-specific 1-item version)
Expand Down

0 comments on commit c366be5

Please sign in to comment.