Skip to content

Commit b04c0fc

Browse files
committed
Canonicalize Drop{Take} to Take{Drop}
1 parent cb3a94c commit b04c0fc

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

base/iterator.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,7 @@ julia> collect(drop(a,4))
392392
```
393393
"""
394394
drop(xs, n::Int) = Drop(xs, n)
395+
drop(xs::Take, n::Int) = Take(drop(xs.xs, n), max(0, xs.n - n))
395396
drop(xs::Drop, n::Int) = Drop(xs.xs, n + xs.n)
396397

397398
eltype{I}(::Type{Drop{I}}) = eltype(I)

test/functional.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,18 @@ end
156156
@test_throws MethodError length(drop(countfrom(1), 3))
157157

158158
# double take
159+
# and take/drop canonicalization
159160
# -----------
160161

161162
for xs in Any["abc", [1, 2, 3]]
162-
@test take(take(xs, 2), 3) == take(xs, 2)
163-
@test take(take(xs, 4), 2) == take(xs, 2)
164-
@test drop(drop(xs, 1), 1) == drop(xs, 2)
163+
@test take(take(xs, 2), 3) === take(xs, 2)
164+
@test take(take(xs, 4), 2) === take(xs, 2)
165+
@test drop(drop(xs, 1), 1) === drop(xs, 2)
166+
@test take(drop(xs, 1), 1) === drop(take(xs, 2), 1)
167+
@test take(drop(xs, 3), 0) === drop(take(xs, 2), 3)
165168
@test isempty(drop(drop(xs, 2), 2))
169+
@test drop(take(drop(xs, 1), 2), 1) === take(drop(xs, 2), 1)
170+
@test take(drop(take(xs, 3), 1), 1) === take(drop(xs, 1), 1)
166171
end
167172

168173
# cycle

0 commit comments

Comments
 (0)