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 functors SubFun, DivFun, and PowFun #12322

Merged
merged 3 commits into from
Jul 29, 2015
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
20 changes: 20 additions & 0 deletions base/functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,24 @@ call(::OrFun, x, y) = x | y
immutable AddFun <: Func{2} end
call(::AddFun, x, y) = x + y

immutable SubFun <: Func{2} end
call(::SubFun, x, y) = x - y

immutable MulFun <: Func{2} end
call(::MulFun, x, y) = x * y

immutable RDivFun <: Func{2} end
call(::RDivFun, x, y) = x / y

immutable LDivFun <: Func{2} end
call(::LDivFun, x, y) = x \ y

immutable IDivFun <: Func{2} end
call(::IDivFun, x, y) = div(x, y)

immutable PowFun <: Func{2} end
call(::PowFun, x, y) = x ^ y

immutable MaxFun <: Func{2} end
call(::MaxFun, x, y) = scalarmax(x,y)

Expand Down Expand Up @@ -121,9 +136,14 @@ function specialized_unary(f::Function)
end
function specialized_binary(f::Function)
is(f, +) ? AddFun() :
is(f, -) ? SubFun() :
is(f, *) ? MulFun() :
is(f, /) ? RDivFun() :
is(f, \) ? LDivFun() :
is(f, ^) ? PowFun() :
is(f, &) ? AndFun() :
is(f, |) ? OrFun() :
is(f, div) ? IDivFun() :
UnspecializedFun{2}(f)
end

Expand Down
2 changes: 1 addition & 1 deletion test/functors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ for op in (identity, abs, abs2, exp, log)
@test Base.specialized_unary(op)(3) == Base.specialized_unary(x->op(x))(3) == op(3)
@test Base.specialized_unary(op)(-5+im) == Base.specialized_unary(x->op(x))(-5+im) == op(-5+im)
end
for op in (+, *, &, |)
for op in (+, -, *, /, \, div, ^, &, |)
@test Base.specialized_binary(op)(2,10) == Base.specialized_binary((x,y)->op(x,y))(2,10) == op(2,10)
end

Expand Down