reduce(vcat, arr)
returns a scalar or an array depending on the length of the array.
julia> reduce(vcat, [1])
1
julia> reduce(vcat, [1,2])
2-element Array{Int64,1}:
1
2
julia> @code_warntype reduce(vcat, [1,2])
Variables
#self#::Core.Compiler.Const(reduce, false)
op::Core.Compiler.Const(vcat, false)
A::Array{Int64,1}
Body::Union{Int64, Array{Int64,1}}
1 ─ %1 = Core.NamedTuple()::Core.Compiler.Const(NamedTuple(), false)
│ %2 = Base.pairs(%1)::Core.Compiler.Const(Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}(), false)
│ %3 = Base.:(var"#reduce#622")(%2, #self#, op, A)::Union{Int64, Array{Int64,1}}
└── return %3
I find this surprising as vcat
does not behave like this:
julia> vcat(1)
1-element Array{Int64,1}:
1
julia> vcat(1,2)
2-element Array{Int64,1}:
1
2
The issue may be resolved by specifying an initial value, eg. as
julia> reduce(vcat, [1], init=Int[])
1-element Array{Int64,1}:
1
Why is the type-unstable definition the default?