Using the APL Key Operator ⌸ for Grouped Aggregation
The dyadic key operator ⌸ lets you partition an array by a key and apply a function to each group, enabling concise grouped aggregation without explicit loops.
17 Mar 2026, 19:45 UTC

Problem and Takeaway
You have an array and need a summary for each distinct element without writing a loop. The dyadic key operator ⌸ partitions the data by a key and applies a function to each group, delivering the result in the order the keys first appear.
How ⌸ Works
The operator takes three arguments: a key vector on the left, the data vector on the right, and a function on the right as the operand. It groups the data by each unique key in the order they first occur, then evaluates the operand with the key as its left argument and the corresponding group as its right argument. The operand therefore receives the key and the matching sub‑array.
Availability and Portability
⌸ is part of Dyalog APL’s extended set but is not part of the ISO APL2 core. Consequently, GNU APL, NARS2000, and older interpreters may raise a syntax error. If you need the operator in a multi‑interpreter environment, wrap it behind a small function that checks the interpreter version and falls back to a manual loop when ⌸ is unavailable.
Worked Example
Count the occurrences of each letter in the string ‘ABAB’ using ⌸:
⎕← 'ABAB' ⌸ {⍺,⍴⍵} 'ABCD'
Explanation of the expression:
- The left argument ‘ABAB’ supplies the keys A and B.
- The right argument ‘ABCD’ is the data to be grouped.
- The operand {⍺,⍴⍵} receives the key (⍺) and the group (⍵), then returns a two‑element vector: the key and the group’s length.
Running the command in Dyalog APL yields:
A 2
B 2
Verification steps:
- Execute the same expression in Dyalog APL and confirm that the two rows correspond to keys A and B with counts 2 each.
- Write a hand‑crafted loop that iterates over the distinct keys of ‘ABAB’ and produces the same two‑element vectors; compare the results for equivalence.
- Try the expression in another APL interpreter (e.g., GNU APL). If it raises a syntax error, ⌸ is not supported there.
Trade‑offs and Limitations
The output order follows the first occurrence of each key, not sorted order. If your downstream code expects alphabetical ordering, you must sort the result explicitly. Additionally, keys that never appear in the data produce no row; code that assumes a row for every possible key can break. Because ⌸ is interpreter‑specific, production code that must run on multiple APL versions should isolate the operator behind an abstraction layer.
Actionable Closing
When you need grouped aggregation without explicit loops, try ⌸ in Dyalog APL. First, test the operator with a simple example such as the one above. If the interpreter supports it, replace a multi‑line loop with a single ⌸ expression. If you must support other interpreters, wrap the call in a version‑check function and provide a fallback implementation. This approach gives you concise, readable code while keeping portability concerns in check.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.