Skip to content

Commit

Permalink
Merge pull request #17318 from stevengj/bcast_scalar
Browse files Browse the repository at this point in the history
broadcasting over scalars should produce a scalar
  • Loading branch information
JeffBezanson authored Jul 11, 2016
2 parents 0572533 + 33f36b4 commit 0822970
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ end

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

map(f) = f()
map(f, iters...) = collect(Generator(f, iters...))

# multi-item push!, unshift! (built on top of type-specific 1-item version)
Expand Down
5 changes: 5 additions & 0 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export broadcast_getindex, broadcast_setindex!

## Broadcasting utilities ##

# fallback routines for broadcasting with no arguments or with scalars
# to just produce a scalar result:
broadcast(f) = f()
broadcast(f, x::Number...) = f(x...)

## Calculate the broadcast shape of the arguments, or error if incompatible
# array inputs
broadcast_shape() = ()
Expand Down
4 changes: 4 additions & 0 deletions test/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,10 @@ end
# issue #15689, mapping an abstract type
@test isa(map(Set, Array[[1,2],[3,4]]), Vector{Set{Int}})

# mapping over scalars and empty arguments:
@test map(sin, 1) === sin(1)
@test map(()->1234) === 1234

function test_UInt_indexing(::Type{TestAbstractArray})
A = [1:100...]
_A = Expr(:quote, A)
Expand Down
6 changes: 5 additions & 1 deletion test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ let a = broadcast(Float32, [3, 4, 5])
@test eltype(a) == Float32
end

# broadcasting scalars:
@test sin.(1) === broadcast(sin, 1) === sin(1)
@test (()->1234).() === broadcast(()->1234) === 1234

# issue #4883
@test isa(broadcast(tuple, [1 2 3], ["a", "b", "c"]), Matrix{Tuple{Int,String}})
@test isa(broadcast((x,y)->(x==1?1.0:x,y), [1 2 3], ["a", "b", "c"]), Matrix{Tuple{Real,String}})
Expand All @@ -210,7 +214,7 @@ end

# PR 16988
@test Base.promote_op(+, Bool) === Int
@test isa(broadcast(+, true), Array{Int,0})
@test isa(broadcast(+, [true]), Array{Int,1})
@test Base.promote_op(Float64, Bool) === Float64

# issue #17304
Expand Down

0 comments on commit 0822970

Please sign in to comment.