in q the dyadic zip operation is done by '. I.e.
l1:("a1";"a2")
l2:("b1";"b2")
(l1,'l2)~("a1b1";"a2b2")
I parse this ' as a dyadic operator '[g;l2] where g is a projection of some dyadic function on lists onto a monadic function, e.g. g:,[l1;].
So if we want to perform any other map apart from , during the zipping operation, I would redefine g.
However, '[g;l2] does not give me the expected list output but returns func
The question is: how do I apply arbitrary maps during the zipping operation? E.g. how do I do something like l1 f' l2 where in the example f:, but in general f some dyadic operator on to list items?
Thanks for the help
how do I apply arbitrary maps during the zipping operation?
Like this:
q)f:{x+y}
q)f'[10*x;x:til 5]
0 11 22 33 44
If you like the infix notation, you can also do
q)(10*x) f' til 5
0 11 22 33 44
Note that '[g;l1] is a composition. If you want to create a projection, do
q)g:,'[l1;]
q)g l2
"a1b1"
"a2b2"
Related
Given:
a:10 20 30
How do I get:
"102030"
?
I can only find:
`c$a
But that's definitely not it.
In q there is:
string 42
which returns:
"42"
but I cannot find the equivalent in k.
In K required statement will look like:
k) ,/$a
Where ,/ is raze equivalent and $ is string function equivalent.
There are not many good K tutorials. But I found last one created by Shakti is quite complete, though it may be not fully compatible with Kx's K
I'm able to achieve desired result from below code but by using lambda inside lambda:
Code:
.up.dic:`a`b!(`ab`cd;`ef`gh);
sd:2019.01.14;
ed:2019.01.15;
({[m;d]{[m;d] 0N!m,d;}[m]#'d}#'raze .up.dic)[;sd+til(ed-sd)+1]
Result:
(`ab;2019.01.14)
(`ab;2019.01.15)
(`cd;2019.01.14)
(`cd;2019.01.15)
(`ef;2019.01.14)
(`ef;2019.01.15)
(`gh;2019.01.14)
(`gh;2019.01.15)
Is there a way that we can get rid of inner lambda and pass each dict element with each date
{[m;d] 0N!m,d;}[m]#'d}/Can we get rid of this
Tried lot of things like each, /: before [;sd+til(ed-sd)+1] in outer lambda, but it resulted in length or type error.
You can use cross
q) raze[.up.dic] cross sd+til(ed-sd)+1
`ab 2019.01.14
`ab 2019.01.15
`cd 2019.01.14
`cd 2019.01.15
`ef 2019.01.14
`ef 2019.01.15
`gh 2019.01.14
`gh 2019.01.15
Using Adverbs for custom function:
If you want to do other operations on each pair, you could use a combination of each-left and each-right and define your function to perform the operation on each pair.
For example, join operation using the custom defined function.
q) my_func:{x,y} / x will be item from dict and y will be date from date list
q) raze raze[.up.dic] my_func/:\: sd+til(ed-sd)+1
How to overload an operator in functional amend?
s:string (`a1`b2`c3)
b:string til 2
using functional amend with , gives
q)#[s;0 2;,;b]
("a10";"b2";"c31")
I want to overload the , (append) to prefix the content of list b to list a like :
("0a1";"b2";"1c3")
You need to use a custom function {y,x} instead if , to achieve this
#[s;0 2;{y,x};b]
("0a1";"b2";"1c3")
Please note that here , is a dyadic function; Any other dyadic function e.g. {y,x} can be used in functional amend with valance 4.
The general format of functional amend is following, where f is dyadic function
#[L;I;f;y]
q)#[1 2 3 4 ;1 3;*;5 ] // * is dyadic function {x*y}
1j, 10j, 3j, 20j
and when f is monadic function
#[L;I;f]
q)#[1 2 3 4 ;1 3;neg ]
1j, -2j, 3j, -4j
How do i passed 2 variables to a lambda function, where x is a number and y is a symbol.
I have written this, but it wouldn't process
{[x;y]
// some calculation with x and y
}
each ((til 5) ,\:/: `a`b`c`d`f)
It seems to be complaining that i am missing another arg.
Here's an example that I think does what you're looking for:
q){string[x],string y}./: raze (til 5) ,\:/: `a`b`c`d`f
The issue with your example is that you need to raze the output of ((til 5) ,\:/: `a`b`c`d`f) to get your list of 2 inputs.
Passing a list of variables into a function is accomplished using "." (dot apply) http://code.kx.com/q/ref/unclassified/#apply
.e.g
q){x+y} . 10 2
12
In my example, I've then used an "each right" to then apply to each pair. http://code.kx.com/q/ref/adverbs/#each-right
Alternatively, you could use the each instead if you wrapped the function in another lamda
q){{string[x],string y} . x} each raze (til 5) ,\:/: `a`b`c`d`f
Instead of generating a list of arguments using cross or ",/:\:" and passing each of these into your function, modify your function with each left each right ("/:\:") to give you all combination. his should take the format;
x f/:\: y
Where x and y are both lists. Reusing the example {string[x],string y};
til[5] {string[x], string y}/:\:`a`b`c`d
This will give you a matrix of all combinations of x and y. If you want to flatten that list add a 'raze'
I have this function f
f:{{z+x*y}[x]/[y]}
I am able to call f without a 3rd parameter and I get that, but how is the inner {z+x*y} able to complete without a third parameter?
kdb will assume, if given a single list to a function which takes two parameters, that you want the first one to be x and the remainder to be y (within the context of over and scan, not in general). For example:
q){x+y}/[1;2 3 4]
10
can also be achieved by:
q){x+y}/[1 2 3 4]
10
This is likely what's happening in your example.
EDIT:
In particular, you would use this function like
q){{z+x*y}[x]/[y]}[2;3 4 5 6]
56
which is equivalent to (due to the projection of x):
q){y+2*x}/[3 4 5 6]
56
which is equivalent to (due to my original point above):
q){y+2*x}/[3;4 5 6]
56
Which explains why the "third" parameter wasn't needed
You need to understand 2 things: 'over' behavior with dyadic functions and projection.
1. Understand how over/scan works on dyadic function:
http://code.kx.com/q/ref/adverbs/#over
If you have a list like (x1,x2,x3) and funtion 'f' then
f/(x1,x2,x3) ~ f[ f[x1;x2];x3]
So in every iteration it takes one element from list which will be 'y' and result from last iteration will be 'x'. Except in first iteration where first element will be 'x' and second 'y'.
Ex:
q) f:{x*y} / call with -> f/ (4 5 6)
first iteration : x=4, y=5, result=20
second iteration: x=20, y=6, result=120
2. Projection:
Lets take an example funtion f3 which takes 3 parameters:
q) f3:{[a;b;c] a+b+c}
now we can project it to f2 by fixing (passing) one parameter
q) f2:f3[4] / which means=> f2={[b;c] 4+b+c}
so f2 is dyadic now- it accepts only 2 parameters.
So now coming to your example and applying above 2 concepts, inner function will eventually become dyadic because of projection and then finally 'over' function works on this new dyadic function.
We can rewrite the function as :
f:{
f3:{z+x*y};
f2:f3[x];
f2/y
}