
| Current Path : /var/www/web-klick.de/dsh/50_dev2017/1310__algorithms/Julia/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/web-klick.de/dsh/50_dev2017/1310__algorithms/Julia/functors.jl |
abstract Functor{N}
typealias UnaryFunctor Functor{1}
typealias BinaryFunctor Functor{2}
typealias TernaryFunctor Functor{3}
macro functor1(F, T, f)
quote
global evaluate
type $F <: Functor{1} end
evaluate(::($F), x::($T)) = ($f)(x)
end
end
macro functor2(F, T, f)
quote
global evaluate
type $F <: Functor{2} end
evaluate(::($F), x::($T), y::($T)) = ($f)(x, y)
end
end
macro functor3(F, T, f)
quote
global evaluate
type $F <: Functor{3} end
evaluate(::($F), x::($T), y::($T), z::($T)) = ($f)(x, y, z)
end
end
export SqrtFun, Abs2Fun, Add
@functor2 SqrtFun Number sqrt
@functor1 Abs2Fun Number abs2
type Add <: BinaryFunctor end
evaluate(::Add, x::Number, y::Number) = x + y
fptype{T<:FloatingPoint}(::Type{T}) = T
fptype{T<:Integer}(::Type{T}) = Float64
fptype{T<:Union(Bool,Uint8,Int8,Uint16,Int16)}(::Type{T}) = Float32
result_type(f::Functor{1}, t1::Type) = typeof(evaluate(f, one(t1)))
result_type(f::Functor{2}, t1::Type, t2::Type) = typeof(evaluate(f, one(t1), one(t2)))
result_type(f::Functor{3}, t1::Type, t2::Type, t3::Type) = typeof(evaluate(f, one(t1),one(t2), one(t3)))