4 ms·
Other people in the comments have mentioned you can use functions to achieve some of the benefits of the {blocks}. I wanted to point out how q/kdb handles this
by maest 4y ago
Other people in the comments have mentioned you can use functions to achieve some of the benefits of the {blocks}.
I wanted to point out how q/kdb handles this, because I think it's quite nice.
In kdb, blocks are how you define functions.
{x+1};
That is a function which takes your argument, adds one to it and returns it. (x is the default name for the function argument, another lovely piece of design).
If you want to have a named function, just assign it to a variable:
my_inc: {x+1};
Now you can call my_inc(1) and get back 2.
Light, consistent and reusable language design, very nice. No need to have two ways of defining functions (e.g. the needless separation of def and lambdas in Python).
The upshot of this is that you get these block constructs for free:
myvar_a: 1;
myvar_b: 2;
my_top_level_var: {
//less important work
}[];
(The one downside is that you need to call the function with the [] brackets)
- surprisetalk 4y agoWow, super cool design decision! Can you give multiple named variables? How would you write a function like this? const f = (a,b,c) => { return a * b + c } Also, is this just q/kdb, or does this also apply to K?
- maest 4y agoThe default variable names go all the way up to z. So you I would write your function as: f:{z+y*x} (k is evaluated strictly right to left - another design decision that I quite like -, so I had to move the variables around in the expression) If you wish, you can provide your own variable names as follows: f:{[a;b;c]c+b*a} This works in k and q (q is largely k with some nice-to-have functions defined on top).
- leprechaun1066 4y agoK4 is the implementation language of q, kdb is the database part of the language. Most q is just named utility functions written in K4. There isn't really much difference in what the machine does with them under the covers, but when stuck on a problem talking about q is more likely to get help than k. You can provide up to 8 named inputs in a function definition. q example for running sum of 8 inputs: q)f:{[a;b;c;d;e;f;g;h]sums a,b,c,d,e,f,g,h} same in K4: q)\ f:{[a;b;c;d;e;f;g;h]+\a,b,c,d,e,f,g,h}