Skip to content

Commit 084585b

Browse files
committed
define axes1(::Broadcasted)
1
1 parent c04a3fd commit 084585b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

base/broadcast.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ _axes(bc::Broadcasted{<:AbstractArrayStyle{0}}, ::Nothing) = ()
226226

227227
@inline Base.axes(bc::Broadcasted{<:Any, <:NTuple{N}}, d::Integer) where N =
228228
d <= N ? axes(bc)[d] : OneTo(1)
229+
@inline Base.axes(bc::Broadcasted{<:Any, Nothing}, d::Integer) =
230+
(ax = axes(bc); d <= length(ax) ? ax[d] : OneTo(1))
231+
232+
Base.axes1(bc::Broadcasted) = axes(bc, 1)
233+
Base.axes1(bc::Broadcasted{<:AbstractArrayStyle{0}}) = OneTo(1)
229234

230235
BroadcastStyle(::Type{<:Broadcasted{Style}}) where {Style} = Style()
231236
BroadcastStyle(::Type{<:Broadcasted{S}}) where {S<:Union{Nothing,Unknown}} =

test/broadcast.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,3 +1079,15 @@ end
10791079
y = randn(2)
10801080
@inferred(test(x, y)) == [0, 0]
10811081
end
1082+
1083+
@testset "axes1 and axes" begin
1084+
bc = Base.broadcasted(+, reshape(1:6,3,:), 1)
1085+
@test Base.axes(bc) == (Base.OneTo(3),Base.OneTo(2))
1086+
@test Base.axes1(bc) == axes(bc, 1) == Base.OneTo(3)
1087+
bc = Broadcast.instantiate(bc)
1088+
@test Base.axes(bc) == (Base.OneTo(3),Base.OneTo(2))
1089+
@test Base.axes1(bc) == axes(bc, 1) == Base.OneTo(3)
1090+
bc = Base.broadcasted(+, fill(1), 1)
1091+
@test Base.axes(bc) == ()
1092+
@test Base.axes1(bc) == axes(bc, 1) == Base.OneTo(1)
1093+
end

0 commit comments

Comments
 (0)