2

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.

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

3 Comments

i think the dot apply did the trick! Additionally i found out i can use "cross" to achieve the above
is it possible to parallelize the "./:" using "peach" ?
Don't think so, swapping the each for peach will work for the second example. Not a great example function for parallelization as it could be vectorised first then once optimized by vectorizing need to determine is it worth overhead of splitting up and joining back together. .e.g vecotrize my example would be {string[x],'string y} . flip 100000#raze (til 5) ,\:/: `a`b`c`d`f (Added the "100000#" for more test data )
2

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'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.