diff --git a/base/reduce.jl b/base/reduce.jl index 78a7ee3ccf5e67..e498ff966ecf3c 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -579,11 +579,12 @@ true ``` """ any(f::Any, itr) = any(Predicate(f), itr) -any(f::Predicate, itr) = mapreduce_sc_impl(f, |, itr) -any(f::typeof(identity), itr) = - eltype(itr) <: Bool ? - mapreduce_sc_impl(f, |, itr) : - reduce(or_bool_only, false, itr) +function any(f::Predicate, itr) + for x in itr + f(x) && return true + end + return false +end """ all(p, itr) -> Bool @@ -596,11 +597,12 @@ true ``` """ all(f::Any, itr) = all(Predicate(f), itr) -all(f::Predicate, itr) = mapreduce_sc_impl(f, &, itr) -all(f::typeof(identity), itr) = - eltype(itr) <: Bool ? - mapreduce_sc_impl(f, &, itr) : - reduce(and_bool_only, true, itr) +function all(f::Predicate, itr) + for x in itr + f(x) || return false + end + return true +end ## in & contains diff --git a/test/reduce.jl b/test/reduce.jl index 207e57684a95a0..603b07db6f5b71 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -224,6 +224,15 @@ let c = [0, 0], A = 1:1000 @test c == [10,10] end +# 19151 - always short circuit +let c = Int[], d = Int[], A = 1:9 + all((push!(c, x); x < 5) for x in A) + @test c == collect(1:5) + + any((push!(d, x); x > 4) for x in A) + @test d == collect(1:5) +end + # any and all with functors immutable SomeFunctor end