Blocks
Block → Object
Blocks are the bread and butter of Atomo. They represent chunks of unevaluated code - sort of like lists of expressions, but with a reference to the context in which they were evaluated.
They are first-class values and can be passed around, evaluated, torn apart, created programmatically, sent between processes, etc.
Block new: expressions in: context → Block | expressions is-a?: List
Returns a new Block
with expressions
as its contents and context
as its context.
b call → any | b is-a?: Block
Returns the result of evaluating each expression in b
, in order, with a new toplevel object that delegates to the block's context. call
ing a block does not affect its context (unless it is a block returned by in-context
).
Sending call
to an empty block raises a @no-expressions
error.
b call: (... args) → any | b is-a?: Block
Like call
, but passes some extra arguments along to b
. Any number of arguments may be passed to the block; it can use however many it wants.
Sending call:
to an empty block raises a @no-expressions
error.
Example:
> { a b | a + b } call: (1, 4) 5 > { a | a + 1 } call: 1 2 > { 1 + 1 } call: 42 2 > {} call: 42 ERROR: <error @no-expressions>
Note that the arguments also do not affect the outer scope:
v join: b → any | b is-a?: Block
Execute b
with v delegating-to: b context
as its toplevel object (i.e., the block's context is accessible). Returns the result of the block's execution.
Note that v
's methods take priority over the block's context.
This can be useful for executing a block in the current scope:
v join: b with: (... args) → any | b is-a?: Block
Like join:
, but with arguments passed to the block.
Note that v
's methods take priority over both the arguments and the block's context.
b in-context → is-a?: Block | b is-a?: Block
Returns an object that delegates to the original block, b
, but has its own call
and call:
methods defined. These overrides evaluate the block in its original scope, as opposed to lexically, via join:
and join:with:
.
start to: end by: diff do: b → @ok | start is-a?: Integer | end is-a?: Integer | diff is-a?: Integer | b is-a?: Block
Calls b
with each integer in the range from start
to end
inclusive, by adding diff
to the current number.
Example:
> 0 to: 5 by: 2 do: { n | ("Counting up... " .. n show) print } Counting up... 0 Counting up... 2 Counting up... 4 @ok > 5 to: 0 by: -1 do: { n | ("Counting down... " .. n show) print } Counting down... 5 Counting down... 4 Counting down... 3 Counting down... 2 Counting down... 1 Counting down... 0 @ok
b repeat → none | b is-a?: Block
Repeatedly sends call
to b in-context
. Never returns.
b repeat: n → List | b is-a?: Block | n is-a?: Integer
Returns a list containing the results of n
call
s of b in-context
.
Example:
> with-input-from: "doc/main/lists.atomo" do: { { read-line } repeat: 3 } ERROR: <error @(file-not-found: "doc/main/lists.atomo")>
Here we're reading 3 lines of text from the file "doc/main/lists.atomo"
(the source for the documentation you're reading).