Functions
Functions are values. Write a function type as fn(A) -> R. Named functions
declare their input and result types; ordered clauses share one signature.
A lambda uses ->; its parameter types can be omitted when the expected
type supplies them.
Call a named function with f(x) and a function value with f.(x).
|> passes the left-hand side as the first argument of the call on the right.
List<A> is a linked list of one element type. gale_std.stream is a lazy
Elixir stream, not a list. map and take stay lazy until to_list.
Use pipelines to compose calls, lambdas to pass behavior, and clauses to handle different input patterns. A function signature states the input and return types shared by its clauses.
Pipelines read left to right
Section titled “Pipelines read left to right”|> passes the left-hand side as the first argument of the call on the
right. scale(10) below returns 31: add five, double, add one:
fn add(n: Integer, amount: Integer) -> Integer { n + amount}
fn twice(n: Integer) -> Integer { n * 2}
pub fn scale(n: Integer) -> Integer { n |> add(5) |> twice() |> add(1)}Function values
Section titled “Function values”A function type is written fn(A) -> R. A lambda’s parameter types can be
omitted whenever the expected type supplies them. The dot in f.(n)
invokes a function value; plain f(n) calls a named function:
pub fn run(f: fn(Integer) -> Integer, n: Integer) -> Integer { f.(n)}
pub fn inc(n: Integer) -> Integer { run(fn(x) -> x + 1, n)}A lambda is a value: return it, store it, capture into it. The returned
lambda below remembers amount; closures capture the binding as it was at
capture time, so rebinding the name later changes nothing for the closure.
pub fn adder(amount: Integer) -> fn(Integer) -> Integer { fn(value) -> { sum: Integer = value + amount sum }}Function values can also carry contracts. The proving chapter
shows callbacks carrying requires/ensures through storage and later calls.
Clauses share one signature
Section titled “Clauses share one signature”A named function may have several pattern clauses. One signature governs them all; Gale has no ad-hoc overloads:
pub fn first(items: List<Integer>) -> Option<Integer>fn first([]) { None }fn first([head | _]) { Some(head) }Heads and guards choose the first matching clause, exactly as in case.
Clauses with the same name and arity share one signature.
To accept several input shapes, declare a union or ADT and match within one signature. For multiple arities, write the smaller function as a call to the larger. Gale has no default parameters:
alias gale_std.string as text
pub fn greeting(name: String) -> String { greeting(name, "Hello")}
pub fn greeting(name: String, prefix: String) -> String { "#{prefix}, #{text.trim(name)}"}The function header can also declare contracts:
mod function_heads
pub fn count(values: List<Integer>) -> Integerensures { result >= 0 }
fn count([]) { 0 }fn count([_ | tail]) { 1 + count(tail) }One header owns visibility, types, mailbox and contracts; the clauses supply patterns and bodies covering all valid inputs.
Recursion
Section titled “Recursion”Functions can call themselves or functions declared later in the same
module. Ordinary recursion may diverge. Gale does not force termination on
executable code; when a proof needs termination, it asks for a decreases
measure instead (proving):
pub fn sum(items: List<Integer>) -> Integer { case items { [] -> 0 [head | tail] -> head + sum(tail) }}Eager or lazy
Section titled “Eager or lazy”gale_std.list transforms eagerly. gale_std.stream is a lazy Elixir
stream: map, filter and take build a pipeline that does nothing until
an explicit to_list/reduce/run consumes it and returns a checked
execution result:
alias gale_std.stream
pub fn first_n(values: List<Integer>, n: Integer) -> Result<List<Integer>, stream.StreamError> { values |> stream.from_list() |> stream.map(fn(value: Integer) -> value * 2) |> stream.take(n) |> stream.to_list()}Generics keep relationships
Section titled “Generics keep relationships”Generic parameters are explicit. swap preserves both component types, and
same compares two values of one related type without inventing an equality
bound:
pub fn swap<A, B>(pair: {A, B}) -> {B, A} { {left, right} = pair {right, left}}
pub fn same<A>(left: A, right: A) -> Boolean { left == right}