Skip to content

Control Flow

Kura is a functional-first language.

Control flow is treated as a series of expressions that evaluate to a value, rather than statements that mutate state.

If statements in Kura behave like your standard if in other languages:

fn get-text(count: Int): String =>
if count > 10 {
"Too much"
} else {
"Keep going"
}
// you can also do if/else-if:
fn get-text(count: Int): String =>
if count > 10 {
"Too much"
} else if count > 5 {
"Almost there"
} else {
"Keep going"
}

Pattern matching via the match keyword is Kura’s primary mechanism for conditional branching and data destructuring.

A match block must be exhaustive; the compiler will reject code if a potential runtime value is not explicitly handled.

Matches can inspect regular primitives, structural records, and algebraic variants.

When matching a variant with a payload, the internal properties are extracted and bound to local immutable identifiers.

type Position = { x: Int, y: Int }
type Message = Click(Position) | Tick
fn update(message: Message, count: Int): Int =>
match message {
Click(position) => count + 1,
Tick => count,
_ => 0, // Catch-all wildcard
}

Branch arms can access deeply nested data configurations, such as navigating monadic containers like Optional<T>.

This allows complex extraction logic to be written cleanly without cascading nested conditional checks.

type Optional<T> = Some(T) | None
fn nested(m: Optional<Message>): Int =>
match m {
Some(Click(p)) => 1, // Matches if container has a value AND that value is a Click
Some(other) => 2, // Matches any other wrapped Message variant
None => 3, // Matches the absence of a value
}

You can use _ for a catch-all wildcard that will match any pattern shape, but not bind the value to a usable identifier:

fn nested(m: Optional<Message>): Int =>
match m {
Some(Click(p)) => 2, // Matches any other wrapped Message variant
None => 3, // Matches the absence of a value
_ => 0 // Matches anything else
}

when is an if whose else is None, which is what a view wants for content that is only sometimes there:

when model.loaded { #text("Ready") } // a Node, or None

Kura eliminates traditional imperative loops (while, for(i=0;...)) in favor of unified collection expressions.

Loops operate on sequences (Seq<T>) and feature inline condition filtering and folding primitives.

Loops use an item extractor followed by an optional if condition guard.

Elements that evaluate to false in the guard block are omitted from processing.

fn sum(items: Seq<Number>): Number =>
for _, item in items if item > 0 fold total = 0 => total + item

The bindings of a loop follow Go. With one name, the name is the index; with two, they are the index and the element; _ skips one:

for i in items => i // the indexes
for i, item in items => i * item // the index and the element
for _, item in items => item // only the elements

The element may be destructured: for _, { x, y } in points => x + y.

Between square brackets, a loop without fold reads as a Seq comprehension, with the body in front:

let plus-one = [ x + 1 for _, x in items ]
let big = [ x for _, x in items if x > 10 ]

To aggregate state across a sequence, attach a fold block to the loop expression:

fn sum(items: Seq<Number>): Number =>
for _, item in items if item > 0 fold total = 0 => total + item

The fold keyword instantiates a temporary accumulator identifier (total in the example above) bound to a starting seed value (0).

For every item that passes the if constraint, the reducer expression evaluates. The return value becomes the new value of the accumulator for the subsequent element.

Once the sequence stream completes, the loop expression terminates and returns the final value of the accumulator directly.