Skip to content

Commit

Permalink
Make any/all capable of receiving functors.
Browse files Browse the repository at this point in the history
  • Loading branch information
fcard committed Jul 29, 2015
1 parent abd70e0 commit 8cdb199
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions base/functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ call(f::UnspecializedFun{2}, x, y) = f.f(x,y)

# Special purpose functors

type Predicate <: Func{1}
f::Function
type Predicate{F} <: Func{1}
f::F
end
call(pred::Predicate, x) = pred.f(x)::Bool

Expand Down
12 changes: 6 additions & 6 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -357,16 +357,16 @@ function identity end
any(itr) = any(IdFun(), itr)
all(itr) = all(IdFun(), itr)

any(f::Function, itr) = any(f === identity? IdFun() : Predicate(f), itr)
any(f::Func{1}, itr) = mapreduce_sc_impl(f, OrFun(), itr)
any(f::IdFun, itr) =
any(f::Any, itr) = any(f === identity? IdFun() : Predicate(f), itr)
any(f::Predicate, itr) = mapreduce_sc_impl(f, OrFun(), itr)
any(f::IdFun, itr) =
eltype(itr) <: Bool?
mapreduce_sc_impl(f, OrFun(), itr) :
nonboolean_any(itr)

all(f::Function, itr) = all(f === identity? IdFun() : Predicate(f), itr)
all(f::Func{1}, itr) = mapreduce_sc_impl(f, AndFun(), itr)
all(f::IdFun, itr) =
all(f::Any, itr) = all(f === identity? IdFun() : Predicate(f), itr)
all(f::Predicate, itr) = mapreduce_sc_impl(f, AndFun(), itr)
all(f::IdFun, itr) =
eltype(itr) <: Bool?
mapreduce_sc_impl(f, AndFun(), itr) :
nonboolean_all(itr)
Expand Down
9 changes: 9 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,15 @@ let c = [0, 0], A = 1:1000
@test c == [10,10]
end

# any and all with functors

immutable SomeFunctor end
Base.call(::SomeFunctor, x) = true

@test any(SomeFunctor(), 1:10)
@test all(SomeFunctor(), 1:10)


# in

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

0 comments on commit 8cdb199

Please sign in to comment.