Control flow
Everything here is an expression: if, match, while, for have a value.
if
n = 7
if n > 5 { "many" } else { "few" } # → "many"
size = if n > 10 { "L" } else if n > 5 { "M" } else { "S" }
size # → "M"
if false { 1 } # → nilelse if is two words, there is no elsif. Only nil and false are falsy.
code = 250
if code in 200..<300 { "ok" } else { "error" } # → "ok"while
i = 0
while i < 3 { i += 1 }
i # → 3for
for x in … walks an array, a range, a string (by character) and a dict (in [key, value] pairs).
for x in 1..3 { println(x) }
for ch in "ok" { println(ch) }
for k, v in {a: 1, b: 2} { println("${k}=${v}") }1
2
3
o
k
a=1
b=2The index along with the element — each_with_index:
["Ann", "Boris"].each_with_index { (name, i) -> println("${i}. ${name}") }0. Ann
1. Borisbreak and next
next is continue. Both take an optional value and work both in loops and in closures like each/map.
sum = 0
for x in 1..10 {
next if x.even
break if x > 7
sum += x
}
sum # → 16
found = for x in [3, 8, 12] { break x if x > 5 }
found # → 8
[1, 2, 3].map { next 0 if it == 2; it } # → [1,0,3]Modifiers
if and while can follow an expression.
xs = []
xs.push(1) if xs.empty
xs # → [1]
i = 1
i *= 2 while i < 100
i # → 128fn check(age) {
return "no age" if age == nil
age >= 18 ? "adult" : "child"
}
check(nil) # → "no age"
check(20) # → "adult"match
Branches are tried top to bottom, the first that fits wins. The value of match is the value of the branch; if none fits and there is no else, it is nil.
fn kind(x) {
match x {
0 -> "zero"
1, 2, 3 -> "few"
in 4..9 -> "digit"
else -> "many"
}
}
[0, 2, 7, 100].map { kind(it) } # → ["zero","few","digit","many"]Branches are separated by a line break or ;:
match "b" { "a" -> 1; "b" -> 2; else -> 0 } # → 2Pattern kinds
| Pattern | Fits when |
|---|---|
1, "yes", any expression | x == pattern |
/re/ | x ~ /re/; x is a string or nil, otherwise an error |
in 1..9, in ["a", "b"] | x is in the range, the array, the dict's keys, is a substring |
[a, b], [0, *rest] | x is an array of the right length; names are bound |
{kind: "paid", total} | x is a dict with these keys; names are bound |
Money {amount} | x is a Money record |
if condition | the condition is true |
a, b, c | any of the patterns (of one kind) |
else | always, last branch only |
Regular expressions
fn intent(text) {
match text.lower {
/hello|hi/ -> "greet"
/^\d+$/ -> "number"
else -> "other"
}
}
intent("Hello!") # → "greet"
intent("42") # → "number"Guard after a pattern
n = 7
match n { in 1..10 if n.odd -> "small odd"; else -> "other" } # → "small odd"Arrays
Names in a pattern are bound, literals are compared, *rest takes the remainder.
fn route(parts) {
match parts {
["GET", "users"] -> "list"
["GET", "users", id] -> "user ${id}"
["POST", *rest] -> "create: ${rest.join("/")}"
[] -> "empty"
else -> "404"
}
}
route(["GET", "users", "7"]) # → "user 7"
route(["POST", "a", "b"]) # → "create: a/b"
route([]) # → "empty"
route("x") # → "404"Dicts
The pattern is open: extra keys do not get in the way, named ones are required. A missing key moves on to the next branch.
fn describe(ev) {
match ev {
{kind: "paid", total: t} if t > 0 -> "payment ${t}"
{kind: "refund", data: {reason}} -> "refund: ${reason}"
{age: in 18..65} -> "adult"
{} -> "some dict"
else -> "not a dict"
}
}
describe({kind: "paid", total: 500}) # → "payment 500"
describe({kind: "refund", data: {reason: "defect"}}) # → "refund: defect"
describe({age: 30}) # → "adult"
describe({kind: "paid", total: 0}) # → "some dict"
describe(5) # → "not a dict"Without a subject
match with no value replaces an else if chain: each branch is a condition.
t = 31
weather = match {
t > 30 -> "hot"
t > 15 -> "warm"
else -> "cold"
}
weather # → "hot"Braces in the header
In an if/while/for/match header the first { opens the body. Put a closure in a condition in parentheses:
xs = [1, 2, 3]
if (xs.any { it > 2 }) { "yes" } else { "no" } # → "yes"