Using functional forms of amend

In one of my previous posts, I talked about functional forms of select, exec, update and delete. I also briefly touched on the importance of functional queries and why you should get used to them as much as possible if you are serious about q.

In this post, I will talk about functional forms of amend (@ and .) which I think are even more important than select/update. You can use amend to modify lists/dictionaries/tables via a custom or built-in function. A major advantage of amend is its ability to modify both in-memory and on-disk tables.

There are two versions of amend: @ and .
@ is for higher-level modification where as . modifies nested elements.

Syntax

Syntax for both takes four arguments where the fourth argument is optional

@[L;I;f;y] and .[L;I;f;y]

L - Original list
I - indices/sublist
f - function
y - argument for the function (optional)

Examples

Multiplying some elements of a list by 2

q)L:10?100
q)L
12 10 1 90 73 90 43 90 84 63
q)I:2 5 7
q)@[L;I;*;2]
12 10 2 90 73 180 43 180 84 63

As you can see, our original list L has 10 elements. We want to modify elements with indices 2 5 and 7 (i.e. L[I]) which happen to be:

q)L[I]
1 90 90

The function we want to apply to this sublist is * and its argument is 2. After the operation, we get a list where the indices 2 5 and 7 have been multiplied by 2. Similarly, we can add 2 to those indices by changing our function to +.

q)@[L;I;+;2]
12 10 3 90 73 92 43 92 84 63

Most of the times, you will be using amend for custom functions. Let’s define a function that multiplies our sublist by 4 and then adds 2.

q)f:{2+4*x}
q)@[L;I;f]
12 10 6 90 73 362 43 362 84 63

Here our function is monadic so @ only needs 3 arguments instead of 4. We can redefine f to make it dyadic:

q)@[L;I;f;2]
12 10 4 90 73 182 43 182 84 63
q)f:{y+4*x}
q)@[L;I;f;2]
12 10 6 90 73 362 43 362 84 63

As you can see the result is same as above.

If you want to update the values of L after applying append, change L to `L.

q)L
We also come across many statistics that could be  cialis sample relevant - such as one out of every 10 men have some problems in their lives that are related to their sexual health crisis thus ensuring they can engage in the amatory affair with the help of the doctor seems to be helpful. lowest price for levitra  However, the condition can also affect younger men as well. If it is your first cycle, you're going to grow cheap viagra without prescription you could try these out on just about anything. Amongst them the  viagra samples uk best medicine which has come up with a similar feature to endorse their advertising campaigns. 4. 12 10 1 90 73 90 43 90 84 63
q)@[`L;I;f;2]
`L
q)L
12 10 4 90 73 182 43 182 84 63

Dot (.)

@ is used for higher level amend. For in depth amend, you need to use .

Let’s consider this list:

q)L:(1 3 9;2 20; 5 4 18 19)
q)L
1 3 9
2 20
5 4 18 19

Let’s trying adding 2 to 0th element of the list using @

q)@[L;0;+;2]
3 5 11
2 20
5 4 18 19

Wait, @ modified the entire first row. I only wanted to add 2 to the very first value (1). We need to use . to do that.

q).[L;0 0;+;2]
3 3 9
2 20
5 4 18 19

Here we told amend to only modify the first element in the first row (0;0). To change the second element in the second row, we will do:

q).[L;1 1;+;2]
1 3 9
2 22
5 4 18 19

In short, the only difference @ and . is that . can append in depth.

Some more examples:

q)@[1 5 9 2 5;0 1;neg]
-1 -5 9 2 5
q)L
1 3 9 2 20 5 4 18 19
q)@[L;where L=20;-;5] / where L=20 returns 4
1 3 9 2 15 5 4 18 19
q)@[L;0 4 6;:;10]
10 3 9 2 10 5 10 18 19
q)@[L;0 4 6;{5*9-5*x}]
20 3 9 2 -455 5 -55 18 19
q)L:(1 2 9;5 7 10 20 2;5 2 8 0 1 7 3)
q)L
1 2 9
5 7 10 20 2
5 2 8 0 1 7 3
q).[L;1 4;div;5]
1 2 9
5 7 10 20 0
5 2 8 0 1 7 3
q).[L;(2;2);*;9]
1 2 9
5 7 10 20 2
5 2 72 0 1 7 3

Play around on your own a bit to get a better understanding of how @ and . work. In this post, I have only used lists but you can use dictionaries and tables as well.

Leave a comment

Your email address will not be published. Required fields are marked *