# Closure over a function with keyword arguments while keeping access to the keyword arguments

**URL:** <https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574>\
**Category:** General Usage\
**Tags:** closure\
**Created:** [September 27, 2018, 4:52pm UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574 "2018-09-27T16:52:23Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [September 27, 2018, 4:52pm UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/1 "2018-09-27T16:52:23Z")

</div>

Hi everyone,

I have the following problem. I have a function `foo(x; y = 1)` and would like to obtain a function that calls foo while keeping `x` constant, but still allows me to specify different values for `y`. I would like something like:

PSEUDOCODE:

```julia
foo(x; y = 1) = x + y

x = 1

foo_closure( ; y = 1) = foo(x; y = 1)

foo_closure() -> 2
foo_closure(y = 1) -> 2
foo_closure(y = 2) -> 3

```

Is this possible?

The reason I want this is that I can broadcast over `y`

```julia
y = range(0, stop = 1, 100)
foo_closure.(y = y)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 27, 2018, 5:08pm UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/2 "2018-09-27T17:08:09Z")

</div>

```julia
foo_closure(y = 2) -> 3

```

Why would this happen when

```julia
foo(x; y = 1) = x + 1

```

and `x` is constant `1`?

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [September 27, 2018, 5:11pm UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/3 "2018-09-27T17:11:12Z")

</div>

I am sorry, I meant of course

```julia
foo(x; y = 1) = x + y

```

Then

```julia
x = 1

foo_closure( ; y = 1) = foo(x; y = 1)

```

Would be effectively the same as `foo_closure(y) = y + 1`

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 27, 2018, 5:19pm UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/4 "2018-09-27T17:19:07Z")

</div>

```julia
foo_closure( ; y = 1) = foo(x; y = y)

```

?

Or just

```julia
foo_closure( ; kwargs...) = foo(x; kwargs...)

```

to pass along all the kwargs.

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [September 27, 2018, 7:23pm UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/5 "2018-09-27T19:23:34Z")

</div>

Thanks, I will try it tomorrow 🙂

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [September 28, 2018, 9:07am UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/6 "2018-09-28T09:07:15Z")

</div>

This is amazing, thanks a lot! 🙂

---

<div class="post-metadata">

**Author:** ![IljaK91](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iljak91/32/44301_2.png) [@IljaK91](https://discourse.julialang.org/u/IljaK91)\
**Post date:** [September 28, 2018, 9:37am UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/7 "2018-09-28T09:37:54Z")

</div>

I have one more question. Using the example from above, I would like to broadcast over a different values for the keyword argument.

```julia
# Create a function with one kwarg
foo(x; y = 1) = x .+ y
# Fix x
x = 1
# Define closure
foo_closure(; kwargs...) = foo(x; kwargs...)
# Create range of values I want to broadcast over
test_var = range(0, stop = 1, length = 2)

```

So this runs smoothly

```julia
julia> foo.(test_var)
2-element Array{Float64,1}:
 1.0
 2.0

```

But it does not seem to work with the kwarg:

```julia
julia> foo_closure.(y = test_var)
1.0:1.0:2.0

```

Does the `f.()` broadcasting interface not work with kwargs or do I have to change something? I know I could just write

```julia
res = zeros(2,1)
for i in 1:length(test_var)
    res[i] = foo_closure(y = test_var[i])
end

```

or better perhaps

```julia
res = [foo_closure(y = x) for x in test_var]

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [September 29, 2018, 12:55am UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/8 "2018-09-29T00:55:03Z")

</div>

AFAIU keywords are constants under broadcasting.

---

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [September 29, 2018, 7:41am UTC](https://discourse.julialang.org/t/closure-over-a-function-with-keyword-arguments-while-keeping-access-to-the-keyword-arguments/15574/9 "2018-09-29T07:41:10Z")

</div>

> [@IljaK91](#):
>
> But it does not seem to work with the kwarg:
> 
> ```julia
> julia> foo_closure.(y = test_var)
> 1.0:1.0:2.0
> 
> ```

try with `collect(test_var)`, it just turned the `0:1:1` range to `1:1:2`
