next, prev, xprev¶
Immediate or near neighbors
What if just under this layer of life you could
find the old one, moving forward just the same,
and just above, what’s yet to come
— Emily Berry, Unexhausted Time
next¶
Next items in a list
next x next[x]
Where x is a list or table, for each item in x, returns the next item.
For the last item, next returns a null if x is a vector; otherwise, it returns an empty list.
For a table, the last row is null.
q)next 2 3 5 7 11
3 5 7 11 0N
q)next (1 2;"abc";`ibm)
"abc"
`ibm
`long$()
Duration of a quote:
q)quote:([]sym:`a`b`a`b;time:09:30:00 09:30:01 09:30:03 09:30:07)
q)update dur:(next time)-time by sym from quote
sym time dur
---------------------
a 09:30:00 00:00:03
b 09:30:01 00:00:06
a 09:30:03
b 09:30:07
next is a uniform function.
prev¶
Immediately preceding items in a list
prev x prev[x]
Where x is a list or table, for each item, returns the previous item.
For the first item, prev returns a null if x is a vector; otherwise, it returns an empty list.
For a table, the first row is null.
q)prev 2 3 5 7 11
0N 2 3 5 7
q)prev (1 2;"abc";`ibm)
`long$()
1 2
"abc"
Shift the times in a table:
q)update time:prev time by sym from quote
sym time
------------
a
b
a 09:30:00
b 09:30:01
prev is a uniform function.
xprev¶
Nearby items in a list
x xprev y xprev[x;y]
Where x is a long atom and y is a list, returns, for each item of y, the item x positions before it.
The first x items of the result are null, empty, or blank as appropriate.
There is no xnext function.
Use xprev with a negative left argument instead.
q)2 xprev 2 7 5 3 11
0N 0N 2 7 5
q)-2 xprev 2 7 5 3 11
5 3 11 0N 0N
q)1 xprev "abcde"
" abcd"
xprev is a right-uniform function.