Blocks are kind of like functions. The store a sequence of expressions to be evaluated later. They are a value, so like any value they need to be bound to a name to be stored.
let first = {
print 'Hello, world!'
}
Blocks can be called
first()
# Hello, World!
Blocks can receive values from their environment
let greet = {
in name
print "Hello, ${name}!"
}
print greet('Mike')
# 'Hello, Mike!'
Blocks can give output to the environment
let add = {
in a, b
out a + b
}
add(1, 2)
# 3
Blocks can give have multiple outputs
let parseColor = {
in hexCode # String like '#RRGGBB'
let r, g, b
r = int(hexCode[1:2], 16)
g = int(hexCode[3:4], 16)
b = int(hexCode[5:6], 16)
out r, g, b
}
parseColor('#ff8800')
# [255, 136, 0]
Blocks can have structured outputs
let getNames = {
in id
let user = getUser(id)
out first => user.first_name, last => user.last_name
}
first_name from getNames(42)
# 'Douglas'
Blocks can be bound by name
let say {
in to, msg
out "${to}: ${msg}"
}
say('msg' => 'Hello', 'to' => 'Mike')
# Mike: Hello
say('to' => 'Sarah', 'msg' => 'Hello')
# Sarah: Hello
Blocks are values
let do = {
in func, value
out func(value)
}
let isEven = {
in value
out value % 2 == 0
}
do(isEven, 10)
# true
alternately you could have:
etc.