Skip to content

Commit

Permalink
Merge pull request #26708 from tpapp/tp/add-fix2
Browse files Browse the repository at this point in the history
Add Fix1 (partial application).
  • Loading branch information
StefanKarpinski authored Apr 5, 2018
2 parents 23227c5 + b0ab4b9 commit da67bff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
23 changes: 20 additions & 3 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -813,12 +813,29 @@ julia> filter(!isalpha, str)
"""
!(f::Function) = (x...)->!f(x...)

"""
Fix1(f, x)
A type representing a partially-applied version of the two-argument function
`f`, with the first argument fixed to the value "x". In other words,
`Fix1(f, x)` behaves similarly to `y->f(x, y)`.
"""
struct Fix1{F,T} <: Function
f::F
x::T

Fix1(f::F, x::T) where {F,T} = new{F,T}(f, x)
Fix1(f::Type{F}, x::T) where {F,T} = new{Type{F},T}(f, x)
end

(f::Fix1)(y) = f.f(f.x, y)

"""
Fix2(f, x)
A type representing a partially-applied version of function `f`, with the second
argument fixed to the value "x".
In other words, `Fix2(f, x)` behaves similarly to `y->f(y, x)`.
A type representing a partially-applied version of the two-argument function
`f`, with the second argument fixed to the value "x". In other words,
`Fix2(f, x)` behaves similarly to `y->f(y, x)`.
"""
struct Fix2{F,T} <: Function
f::F
Expand Down
9 changes: 9 additions & 0 deletions test/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,12 @@ end

@test fldmod1(4.0, 3) == fldmod1(4, 3)
end

@testset "Fix12" begin
x = 9
y = 7.0
fx = Base.Fix1(/, x)
fy = Base.Fix2(/, y)
@test fx(y) == x / y
@test fy(x) == x / y
end

0 comments on commit da67bff

Please sign in to comment.