Skip to content

Extenders

The extenders (earlier known as adverbs) are native higher-order operators: they take maps as arguments and return derived functions, known as extensions.

They are the primary means of iterating.

For example, the extender Over (written /) uses a map 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 extension +/ returns the sum of a list; */ returns its product. (Compare map-reduce in some other languages.)

Ambivalence

Each Prior, Over, and Scan applied to binary maps return extensions with both unary and binary forms.

q)+/[2 3 4]           / unary
9
q)+/[1000000;2 3 4]   / binary
1000009

Ambivalence

Postfix application

Like all functions, the extenders can be applied with bracket notation. But they can also be applied postfix, and almost always are.

q)'[count][("The";"quick";"brown";"fox")]
3 5 5 3
q)count'[("The";"quick";"brown";"fox")]
3 5 5 3

Only extenders can be applied postfix.

Postfix returns an infix

Regardless of its rank, an extension formed by postfix application is always an infix.

To apply an infix extension in any way besides infix, you can always use bracket notation.

q)1000000+/2 3 4          / ambivalent extension applied infix
1000009
q)+/[100000;2 3 4]        / ambivalent extension applied binary with brackets
1000009
q)+/[2 3 4]               / ambivalent extension applied unary with brackets
9
q)txt:("the";"quick";"brown";"fox")
q)count'[txt]             / unary extension applied with brackets
3 5 5 4

If the extension is unary or ambivalent, you can also parenthesize it and apply it prefix.

q)(count')txt             / unary extension applied prefix
3 5 5 4
q)(+/)2 3 4               / ambivalent extension appled prefix
9

Glyphs

Six glyphs are used to denote extenders. Some are overloaded.

Extenders in bold type yield uniform extensions; in italic type, ambivalent extensions.

Subscripts indicate the rank of the map; superscripts, the rank of the extension.

glyph operator/s
' Case; Compose; Each
\: Each Left ²
/: Each Right ²
': Each Parallel ¹ ; ₂ Each Prior ¹ ²
/ ₁ Over: Converge ¹, Do ², While ² ; ₂ Reduce ¹ ² ; ₃ Reduce ³
\ ₁ Scan: Converge ¹, Do ², While ² ; ₂ Scan ¹ ² ; ₃ Scan ³

For Over and Scan, maps of rank >2 yield extensions of the same rank as the map.

The overloads are resolved according to the following table of syntactic forms.

Forms

Any extension, like any function, can be applied by bracket notation. Binary extensions can also be applied infix. Unary extensions can also be applied prefix. Some extensions are ambivalent 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. For example, f'[x] is valid, but only (f')x is shown. (Extenders are applied postfix only.)

The mnemonic keywords each, over, peach, prior and scan are also shown.

argument
rank
syntax name semantics
1 int'[x;y;…] Case select from [x;y;…]
1
2
3+
(f')x, f each x
x g'y
ff'[x;y;z;…]
Each apply f to each item of x
apply g to corresponding items of x and y
apply ff to corresponding items of x, y, z
2 x g\:d Each Left apply g to d and items of x
2 d g/:y Each Right apply g to d and items of y
1 (f':)x, f peach x Each Parallel apply f to items of x in parallel tasks
2 (g':)y,
g prior y,
d g':y
Each Prior apply g to (d and) successive pairs of items of y
1 (f/)d, (f\)d Converge apply f to d until result converges
1 n f/d, n f\d Do apply f to d, n times
1 t f/d, t f\d While apply f to d until t of result is 0
1
2
3+
(g/)y, g over y
d g/y
gg/[d;y;z;…]
Reduce reduce a list or lists
1
2
3+
(g\)y, g scan y
d g\y
gg\[d;y;z;…]
Scan scan a list or lists

Key:

d:   data            gg: map, rank ≥2    
int: int vector      n: int atom ≥0 
ff:  map, rank ≥1    t: test map
f:   unary map       y: list
g:   binary map      x: list

The binary extender Compose is not tabulated above.