ChainedFixes
ChainedFixes.ChainedFixes — ModuleChainedFixes
ChainedFixes.jl provides useful tools for interacting with functions where arguments are fixed to them. This includes support for those found in Julia's Base module (Base.Fix1, Base.Fix2) and exported from ChainedFixes (ChainedFix and NFix).
Some simple functionality available form this package is chaining any fixed function.
julia> using ChainedFixes
julia> gt_or_lt = or(>(10), <(5));
julia> gt_or_lt(2)
true
julia> gt_or_lt(6)
false
julia> gt_and_lt = and(>(1), <(5));
julia> gt_and_lt(2)
true
julia> gt_and_lt(0)
falseThere's more convenient syntax for these available in the Julia REPL.
julia> gt_or_lt = >(10) ⩔ <(5); # \Or<TAB>
julia> gt_or_lt(2)
true
julia> gt_or_lt(6)
false
julia> gt_and_lt = >(1) ⩓ <(5); # \And<TAB>
julia> gt_and_lt(2)
true
julia> gt_and_lt(0)
falseAny function can have methods fixed to it with the NFix function.
julia> fxn1(x::Integer, y::AbstractFloat, z::AbstractString) = Val(1);
julia> fxn1(x::Integer, y::AbstractString, z::AbstractFloat) = Val(2);
julia> fxn1(x::AbstractFloat, y::Integer, z::AbstractString) = Val(3);
julia> fxn2(; x, y, z) = fxn1(x, y, z);
julia> fxn3(args...; kwargs...) = (fxn1(args...), fxn2(; kwargs...));
julia> NFix{(1,2)}(fxn1, 1, 2.0)("a")
Val{1}()
julia> NFix{(1,3)}(fxn1, 1, 2.0)("a")
Val{2}()
julia> NFix{(1,3)}(fxn1, 1.0, "")(2)
Val{3}()
julia> NFix(fxn2, x=1, y=2.0)(z = "a")
Val{1}()
julia> NFix(fxn2, x=1, z=2.0)(y="a")
Val{2}()
julia> NFix{(1,2)}(fxn3, 1, 2.0; x=1.0, z="")(""; y = 1)
(Val{1}(), Val{3}())
Constants
The following constants are exported.
| Syntax | Type Constant |
|---|---|
and(f1::F1, f1::F2)/⩓(f1::F1, f1::F2) | And{F1,F2} |
or(f1::F1, f1::F2)/⩔(f1::F1, f1::F2) | Or{F1,F2} |
isapprox(x::T; kwargs::Kwargs) | Approx{T,Kwargs} |
!isapprox(x::T; kwargs::Kwargs) | NotApprox{T,Kwargs} |
in(x::T) | In{T} |
!in(x::T) | NotIn{T} |
<(x::T) | Less{T} |
<=(x::T) | LessThanOrEqual{T} |
>(x::T) | Greater{T} |
>=(x::T) | GreaterThanOrEqual{T} |
==(x::T) | Equal{T} |
isequal(x::T) | Equal{T} |
!=(x::T) | NotEqual{T} |
startswith(x::T) | StartsWith{T} |
endswith(x::T) | EndsWith{T} |
ChainedFixes.ChainedFix — TypeChainedFix{L,F1,F2}Internal type for composing functions from and and or. For example, and(x::Function, y::Function) becomes ChainedFix(and, x, y).
ChainedFixes.NFix — TypeNFix{P}(f::Function, args::Tuple, kwargs::Pairs)Allows fixing a tuple of args to the positions P (e.g., (1,3)) and the key word arguments kwargs.
julia> using ChainedFixes
julia> fxn1(x::Integer, y::AbstractFloat, z::AbstractString) = Val(1);
julia> fxn1(x::Integer, y::AbstractString, z::AbstractFloat) = Val(2);
julia> fxn1(x::AbstractFloat, y::Integer, z::AbstractString) = Val(3);
julia> fxn2(; x, y, z) = fxn1(x, y, z);
julia> fxn3(args...; kwargs...) = (fxn1(args...), fxn2(; kwargs...));
julia> NFix{(1,2)}(fxn1, 1, 2.0)("a")
Val{1}()
julia> NFix{(1,3)}(fxn1, 1, 2.0)("a")
Val{2}()
julia> NFix{(1,3)}(fxn1, 1.0, "")(2)
Val{3}()
julia> NFix(fxn2, x=1, y=2.0)(z = "a")
Val{1}()
julia> NFix(fxn2, x=1, z=2.0)(y="a")
Val{2}()
julia> NFix{(1,2)}(fxn3, 1, 2.0; x=1.0, z="")(""; y = 1)
(Val{1}(), Val{3}())ChainedFixes.and — Methodand(x, y)
x ⩓ ySynonymous with bitwise & operator but may be used to chain multiple Fix1 or Fix2 operations. The ⩓ (\And<TAB>) operator may be used in its place (e.g., x ⩓ y).
Examples
julia> using ChainedFixes
julia> and(true, <(5))(1)
true
julia> and(<(5), false)(1)
false
julia> and(and(<(5), >(1)), >(2))(3)
true
julia> and(<(5) ⩓ >(1), >(2))(3) # ⩓ == \And
true
ChainedFixes.execute — Methodexecute(f, args...; kwargs...) -> f(args...; kwargs...)Executes function f with provided positional arugments (args...) and keyword arguments (kwargs...).
ChainedFixes.getargs — Methodgetargs(f) -> TupleReturn a tuple of fixed positional arguments of the fixed function f.
Examples
julia> using ChainedFixes
julia> getargs(==(1))
(1,)
ChainedFixes.getfxn — Methodgetfxn(f) -> FunctionGiven a fixed function f, returns raw method without any fixed arguments.
ChainedFixes.getkwargs — Methodgetkwargs(f) -> PairsReturn the fixed keyword arguments of the fixed function f.
Examples
julia> using ChainedFixes
julia> getkwargs(isapprox(1, atol=2))
pairs(::NamedTuple) with 1 entry:
:atol => 2
ChainedFixes.is_fixed_function — Methodis_fixed_function(f) -> BoolReturns true if f is a callable function that already has arguments fixed to it. A "fixed" function can only be called on one argument (e.g., f(arg)) and all other arguments are already assigned. Functions that return true should also have getargs defined.
ChainedFixes.or — Methodor(x, y)
x ⩔ ySynonymous with bitwise | operator but may be used to chain multiple Fix1 or Fix2 operations. The ⩔ (\Or<TAB>) operator may be used in its place (e.g., x ⩔ y).
Examples
julia> using ChainedFixes
julia> or(true, <(5))(1)
true
julia> or(<(5), false)(1)
true
julia> or(<(5) ⩔ >(1), >(2))(3) # ⩔ == \Or
trueChainedFixes.positions — Methodpositions(f) -> Tuple{Vararg{Int}}Returns positions of new argument calls to f. For example, Fix2 would return (2,)