2022.10.26 01:38 PM
Hi,
Newbie question I guess. To simplify, say I have 2 functions f1 & f2:
q) f1: {1-x%y}
q) f2: {mavg[x;y]} /This is voluntary
which actually in this case, have a common argument when a composition <g> is applied, eg. :
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
I can't figure a compact/elegant way to specify <g> without repeating twice argument <a>.
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
2022.10.26 08:55 PM
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
2022.10.27 04:20 AM - edited 2022.10.27 04:28 AM
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
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
2022.10.26 08:55 PM
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
2022.10.27 04:20 AM - edited 2022.10.27 04:28 AM
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
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
2022.10.27 11:47 AM
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
EMEA
Tel: +44 (0)28 3025 2242
AMERICAS
Tel: +1 (212) 447 6700
APAC
Tel: +61 (0)2 9236 5700
KX. All Rights Reserved.
KX and kdb+ are registered trademarks of KX Systems, Inc., a subsidiary of FD Technologies plc.