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 FixKw, a companion to Fix{N} #55711

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
46 changes: 46 additions & 0 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1169,6 +1169,8 @@ A type representing a partially-applied version of a function `f`, with the argu
available arguments, rather than an absolute ordering on the target function. For example,
`Fix{1}(Fix{2}(f, 4), 4)` fixes the first and second arg, while `Fix{2}(Fix{1}(f, 4), 4)`
fixes the first and third arg.

See also [`Base.FixKw`](@ref).
"""
struct Fix{N,F,T} <: Function
f::F
Expand Down Expand Up @@ -1203,6 +1205,50 @@ Alias for `Fix{2}`. See [`Fix`](@ref Base.Fix).
"""
const Fix2{F,T} = Fix{2,F,T}

"""
Base.FixKw(f, kw::NamedTuple) <: Function
Base.FixKw(f; kw...) <: Function

Returns a `Function` that calls `f` with the keyword arguments `kw` prepended
to those given in each call. The fixed keyword arguments cannot be removed,
but they can be overridden by those specified in the call.

!!! compat "Julia 1.12"
`Base.FixKw` requires at least Julia 1.12.

See also [`Base.Fix`](@ref).

# Examples
```jldoctest
julia> f(x; a = 0, b = 0, c = 0) = x+a+b+c;

julia> g = Base.FixKw(f, (a = 1, b = 2));

julia> g(0)
3

julia> g(0; c = 3)
6

julia> g(0; a = 0, c = 3)
5

julia> map(Base.FixKw(sum; init = 0), [(), (1,), (1, 2)])
3-element Vector{Int64}:
0
1
3
```
"""
struct FixKw{F,KW<:NamedTuple} <: Function
f::F
kw::KW
FixKw(f, kw::KW) where KW <: NamedTuple = new{_stable_typeof(f),KW}(f, kw)
end

FixKw(f; kw...) = FixKw(f, values(kw))

(g::FixKw)(args::Vararg{Any,N}; kw...) where N = g.f(args...; pairs(g.kw)..., kw...)

"""
isequal(x)
Expand Down
1 change: 1 addition & 0 deletions base/public.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public
Fix,
Fix1,
Fix2,
FixKw,
Generator,
ImmutableDict,
OneTo,
Expand Down
1 change: 1 addition & 0 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ Base.splat
Base.Fix
Base.Fix1
Base.Fix2
Base.FixKw
```

## Syntax
Expand Down
Loading