Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add takewhile, dropwhile to iterators #33437

Merged
merged 23 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ New library functions
* The `tempname` function now takes a `cleanup::Bool` keyword argument defaulting to `true`, which causes the process to try to ensure that any file or directory at the path returned by `tempname` is deleted upon process exit ([#33090]).
* The `readdir` function now takes a `join::Bool` keyword argument defaulting to `false`, which when set causes `readdir` to join its directory argument with each listed name ([#33113]).
* The new `only(x)` function returns the one-and-only element of a collection `x`, and throws an `ArgumentError` if `x` contains zero or multiple elements. ([#33129])
* `takewhile` and `dropwhile` have been added to the Iterators submodule ([#33437]).


Standard library changes
Expand Down
101 changes: 100 additions & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import .Base:
getindex, setindex!, get, iterate,
popfirst!, isdone, peek

export enumerate, zip, rest, countfrom, take, drop, cycle, repeated, product, flatten, partition
export enumerate, zip, rest, countfrom, take, drop, takewhile, dropwhile, cycle, repeated, product, flatten, partition

tail_if_any(::Tuple{}) = ()
tail_if_any(x::Tuple) = tail(x)
Expand Down Expand Up @@ -649,6 +649,105 @@ end
iterate(it::Drop, state) = iterate(it.xs, state)
isdone(it::Drop, state) = isdone(it.xs, state)


# takewhile

struct TakeWhile{I,P<:Function}
o314 marked this conversation as resolved.
Show resolved Hide resolved
pred::P
xs::I
o314 marked this conversation as resolved.
Show resolved Hide resolved
end

"""
takewhile(pred, iter)

An iterator that generates element from `iter` as long as predicate `pred` is true,
afterwards, drops every element.

!!! compat "Julia 1.4"
This function requires at least Julia 1.4.

# Examples

```jldoctest
julia> s = collect(1:5)
5-element Array{Int64,1}:
1
2
3
4
5

julia> collect(Iterators.takewhile(<(3),s))
2-element Array{Int64,1}:
1
2
```
"""
takewhile(pred,xs) = TakeWhile(pred,xs)

function iterate(ibl::TakeWhile, itr...)
y = iterate(ibl.xs,itr...)
y === nothing && return nothing
ibl.pred(y[1]) || return nothing
y
end

IteratorSize(::Type{<:TakeWhile}) = SizeUnknown()
eltype(::Type{TakeWhile{I,P}}) where {I,P} = eltype(I)
IteratorEltype(::Type{TakeWhile{I,P}}) where {I,P} = IteratorEltype(I)


# dropwhile

struct DropWhile{I,P<:Function}
pred::P
xs::I
end

"""
dropwhile(pred, iter)

An iterator that drops element from `iter` as long as predicate `pred` is true,
afterwards, returns every element.

!!! compat "Julia 1.4"
This function requires at least Julia 1.4.

# Examples

```jldoctest
julia> s = collect(1:5)
5-element Array{Int64,1}:
1
2
3
4
5

julia> collect(Iterators.dropwhile(<(3),s))
3-element Array{Int64,1}:
3
4
5
```
"""
dropwhile(pred,itr) = DropWhile(pred,itr)

iterate(ibl::DropWhile,itr) = iterate(ibl.xs, itr)
function iterate(ibl::DropWhile)
y = iterate(ibl.xs)
while y !== nothing
ibl.pred(y[1]) || break
y = iterate(ibl.xs,y[2])
end
y
end

IteratorSize(::Type{<:DropWhile}) = SizeUnknown()
eltype(::Type{DropWhile{I,P}}) where {I,P} = eltype(I)
o314 marked this conversation as resolved.
Show resolved Hide resolved
IteratorEltype(::Type{DropWhile{I,P}}) where {I,P} = IteratorEltype(I)


# Cycle an iterator forever

struct Cycle{I}
Expand Down
23 changes: 23 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,29 @@ for xs in Any["abc", [1, 2, 3]]
@test take(drop(take(xs, 3), 1), 1) === take(drop(xs, 1), 1)
end

# takewhile
o314 marked this conversation as resolved.
Show resolved Hide resolved
# --------
@testset begin
@test collect(takewhile(<(4),1:10)) == [1,2,3]
@test collect(takewhile(<(4),Iterators.countfrom(1))) == [1,2,3]
@test collect(takewhile(<(4),5:10)) == []
@test collect(takewhile(_->true,5:10)) == 5:10
@test collect(takewhile(isodd,[1,1,2,3])) == [1,1]
@test collect(takewhile(<(2), takewhile(<(3), [1,1,2,3]))) == [1,1]
end

# dropwhile
# --------
@testset begin
@test collect(dropwhile(<(4), 1:10)) == 4:10
@test collect(dropwhile(<(4), 1:10)) isa Vector{Int}
@test isempty(dropwhile(<(4), []))
@test collect(dropwhile(_->false,1:3)) == 1:3
@test isempty(dropwhile(_->true, 1:3))
@test collect(dropwhile(isodd,[1,1,2,3])) == [2,3]
@test collect(dropwhile(iseven,dropwhile(isodd,[1,1,2,3]))) == [3]
end

# cycle
# -----
let i = 0
Expand Down