Index Amend and Apply
Operators @
and .
-
@
and.
build on the functionality provided by square bracket notation -
If
x
is a data structure,x[y]
means 'Index'
f
is a function, f[y]
means 'Apply':
* @
and .
provides more powerful, flexible and alternatives to square bracket notation
* @
is used for single-dimensional lists
* .
is used for nested lists
Index using @
and .
- If list is single-dimensional list and index is 2:
then same can be acheived using
@
operator as: - If list is nested list and row is 1 and column is 2:
then same can be acheived using
.
operator as:
Amend/Apply using @
- Previously we used
:
operator to amend items in a single-dimensional list
@
for single-dimensional list, however we need to give list name as symbol for changes to be permanent
* we can also apply mathematical operator to list
q)list
4 9 2 7 0 1 9 2 1 8
q)@[list;3;+;66]
4 9 2 73 0 1 9 2 1 8
q)list:(8 4 3;9 8 76;7 8)
q)list
8 4 3
9 8 76
7 8
q).[list;1 2;+;100]
8 4 3
9 8 176
7 8
@
vs []
- square brackets are easy to read for simple use cases, but there are cases where
@
can be used where[]
cannot []
can only be used with named variables but@
can be used without named variables, it can also be used with result of previous operation@
has a wider range of operations - square bracket allows us to append to a dictionary itembutq)d:(`a`b`c)!(1 2;3 4 5;6 7 8 9) q)d a| 1 2 b| 3 4 5 c| 6 7 8 9 q)d[`c],:10 q)d a| 1 2 b| 3 4 5 c| 6 7 8 9 10
@
also allows us to prepend-
we can use
@
with non-dyadic operators -
to use a function taking more than 2 arguments, we create a monadic projection by fixing the other arguments inside the 3rd part of
@
expression
Advanced use of @
index
- Setup 2 structures:
- l is a mixed list of symbols and integers
- m is a mapping of symbol to integers
- we can apply the mapping at the indexes of the symbols
Using .
with database tables
-
we can use
.
to index into a table -
.
also allows us to update entire object at once by passing an empty set of indexes - this allows us to amend each element of the list
Convert @
to .
- we can convert an
@
statement to.
statement as@
is a speacial case of.
Apply using @
and .
- we have seen that when
f
is a function thenf[x]
means apply []
can be replaced with@
(for single argument)and.
(for multiple arguments)- Let's say we have list of lists with varying length and we want to flip it into a table - we can't do it straight away as lenghts are not same
- instead we can use
@
to fill the lists with0N
to make them of same lenght and then flip it into a table
Using @
on database tables
- we can use
@
as simple version of functional select - here column name is used as symbol so it can be passed programatically