Skip to content

Commit 8cdb199

Browse files
committed
Make any/all capable of receiving functors.
1 parent abd70e0 commit 8cdb199

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

base/functors.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ call(f::UnspecializedFun{2}, x, y) = f.f(x,y)
7878

7979
# Special purpose functors
8080

81-
type Predicate <: Func{1}
82-
f::Function
81+
type Predicate{F} <: Func{1}
82+
f::F
8383
end
8484
call(pred::Predicate, x) = pred.f(x)::Bool
8585

base/reduce.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,16 +357,16 @@ function identity end
357357
any(itr) = any(IdFun(), itr)
358358
all(itr) = all(IdFun(), itr)
359359

360-
any(f::Function, itr) = any(f === identity? IdFun() : Predicate(f), itr)
361-
any(f::Func{1}, itr) = mapreduce_sc_impl(f, OrFun(), itr)
362-
any(f::IdFun, itr) =
360+
any(f::Any, itr) = any(f === identity? IdFun() : Predicate(f), itr)
361+
any(f::Predicate, itr) = mapreduce_sc_impl(f, OrFun(), itr)
362+
any(f::IdFun, itr) =
363363
eltype(itr) <: Bool?
364364
mapreduce_sc_impl(f, OrFun(), itr) :
365365
nonboolean_any(itr)
366366

367-
all(f::Function, itr) = all(f === identity? IdFun() : Predicate(f), itr)
368-
all(f::Func{1}, itr) = mapreduce_sc_impl(f, AndFun(), itr)
369-
all(f::IdFun, itr) =
367+
all(f::Any, itr) = all(f === identity? IdFun() : Predicate(f), itr)
368+
all(f::Predicate, itr) = mapreduce_sc_impl(f, AndFun(), itr)
369+
all(f::IdFun, itr) =
370370
eltype(itr) <: Bool?
371371
mapreduce_sc_impl(f, AndFun(), itr) :
372372
nonboolean_all(itr)

test/reduce.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ let c = [0, 0], A = 1:1000
216216
@test c == [10,10]
217217
end
218218

219+
# any and all with functors
220+
221+
immutable SomeFunctor end
222+
Base.call(::SomeFunctor, x) = true
223+
224+
@test any(SomeFunctor(), 1:10)
225+
@test all(SomeFunctor(), 1:10)
226+
227+
219228
# in
220229

221230
@test in(1, Int[]) == false

0 commit comments

Comments
 (0)