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 Fix1 (partial application). #26708

Merged
merged 1 commit into from
Apr 5, 2018
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
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