flan/test/programs/pipe.fln

43 lines
1.2 KiB
Plaintext

;; The pipe x |> f(a) is f(x, a): the value on the left is the first argument,
;; evaluated before the others. Typed, dyn, chained, multi-line and qualified.
import shape "pkgs/shape"
import area "pkgs/area"
fn add(a: i32, b: i32) -> i32 = a + b
fn scale(a: i32, k: i32) -> i32 = a * k
fn twice(a: i32) -> i32 = a * 2
fn sub3(a: i32, b: i32, c: i32) -> i32 = a - b - c
;; Prints its argument, to show the order the arguments run in.
fn note(n: i32) -> i32
println(n)
n
fn held-or(o: i32?, d: i32) -> i32 = o ?? d
fn bump(x) = x + 1
fn main()
println(3 |> add(4))
println(3 |> twice)
println(3 |> twice())
println(1 |> add(2) |> scale(10) |> twice)
;; Below or and the arithmetic: the whole left side is piped.
println(2 + 3 |> scale(2))
let n: i32? = None
println(n ?? 4 |> twice)
println(1 == 1 |> not)
let total = 10
|> add(5)
|> scale(2)
println(total)
let other = 10 |>
sub3(1, 2)
println(other)
println(Some(7) |> held-or(0), n |> held-or(-1))
println(shape/box(3, 2) |> area/of)
println(5 |> bump |> bump)
;; The left side runs first, then the other arguments in order: 1 2 3.
println(note(1) |> sub3(note(2), note(3)))