Back to Dictionaries

Dictionaries

Step Dictionary

Skip to 2 practice questions

A step dictionary is a dictionary with the sorted attribute applied to the whole dictionary, giving it special lookup behaviour for keys that don't exist exactly.

Syntax

ticks:`s#0 1 10 100!0.01 0.05 0.10 0.50

The `s# must be applied to the entire keys!values expression — not just the key vector. The keys must already be sorted ascending, or applying `s# fails with 's-fail.

Lookup Behaviour

  • If the key exists, its value is returned as normal.
  • If the key does not exist, the value for the greatest key ≤ the lookup key is returned.
ticks[10]   / 0.1   — exact match
ticks[12]   / 0.1   — falls between 10 and 100, uses the 10 band
ticks[150]  / 0.5   — falls in the 100+ band

Why Use One?

Step dictionaries model price-band style rules with a single lookup instead of nested $[...] conditionals — tick sizes, fee tiers, margin schedules, minimum order quantities, and similar threshold-based logic. Values don't need to be numeric; they can map ranges to symbols or categories.

prices:([] sym:`AAPL`MSFT`GOOG; px:0.75 12 150)
update tickSize:(`s#0 1 10 100f!0.01 0.05 0.10 0.50)[px] from prices
sym  px   tickSize
------------------
AAPL 0.75 0.01
MSFT 12   0.1
GOOG 150  0.5

The keys are written 0 1 10 100f because px is a float column — the lookup value and the keys must be the same type, or the lookup fails with 'type.

An equivalent result can be obtained with an as-of join (aj) against a small rules table, but the step dictionary is usually more concise for simple threshold lookups.

A Common Mistake

The `s# must wrap the whole keys!values expression, not just the key vector:

ticks:`s#0 1 10 100!0.01 0.05 0.10 0.50      / correct — step dictionary
ticks:(`s#0 1 10 100)!0.01 0.05 0.10 0.50    / wrong — ordinary dictionary with sorted keys

The second form looks identical but behaves like a normal dictionary: ticks[12] returns a float null (0n) instead of falling back to the 10 band.

Practice Questions

Test your understanding with 2 hands-on questions