# Function composition using common argument

**URL:** <https://forum.kx.com/t/function-composition-using-common-argument/13396>\
**Category:** Community Support\
**Tags:** imported, kdb-and-q\
**Created:** [October 26, 2022, 12:00am UTC](https://forum.kx.com/t/function-composition-using-common-argument/13396 "2022-10-26T00:00:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jp1](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@jp1](https://forum.kx.com/u/jp1)\
**Post date:** [October 26, 2022, 12:00am UTC](https://forum.kx.com/t/function-composition-using-common-argument/13396/1 "2022-10-26T00:00:00Z")

</div>

[https://learninghub.kx.com/forums/topic/function-composition-using-common-argument](https://learninghub.kx.com/forums/topic/function-composition-using-common-argument)

Hi,

Newbie question I guess. To simplify, say I have 2 functions f1 & f2:

&nbsp;

```
q) f1: {1-x%y} 
q) f2: {mavg[x;y]} /This is voluntary
```

&nbsp;

which actually in this case, have a common argument when a composition is applied, eg. :

&nbsp;

```
q) a: til 10 
q) g: f1 . (f2 . (3;a); a) 
0n 0.5 0.5 0.3333333 0.25 0.2 0.1666667 0.1428571 0.125 0.1111111
```

&nbsp;

I can’t figure a compact/elegant way to specify **without repeating** twice argument .

 

A wrapper function can obviously do the job, though complexity grows with the number of compositions. So the question is basically: Is there an iterative (or other) way to pass an identical argument to any number of multivalent functions composed successively?

 

Thx

---

<div class="post-metadata">

**Author:** ![cillianreilly](https://avatars.discourse-cdn.com/v4/letter/c/7bcc69/32.png) [@cillianreilly](https://forum.kx.com/u/cillianreilly)\
**Post date:** [October 27, 2022, 12:00am UTC](https://forum.kx.com/t/function-composition-using-common-argument/13396/2 "2022-10-27T00:00:00Z")

</div>

You can do this using iteration over a list of the functions to apply.

```
q){z .(y;x)}[a]/[3;(f2;f1)] 
0n 0.5 0.5 0.3333333 0.25 0.2 0.1666667 0.1428571 0.125 0.1111111
```

Adding another function:

```
q)f3:+ 
q){z .(y;x)}[a]/[3;(f2;f1;f3)] 
0n 1.5 2.5 3.333333 4.25 5.2 6.166667 7.142857 8.125 9.111111
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![lkerr2](https://sea2.discourse-cdn.com/flex002/user_avatar/forum.kx.com/lkerr2/32/57_2.png) [@lkerr2](https://forum.kx.com/u/lkerr2)\
**Post date:** [October 27, 2022, 12:00am UTC](https://forum.kx.com/t/function-composition-using-common-argument/13396/3 "2022-10-27T00:00:00Z")

</div>

Q doesnt really offer built-in combinators as liberally as its ancestor language APL does. An elegant way to compose the projections of `f1` and `f2`? You can lose the parens

```
q)c: f1[;a] f2[;a]@ 
q)c 3 
0n 0.5 0.5 0.3333333 0.25 0.2 0.1666667 0.1428571 0.125 0.1111111
```

Of course, if you have to do this often, you can write your own combinator. Call it `cr` for _curry right:_

```
q)cr:{x[;y]} 
q)c:('[;])over(f1;f2)cr:a / Compose over (f1[;a];f2[;a]) 
q)c 3 
0n 0.5 0.5 0.3333333 0.25 0.2 0.1666667 0.1428571 0.125 0.1111111
```

[Compose](https://code.kx.com/q/ref/compose/") over a list of functions as many as you need.

---

<div class="post-metadata">

**Author:** ![jp1](https://avatars.discourse-cdn.com/v4/letter/j/f1d935/32.png) [@jp1](https://forum.kx.com/u/jp1)\
**Post date:** [October 27, 2022, 12:00am UTC](https://forum.kx.com/t/function-composition-using-common-argument/13396/4 "2022-10-27T00:00:00Z")

</div>

Many thanks to you both for your inputs, very useful illustrations of projection/iteration/composition functionalities.

The function approaches work well in their current incarnation, with the small caveat (at least from my understanding) that functions to be composed must have the same rank. Relatively easy to implement argument condition handling should allow for a more generic multivalent case. Again, thank you,

Best,

JP
