From cd76a76d1c4888dfe0c3927e1a4ddea00a1cd681 Mon Sep 17 00:00:00 2001 From: Neven Sajko Date: Fri, 6 Mar 2026 09:52:30 +0100 Subject: [PATCH] make `takewhile` an alias for `Iterators.takewhile` `Iterators.takewhile` is available since Julia v1.4. Thus it is available on all versions of Julia supported by IterTools (v1.8 and up). Added into Julia with PR JuliaLang/julia#33437. --- src/IterTools.jl | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/src/IterTools.jl b/src/IterTools.jl index 30c03d4..5a9db02 100644 --- a/src/IterTools.jl +++ b/src/IterTools.jl @@ -1,6 +1,7 @@ module IterTools import Base.Iterators: drop, take +using Base.Iterators: takewhile import Base: iterate, eltype, length, size, peek import Base: tail @@ -919,13 +920,6 @@ function iterate(ff::FlagFirst, state = (true, )) (isfirst, elt), (false, nextstate) end -# TakeWhile iterates through values from an iterable as long as a given predicate is true. - -struct TakeWhile{I} - cond::Function - xs::I -end - """ takewhile(cond, xs) @@ -940,17 +934,7 @@ julia> collect(takewhile(x-> x^2 < 10, 1:100)) 3 ``` """ -takewhile(cond, xs) = TakeWhile(cond, xs) - -function Base.iterate(it::TakeWhile, state=nothing) - (val, state) = @ifsomething (state === nothing ? iterate(it.xs) : iterate(it.xs, state)) - it.cond(val) || return nothing - val, state -end - -eltype(::Type{TakeWhile{I}}) where {I} = eltype(I) -IteratorEltype(::Type{TakeWhile{I}}) where {I} = IteratorEltype(I) -IteratorSize(::Type{<:TakeWhile}) = Base.SizeUnknown() +takewhile(cond, xs) # Properties