Flows and Subflows

In Koru, flows are used to define the sequence of events and their possible outcomes. Flows can be nested to create complex event sequences.

The three examples below are one file, built up. Each adds to the one before it, so the second and third do not stand on their own — copied alone, the second gets KORU040: unknown tor 'main:fetch', because fetch is declared in the first. Put them in a single main.k and it compiles and runs.

Two arrows appear below and they do different things. => produces an outcome — it says which of the event’s declared branches this arm resolves to. |> pipes into a call — it hands the branch’s payload to something else. An arm that ends a flow produces; an arm that continues it pipes.

Flows

A flow invokes an event and handles every declared branch:

import std/io

tor fetch { id: i64 }
| found string
| missing

fetch = if(id == 1)
| then => found "Lars"
| else => missing

fetch(id: 1)
| found name |> std/io:print.ln("Found {{ name:s }}")
| missing |> std/io:print.ln("Not found")

fetch = if(id == 1) is the implementation: if has a then and an else branch, and each arm produces one of fetch’s own outcomes. The invocation underneath then handles both of them. Run it and it prints:

Found Lars

Subflows

A subflow implements one event in terms of another. The outer event’s branches are produced from the inner event’s outcomes:

tor describe { id: i64 }
| named string
| unknown

describe = fetch(id)
| found name => named name
| missing => unknown

Invoking Events

Events are invoked with flows:

describe(id: 2)
| named text |> std/io:print.ln(text)
| unknown |> std/io:print.ln("nobody")

With all three in one file, koruc main.k && ./a.out prints:

Found Lars
nobody

id: 2 is not 1, so fetch produces missing, describe turns that into unknown, and the last arm prints nobody.