# Composing a function with keyword arguments

**URL:** <https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677>\
**Category:** General Usage\
**Created:** [August 19, 2021, 1:57pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677 "2021-08-19T13:57:23Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [August 19, 2021, 1:57pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/1 "2021-08-19T13:57:23Z")

</div>

Hi all,

I want to wrap a function with keyword arguments. Here is what I am trying to do,

```julia

# Wraps `fk(x)` with `d`, i.e. `fn(x) = fk(d * x)` 
function wrapper(fk)    
    function fn(x; d = 1)
        @show d
        fk(d * x)
    end
end

# Wraps `fi`, `niter` times 
wrap(fi, niter) = ∘([wrapper for _ in 1 : niter]...)(fi) 

# Get wrapped function `h`  
niter = 5;
f0(x) = x;
h = wrap(f0, niter);

```

and this is the output I get when I call `h` with `d` specified.

```julia
julia> h(5, d=10);
d = 10
d = 1
d = 1
d = 1
d = 1

```

The problem here is that the keyword parameter `d` is evaluated as `10` just once (I think just when the function is compiled), not `5` times. I wonder if it is possible to evaluate as `10` for `5` times, not just for once?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 19, 2021, 2:09pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/2 "2021-08-19T14:09:59Z")

</div>

what are you \*really trying to do? (smell of XY problem

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [August 19, 2021, 2:16pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/3 "2021-08-19T14:16:40Z")

</div>

What I am trying to do is this: given `f0(x) = x`, the iteration should continue like this

```julia

f1(x) = d x

f2(x) = d^2 x

f3(x) = d^3 x

f4(x) = d^4 x

f5(x) = d^5 x

```

After wrapping `f0` `5` times I should get `h(x) = f5(x) = d^5 x` which means I should get `h(5, d=10) = 10^5 * 5 = 50000`

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 19, 2021, 2:40pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/4 "2021-08-19T14:40:53Z")

</div>

if your goal is to obtain a sequence like this:

```julia
julia> f(x, d, n) = d^n * x
f (generic function with 1 method)

julia> f.(0.1, 10, 1:5)
5-element Vector{Float64}:
     1.0
    10.0
   100.0
  1000.0
 10000.0

```

or do you need a vector of functions for some other reason?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [August 19, 2021, 3:46pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/5 "2021-08-19T15:46:08Z")

</div>

You can achieve that with a functor:

> [@In Julia, how to create a function that saves its own internal state?](https://discourse.julialang.org/t/in-julia-how-to-create-a-function-that-saves-its-own-internal-state/58457/6):
>
> Or a functor? struct F state end (o::F)(x) = o.state + x f = F(1) f(2) 3 more specifically: julia\> Base.@kwdef mutable struct F state = nothing end F julia\> function (o::F)() if o.state == nothing o.state = 1 else o.state += 1 end end julia\> f = F() F(nothing) julia\> f() 1 julia\> f() 2 julia\> f() 3 julia\> f.state # now this is part of the language, because f is an instance of F 3

Though it is probably good to be sure that’s is the best strategy for what you really want to do.

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [August 19, 2021, 5:41pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/6 "2021-08-19T17:41:10Z")

</div>

> [@jling](#):
>
> if your goal is to obtain a sequence like this:
> 
> ```julia
> julia> f(x, d, n) = d^n * x
> f (generic function with 1 method)
> 
> julia> f.(0.1, 10, 1:5)
> 5-element Vector{Float64}:
> 1.0
> 10.0
> 100.0
> 1000.0
> 10000.0
> 
> ```
> 
> or do you need a vector of functions for some other reason?

Neither of them, actually. Simply stated, consider that there is a mapping T[f\_k; d[that maps f\_k to f\_{k + 1} such that f\_{k + 1}(x) = T[f\_k; d](x) = f\_k(d \cdot x). Here, d \in \mathbb{R} is a parameter of the mapping T[f\_k; d]. Given an initial function f\_0(x), I want to compute the limit f^\* of the mapping T[f\_k; d], i.e., f^\*(x) = \lim\limits\_{k \mapsto \infty} T[f\_k; d](x) \approx T^{\circ N}[f\_0; d] for some large N where \circ is the composition operator. So, what I really want is the limit f^\* of T[f\_k;d] (which is a composed function, not a sequence of numbers, or a vector of functions).

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [August 19, 2021, 5:43pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/7 "2021-08-19T17:43:15Z")

</div>

> [@lmiq](#):
>
> You can achieve that with a functor:

May be an option, yes. Thanks for the suggestion.

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [August 19, 2021, 6:02pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/8 "2021-08-19T18:02:29Z")

</div>

If the initial function `f0(x)` also accepts a keyword argument, I think the following works.

```julia
function wrapper(fk)    
    function fn(x; d = 1)
        @show d
        fk(d * x; d=d)
    end
end

wrap(fi, niter) = ∘([wrapper for _ in 1 : niter]...)(fi) 

niter = 5;
f0(x; d=1) = x;
h = wrap(f0, niter);
h(5, d = 10) 

```

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [August 19, 2021, 6:16pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/9 "2021-08-19T18:16:12Z")

</div>

You probably realize this already, but I’ll just comment that the behavior you see is expected. If you unroll your original `h` (with `niter = 2` for simplicity), you get

```julia
h = function fn(x; d = 1)
    @show d
    (function fn(x; d = 1)
        @show d
        f0(d * x)
    end)(d * x)
end

```

so `h(5, d = 10)` only modifies the `d` of the outermost `fn`, not any of the inner `d`s.

* * *

I started this post before your most recent post, so it looks like you already figured it out. But I’ll leave this comment in case it helps someone else understand why.

---

<div class="post-metadata">

**Author:** ![zekeriya.sari](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zekeriya.sari/32/13695_2.png) [@zekeriya.sari](https://discourse.julialang.org/u/zekeriya.sari)\
**Post date:** [August 19, 2021, 6:31pm UTC](https://discourse.julialang.org/t/composing-a-function-with-keyword-arguments/66677/10 "2021-08-19T18:31:42Z")

</div>

> [@StevenWhitaker](#):
>
> so `h(5, d = 10)` only modifies the `d` of the outermost `fn` , not any of the inner `d` s.

Yes, that is exactly the case. Thank you for your time and comment.
