<commit_before># common functors

abstract Functor{N}  # N: the number of arguments

typealias UnaryFunctor Functor{1}
typealias BinaryFunctor Functor{2}
typealias TernaryFunctor Functor{3}

# additional functions

logit{T<:FloatingPoint}(x::T) = log(x/(one(T)-x))
xlogx{T<:FloatingPoint}(x::T) = x > 0 ? x * log(x) : zero(T)
xlogy{T<:FloatingPoint}(x::T, y::T) = x > 0 ? x * log(y) : zero(T)

relu{T<:FloatingPoint}(x::T) = max(zero(T), x)

logistic{T<:FloatingPoint}(x::T) = one(T)/(one(T) + exp(-x))
invlogistic{T<:FloatingPoint}(y::T) = -log(one(T) / y - one(T))

softplus{T<:FloatingPoint}(x::T) = log(one(T) + exp(x))
invsoftplus{T<:FloatingPoint}(y::T) = log(exp(y) - one(T))

# unary functors 

for e in [
    (:Negate, :-), (:AbsFun, :abs), (:Abs2Fun, :abs2), (:SqrtFun, :sqrt), (:CbrtFun, :cbrt),
    (:FloorFun, :floor), (:CeilFun, :ceil), (:RoundFun, :round), (:TruncFun, :trunc),
    (:ExpFun, :exp), (:Exp2Fun, :exp2), (:Exp10Fun, :exp10), (:Expm1Fun, :expm1),
    (:LogFun, :log), (:Log2Fun, :log2), (:Log10Fun, :log10), (:Log1pFun, :log1p), 
    (:SinFun, :sin), (:CosFun, :cos), (:TanFun, :tan), 
    (:AsinFun, :asin), (:AcosFun, :acos), (:AtanFun, :atan), 
    (:SinhFun, :sinh), (:CoshFun, :cosh), (:TanhFun, :tanh),
    (:AsinhFun, :asinh), (:AcoshFun, :acosh), (:AtanhFun, :atanh), 
    (:ErfFun, :erf), (:ErfcFun, :erfc), 
    (:GammaFun, :gamma), (:LgammaFun, :lgamma), (:DigammaFun, :digamma), 
    (:IsfiniteFun, :isfinite), (:IsnanFun, :isnan), (:IsinfFun, :isinf)]

    @eval type $(e[1]) <: UnaryFunctor end
    @eval evaluate(::($(e[1])), x::Number) = ($(e[2]))(x)
end

# binary functors

for e in [
    (:Add, :+), (:Subtract, :-), (:Multiply, :*), (:Divide, :/), (:Pow, :^), 
    (:Greater, :>), (:GreaterEqual, :>=), (:Less, :<), (:LessEqual, :<=), 
    (:Equal, :(==)), (:NotEqual, :(!=)),
    (:MaxFun, :max), (:MinFun, :min), (:HypotFun, :hypot), (:Atan2Fun, :atan2)]

    @eval type $(e[1]) <: BinaryFunctor end
    @eval evaluate(::($(e[1])), x::Number, y::Number) = ($(e[2]))(x, y)
end

### Note: this special code is needed to get performance 
# due to compiler issues, the functions are not properly inlined without this
# in some cases

evaluate{T<:Integer}(::MaxFun, x::T, y::T) = (x > y ? x : y)
evaluate{T<:Integer}(::MinFun, x::T, y::T) = (x < y ? x : y)

@unix_only const libm = dlopen("libm")
@unix_only const _fmax  = dlsym(libm, :fmax)
@unix_only const _fmaxf = dlsym(libm, :fmaxf)
@unix_only const _fmin  = dlsym(libm, :fmin)
@unix_only const _fminf = dlsym(libm, :fminf)

@unix_only evaluate(::MaxFun, x::Float64, y::Float64) = ccall(_fmax,  Float64, (Float64, Float64), x, y)
@unix_only evaluate(::MaxFun, x::Float32, y::Float32) = ccall(_fmaxf, Float32, (Float32, Float32), x, y)
@unix_only evaluate(::MinFun, x::Float64, y::Float64) = ccall(_fmin,  Float64, (Float64, Float64), x, y)
@unix_only evaluate(::MinFun, x::Float32, y::Float32) = ccall(_fminf, Float32, (Float32, Float32), x, y)

