Skip to content

Commit c8a6392

Browse files
fix: fix Base.length for empty ArrayPartition, add tests
1 parent 94f10bc commit c8a6392

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

src/array_partition.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ recursive_eltype(A::ArrayPartition) = recursive_eltype(first(A.x))
285285
Base.iterate(A::ArrayPartition) = iterate(Chain(A.x))
286286
Base.iterate(A::ArrayPartition, state) = iterate(Chain(A.x), state)
287287

288-
Base.length(A::ArrayPartition) = sum(broadcast(length, A.x))
288+
Base.length(A::ArrayPartition) = sum(broadcast(length, A.x); init = 0)
289289
Base.size(A::ArrayPartition) = (length(A),)
290290

291291
# redefine first and last to avoid slow and not type-stable indexing

test/partitions_test.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
using RecursiveArrayTools, Test, Statistics, ArrayInterface
2+
3+
@test length(ArrayPartition()) == 0
4+
@test isempty(ArrayPartition())
5+
26
A = (rand(5), rand(5))
37
p = ArrayPartition(A)
48
@inferred p[1]

0 commit comments

Comments
 (0)