From a993288f594f9a2bf697320394783ae939bbee7b Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Thu, 7 Jul 2016 10:44:50 -0400 Subject: [PATCH 1/2] broadcasting over scalars should produce a scalar --- base/abstractarray.jl | 1 + base/broadcast.jl | 5 +++++ test/abstractarray.jl | 4 ++++ test/broadcast.jl | 6 +++++- 4 files changed, 15 insertions(+), 1 deletion(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index fec3620e2a2bb..6d9c7937ac4b5 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -1504,6 +1504,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) diff --git a/base/broadcast.jl b/base/broadcast.jl index ab8fee3c97753..30aeafe7ee19a 100644 --- a/base/broadcast.jl +++ b/base/broadcast.jl @@ -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() = () diff --git a/test/abstractarray.jl b/test/abstractarray.jl index 438e7b70e0274..c810704a018a3 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -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) diff --git a/test/broadcast.jl b/test/broadcast.jl index 0b43510974ae7..302960ec298e1 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -197,6 +197,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}}) @@ -211,5 +215,5 @@ 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 From 33f36b4d0d6aeff6bcc1be0452402853b7fe662b Mon Sep 17 00:00:00 2001 From: "Steven G. Johnson" Date: Thu, 7 Jul 2016 14:32:53 -0400 Subject: [PATCH 2/2] check types as well as values --- test/abstractarray.jl | 4 ++-- test/broadcast.jl | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/test/abstractarray.jl b/test/abstractarray.jl index c810704a018a3..a686be2af4175 100644 --- a/test/abstractarray.jl +++ b/test/abstractarray.jl @@ -551,8 +551,8 @@ end @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 +@test map(sin, 1) === sin(1) +@test map(()->1234) === 1234 function test_UInt_indexing(::Type{TestAbstractArray}) A = [1:100...] diff --git a/test/broadcast.jl b/test/broadcast.jl index 302960ec298e1..21d5f0f72c4ec 100644 --- a/test/broadcast.jl +++ b/test/broadcast.jl @@ -198,8 +198,8 @@ let a = broadcast(Float32, [3, 4, 5]) end # broadcasting scalars: -@test sin.(1) == broadcast(sin, 1) == sin(1) -@test (()->1234).() == broadcast(()->1234) == 1234 +@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}})