flow authorize(granted: Bool, level: Int) (out: Bool) { admin = level > 0 out = rule (admin, granted) { (true, -) => true (false, true) => true (false, false) => requireGrant(granted) } } flow display(value: Text?) (out: Text) { out = rule value { null => "guest" other => value } } requireGrant(granted: Bool) (out: Bool) { check granted else Forbidden out = granted } type Line { amount: Int discounted: Bool } loop totals(lines: List) (amount: Int, discounted: Bool) line in lines { amount = sum line.amount discounted = any line.discounted } flow checkout(level: Int, granted: Bool, lines: List) (amount: Int) { allowed = authorize(granted, level) check allowed else Forbidden result_row = totals(lines) amount = result_row.amount } loop cartStats(lines: List) (discount: Int, units: Int) line in lines { discount = rule line.discounted { true => sum line.amount false => skip } units = count } flow discountTotal(orders: List>) (out: Int) { out = loop order in orders { sum loop item in order { rule item.discounted { true => sum item.amount false => skip } } } } type Refund { id: Int, limited: Bool } refundOne(order: Refund) (acc: Int?, stop: Bool) { check order.id > 0 else Invalid acc = pendingRefund(order.limited, order.id) stop = order.limited } rule pendingRefund(limited: Bool, id: Int) (out: Int?) limited { true => id false => null } flow runRefunds(orders: List) (pending: Int?) { pending = loop order in orders { foldUntil(previous = null, refundOne(order)) } }