From 3c128297f4f13216ed32b56069ed119ca9d411bd Mon Sep 17 00:00:00 2001 From: matthias314 Date: Sat, 7 Sep 2024 14:02:47 -0400 Subject: [PATCH 1/2] added `FixKw` --- base/operators.jl | 43 +++++++++++++++++++++++++++++++++++++++++++ base/public.jl | 1 + doc/src/base/base.md | 1 + 3 files changed, 45 insertions(+) diff --git a/base/operators.jl b/base/operators.jl index d01902e302359..5f3f268ed310e 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -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 @@ -1203,6 +1205,47 @@ 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. + +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) diff --git a/base/public.jl b/base/public.jl index 803766a0cec1b..9ea7929102b62 100644 --- a/base/public.jl +++ b/base/public.jl @@ -17,6 +17,7 @@ public Fix, Fix1, Fix2, + FixKw, Generator, ImmutableDict, OneTo, diff --git a/doc/src/base/base.md b/doc/src/base/base.md index b5d50a846ce89..3c1ba9d3b3062 100644 --- a/doc/src/base/base.md +++ b/doc/src/base/base.md @@ -285,6 +285,7 @@ Base.splat Base.Fix Base.Fix1 Base.Fix2 +Base.FixKw ``` ## Syntax From 34febcfd295deef87f4833af694274fe590f1462 Mon Sep 17 00:00:00 2001 From: matthias314 Date: Sat, 7 Sep 2024 19:51:28 -0400 Subject: [PATCH 2/2] added compat note --- base/operators.jl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/base/operators.jl b/base/operators.jl index 5f3f268ed310e..d009bca05f94f 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -1213,6 +1213,9 @@ 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