On this eighth day of Nockmas (and first of the New Year), we look at opcode 7, Compose.
Opcode 7: Compose
Syntax
*[a 7 b c] *[*[a b] c]
Explanation
Opcode 7 provides function composition. It evaluates b against the subject, then uses that result as the subject for evaluating c. This produces a classic “pipe” pattern.
- Evaluate
bagainst subject to produce an intermediate noun. - Evaluate
cagainst that intermediate to produce the final product.
When reading it, think of it as “then” or “pipe”. The first formula changes the subject and the second operates on the new subject.
Again, our subject:
:subject 42
Output:
Subject set to: 42
There are two common patterns for using opcode 7. The first is simply to chain multiple computations together in sequence:
*[x [7 transform1 [7 transform2 transform3]]]
In the most simple case; increment, then increment again:
[7 [4 0 1] [4 0 1]]
Output:
44
Or continuing the chain: Increment, then increment again, then check if it's a cell:
[7 [4 0 1] [7 [4 0 1] 3 0 1]]
Output:
1
Make a cell of the subject repeated, then check if it's a cell:
[7 [[0 1] [0 1]] 3 0 1]
Output:
0
The second is to set up a context for a computation by first extending the subject with additional data, then performing the computation in that extended context:
*[input [7 [build-context] [main-computation]]]
(This is typically paired with opcode 8 to build the context.)
Revisiting our basic subject:
:subject 42
Output:
Subject set to: 42
We then call a Nock 7 to build a context and run a computation:
[7 [[0 1] [1 41]] [5 [0 2] [4 0 3]]]
Output:
0
Think through the formula above; What is it doing?
Having trouble? Shoot a note to @urbit or join us on Tlon Messenger in the [battery payload] group.
Join us tomorrow when we cover Nock 8, Extend.
12 Days of Nockmas is an exploration of Nock, Urbit's instruction set architecture. This ISA is used by both Urbit and Nockchain, has interpreters written in many languages, with production versions in both C and Rust. The content of this series is drawn from the Nock language site. Visit the site for interactive code examples and more Nock related content.