# Looking for a curry package

**URL:** <https://discourse.julialang.org/t/looking-for-a-curry-package/134223>\
**Category:** General Usage\
**Tags:** currying\
**Created:** [November 29, 2025, 6:23pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223 "2025-11-29T18:23:58Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [November 29, 2025, 6:23pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/1 "2025-11-29T18:23:58Z")

</div>

I have a lot of functions that I would like to add an extra method to with the code

```julia-auto
f(; kwargs...) = (args...; kw...) -> f(args...; kwargs..., kw...)

```

where `f` is the function which takes at least one positional argument and one keyword argument. Is there a package that can do this?

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [November 29, 2025, 7:39pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/2 "2025-11-29T19:39:17Z")

</div>

I made a macro that more or less solves my needs:

```julia
macro flexkw(f, g=f)
    :($f(; kwargs...) = (args...; kw...) -> $g(args...; kwargs..., kw...))
end

```

This allows me to just run

```julia
@flexkw f

```

to add the new method to the function `f`. If a package that can do this already exists, then I would still be interested in hearing about it.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 29, 2025, 8:04pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/3 "2025-11-29T20:04:55Z")

</div>

Should humour be allowed here, you might want to consider [this package](https://www.orientalwebshop.nl/en/curry-package).

---

<div class="post-metadata">

**Author:** ![reallyasi9](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/reallyasi9/32/207648_2.png) [@reallyasi9](https://discourse.julialang.org/u/reallyasi9)\
**Post date:** [November 29, 2025, 8:17pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/4 "2025-11-29T20:17:12Z")

</div>

Rather than a macro that defines a function that itself returns an anonymous function that calls the method you want, does it fit in your use case to simply define a method with a different name?

```julia
f(a, b; negative=false, exponent=2) = negative ? (a - b)^exponent : (a + b)^exponent
f_negative(args...; kwargs...) = f(args...; negative=true, kwargs...)

@assert f(1, 2; exponent=3) == 27
@assert f(1, 2; negative=true, exponent=3) == -1
@assert f_negative(1, 2; exponent=3) == -1

```

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [November 29, 2025, 9:10pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/5 "2025-11-29T21:10:07Z")

</div>

Thanks for the reply; the anonymous function aspect is really important for me because I am using it as input to another function and want to try out several different combinations of keyword values.

e.g. if `@flexkw f` was run immediately after definition of the `f` that you define, then

```julia
a = sort(rand(55); lt=f(; negative=true, exponent=5))
b = sort(rand(55); lt=f(; exponent=4))
c = sort(rand(55); lt=f(; negative=true, exponent=8))

```

would be legal and (in my opinion) easier to read than

```julia
a = sort(rand(55); lt=(x,y)->f(x,y; negative=true, exponent=5))
b = sort(rand(55); lt=(x,y)->f(x,y; exponent=4))
c = sort(rand(55); lt=(x,y)->f(x,y; negative=true, exponent=8))

```

For 1 argument functions, this also makes the `|>` syntax neater:

```julia
g(x; a=11, b=7) = a*x + b
@flexkw g
rand(88) |> sort |> g(; a=5, b=8)
# rather than
rand(88) |> sort |> x->g(x; a=5, b=8)

```

PartialFunctions.jl can produce anonymous functions, but I prefer the solution that I have shown in this post. It is also the solution used by some functions in `Base` (e.g. `contains`).

* * *

@rafael.guerra : I like the humour 😄

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [November 30, 2025, 1:44am UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/6 "2025-11-30T01:44:35Z")

</div>

Would the following be relevant?

> <https://github.com/JuliaLang/julia/pull/55711>
>
> Keyword arguments are mentioned several times in the discussion of the recent \`F…ix{N}\` PR #54653. However, support for them was dropped in the end. I don't know if that was to keep \`Fix{N}\` simple or for more fundamental reasons. I would like to propose \`FixKw\` as a way to do for keyword arguments what \`Fix{N}\` does for positional arguments. I think it should exist for the sake of completeness. It is also helpful because \`map\`, \`reduce\` and the like don't allow to pass keyword arguments to the function one uses.
> 
> I've chosen the name \`FixKw\` to resemble \`Fix\` although the "fixed" keyword arguments can still be overridden. I'm open to other names.
> 
> Please let me know what you think. If you like it, I can add tests and update \`NEWS.md\`.

Then your examples would be of the form

```julia-auto
a = sort(rand(55); lt = FixKw(f; negative = true, exponent = 5))

```

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [November 30, 2025, 5:47am UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/7 "2025-11-30T05:47:12Z")

</div>

Thanks for bringing this to my attention. For my specific situation I still prefer the `@flexkw` macro because it is shorter and easier for me personally to read. In a slightly different scenario (where I don’t own the function), e.g.

```julia
rand(88) |> x->sort(x; rev=true) #1

```

the `FixKw` version would be

```julia
rand(88) |> FixKw(sort; rev=true) #2

```

and would be up against

```julia
using PartialFunctions
rand(88) |> sort $ (; rev=true) #3

```

```julia
using PartialFuns
@underscores rand(88) |> sort(_; rev=true) #4

```

and

```julia
@flexkw flexkw_sort sort
rand(88) |> flexkw_sort(; rev=true) #5

```

Personally, I would rank these in the order 1,2,4,3,5 but with time I might end up preferring 2 over 1.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 30, 2025, 10:56am UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/8 "2025-11-30T10:56:52Z")

</div>

The main alternative is to write an eval loop like

```julia-auto
for f in functions_to_curry
    @eval $f(; kwargs...) = (args...; kw...) -> $f(args...; kwargs..., kw...)
end

```

where `functions_to_curry` is a collection of symbols.

Compared to the macro approach it’s mostly a stylistic choice but it might be nicer if you have many functions and can construct `functions_to_curry` efficiently, e.g. extract all functions from a module.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [November 30, 2025, 11:02am UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/9 "2025-11-30T11:02:18Z")

</div>

For the macro approach you could also consider writing the macro so it can work as a decorator on the function definition:

```julia-auto
@flexkw function f(...)
...
end

```

(Pass through the original AST and append the extra method after extracting the function name from the AST.)

---

<div class="post-metadata">

**Author:** ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)\
**Post date:** [November 30, 2025, 4:00pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/10 "2025-11-30T16:00:49Z")

</div>

To make `@flexkw` work in all situations, one should probably wrap the variables `f` and `g` in `esc`. (Alternatively, one could wrap the whole expression in this case.)

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [November 30, 2025, 7:11pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/11 "2025-11-30T19:11:22Z")

</div>

Thank you, yes this works better:

```julia
macro flexkw(f, g=f)
    esc(:($f(; kwargs...) = (args...; kw...) -> $g(args...; kwargs..., kw...)))
end

```

It can now be used in other modules and even inside other functions, e.g.:

```julia
function g()
    @flexkw flexkw_sort sort #flex_sort becomes just a local function
    rand(88) |> flexkw_sort(; rev=true)
end

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [November 30, 2025, 9:59pm UTC](https://discourse.julialang.org/t/looking-for-a-curry-package/134223/12 "2025-11-30T21:59:10Z")

</div>

The main issue with macro solutions here is that you have to have know you want to curry _before_ you start typing, since macros look forward at the expression ahead of them for what to transform. I posted a [feature request](https://github.com/JuliaLang/julia/issues/47895) a few years ago for “backwards-looking” macros which would make it easier to do things like `x %>% fun(z = 2)` (or whatever syntax). But I don’t think it’s easily implemented currently.