immutable FixAbsPow{T<:Real} <: UnaryFunctor 
    p::T
end
evaluate(op::FixAbsPow, x::Number) = abs(x) ^ op.p<commit_msg>no need to specialize min/max with a faster min/max in base<commit_after># common functors

abstract Functor{N}  # N: the number of arguments

typealias UnaryFunctor Functor{1}
typealias BinaryFunctor Functor{2}
typealias TernaryFunctor Functor{3}

# additional functions

logit{T<:FloatingPoint}(x::T) = log(x/(one(T)-x))
xlogx{T<:FloatingPoint}(x::T) = x > 0 ? x * log(x) : zero(T)
xlogy{T<:FloatingPoint}(x::T, y::T) = x > 0 ? x * log(y) : zero(T)

relu{T<:FloatingPoint}(x::T) = max(zero(T), x)

logistic{T<:FloatingPoint}(x::T) = one(T)/(one(T) + exp(-x))
invlogistic{T<:FloatingPoint}(y::T) = -log(one(T) / y - one(T))

softplus{T<:FloatingPoint}(x::T) = log(one(T) + exp(x))
invsoftplus{T<:FloatingPoint}(y::T) = log(exp(y) - one(T))

# unary functors 

for e in [
    (:Negate, :-), (:AbsFun, :abs), (:Abs2Fun, :abs2), (:SqrtFun, :sqrt), (:CbrtFun, :cbrt),
    (:FloorFun, :floor), (:CeilFun, :ceil), (:RoundFun, :round), (:TruncFun, :trunc),
    (:ExpFun, :exp), (:Exp2Fun, :exp2), (:Exp10Fun, :exp10), (:Expm1Fun, :expm1),
    (:LogFun, :log), (:Log2Fun, :log2), (:Log10Fun, :log10), (:Log1pFun, :log1p), 
    (:SinFun, :sin), (:CosFun, :cos), (:TanFun, :tan), 
    (:AsinFun, :asin), (:AcosFun, :acos), (:AtanFun, :atan), 
    (:SinhFun, :sinh), (:CoshFun, :cosh), (:TanhFun, :tanh),
    (:AsinhFun, :asinh), (:AcoshFun, :acosh), (:AtanhFun, :atanh), 
    (:ErfFun, :erf), (:ErfcFun, :erfc), 
    (:GammaFun, :gamma), (:LgammaFun, :lgamma), (:DigammaFun, :digamma), 
    (:IsfiniteFun, :isfinite), (:IsnanFun, :isnan), (:IsinfFun, :isinf)]

    @eval type $(e[1]) <: UnaryFunctor end
    @eval evaluate(::($(e[1])), x::Number) = ($(e[2]))(x)
end

# binary functors

for e in [
    (:Add, :+), (:Subtract, :-), (:Multiply, :*), (:Divide, :/), (:Pow, :^), 
    (:Greater, :>), (:GreaterEqual, :>=), (:Less, :<), (:LessEqual, :<=), 
    (:Equal, :(==)), (:NotEqual, :(!=)),
    (:MaxFun, :max), (:MinFun, :min), (:HypotFun, :hypot), (:Atan2Fun, :atan2)]

    @eval type $(e[1]) <: BinaryFunctor end
    @eval evaluate(::($(e[1])), x::Number, y::Number) = ($(e[2]))(x, y)
end

### Note: this special code is needed to get performance 
# due to compiler issues, the functions are not properly inlined without this
# in some cases

evaluate{T<:Integer}(::MaxFun, x::T, y::T) = (x > y ? x : y)
evaluate{T<:Integer}(::MinFun, x::T, y::T) = (x < y ? x : y)

immutable FixAbsPow{T<:Real} <: UnaryFunctor 
    p::T
end
evaluate(op::FixAbsPow, x::Number) = abs(x) ^ op.p
{'fraction_non_alphanumeric': 0.24715447154471545, 'fraction_numerical': 0.01842818428184282, 'mean_word_length': 5.2202247191011235, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2920328', 'n_tokens_mistral': 2586, 'n_tokens_neox': 2365, 'n_words': 519}