The iterators (earlier known as adverbs) are native higher-order operators: they take iterables as arguments and return derived functions.
An iterable is a q object that can be indexed or applied to arguments: a function (operator, keyword, lambda, or derived function), a list (vector, mixed list, matrix, or table), a process handle, or a dictionary.
Iterators are the primary means of iterating in q.
For example, the iterator Over (written
/) uses an iterable to reduce a list or dictionary.
q)+/[2 3 4] /reduce 2 3 4 with + 9 q)*/[2 3 4] /reduce 2 3 4 with * 24
Over is applied here postfix, with
+ as its argument.
The derived function
+/ returns the sum of a list;
*/ returns its product.
(Compare map-reduce in some other languages.)
Each Prior, Over, and Scan applied to binary iterables derive functions with both unary and binary forms.
q)+/[2 3 4] / unary 9 q)+/[1000000;2 3 4] / binary 1000009
Like all functions, the iterators can be applied with bracket notation. But they can also be applied postfix, and almost always are.
q)'[count][("The";"quick";"brown";"fox")] / ' applied with brackets 3 5 5 3 q)count'[("The";"quick";"brown";"fox")] / ' applied postfix 3 5 5 3
Only iterators can be applied postfix.
Postfix returns an infix
Regardless of its rank, a function derived by postfix application is always an infix.
To apply an infix derived function in any way besides infix, you can use bracket notation, as you can with any function.
q)1000000+/2 3 4 / variadic function applied infix 1000009 q)+/[100000;2 3 4] / variadic function applied binary with brackets 1000009 q)+/[2 3 4] / variadic function applied unary with brackets 9 q)txt:("the";"quick";"brown";"fox") q)count'[txt] / unary function applied with brackets 3 5 5 4
If the derived function is unary or ambivalent, you can also parenthesize it and apply it prefix.
q)(count')txt / unary function applied prefix 3 5 5 4 q)(+/)2 3 4 / variadic function appled prefix 9
Six glyphs are used to denote iterators. Some are overloaded.
- in bold type derive uniform functions;
- in italic type, variadic functions.
Subscripts indicate the rank of the iterable; superscripts, the rank of the derived function. (Ranks 4-8 follow the same rule as rank 3.)
||₁ Case; Each|
||₂ Each Left ²|
||₂ Each Right ²|
||₁ Each Parallel ¹ ; ₂ Each Prior ¹ ²|
||₁ Converge ¹ ; ₁ Do ² ; ₁ While ² ; ₂ Reduce ¹ ² ; ₃ Reduce ³|
||₁ Converge ¹ ; ₁ Do ² ; ₁ While ² ; ₂ Accumulate ¹ ² ; ₃ Accumulate ³|
Over and Scan, with iterables of rank >2, derive functions of the same rank as the iterable.
The overloads are resolved according to the following table of syntactic forms.
Two groups of iterators¶
There are two kinds of iterators: maps and accumulators.
distribute the application of their iterables across the items of a list or dictionary. They are implicitly parallel.
apply their iterables successively: first to the entire (left) argument, then to the result of that evaluation, and so on. With iterables of rank ≥2 they correspond to forms of map reduce and fold in other languages.
A derived function, like any function, can be applied by bracket notation. Binary derived functions can also be applied infix. Unary derived functions can also be applied prefix. Some derived functions are variadic and can be applied as either unary or binary functions.
This gives rise to multiple equivalent forms, tabulated here.
Because all functions can be applied with bracket notation, to simplify, such forms are omitted here in favour of prefix or infix application.
f'[x] is valid, but only
(f')x is shown here.
(Iterators are applied here postfix only.)
The mnemonic keywords
scan are also shown.
||Reduce||reduce a list or lists|
||Accumulate||scan a list or lists|
d: data gg: iterable, rank ≥2 int: int vector n: int atom ≥0 ff: iterable, rank ≥1 t: test iterable f: unary iterable y: list g: binary iterable x: list