functional programming
editable examples
Every example on this page can be edited and run here: click the pencil to open it in an editor, change it, and run it in your browser. Errors, hovers and completions come from the ghūl compiler as you type.
The ghul-examples repository has fuller functional-programming examples to build and run locally, in a GitHub Codespace or a dev container.
ghūl supports a functional style of programming: functions are first-class values, the common data types are read-only by default, unions and pattern matching model data by cases, and pipes transform sequences without mutating them
first class functions
ghūl has first class functions. There is a function literal syntax that constructs functions, which can then be called, but also assigned to variables, passed to other functions, stored in data structures, or pretty much anything else you can do with any other ghūl value
filter, map, reduce
ghūl pipes provide filter, map and reduce as well as other ways to work with sequences of values. Each is a global function in Ghul.Pipes taking the sequence as its first argument, so the thread-first operator |> feeds one into the next:
recursion
ghūl methods, global functions and anonymous functions can all call themselves or each other recursively
self recursion in anonymous functions
factorial(5): 120 fibonacci(10): 55
mutual recursion in anonymous functions
Mutual recursion for anonymous functions is slightly awkward because of the forward reference. One way is to declare one as a mutable variable, define the other, then assign to it: the let mut is captured by reference, so the first function sees the second once it is assigned:
even(10): True odd(10): False
mutual recursion in named functions
Mutual recursion with named functions doesn't require any workarounds
read-only by default
While ghūl supports imperative code, it also aims to make pure functions and predictable shared data low friction: the types and traits below expose no way to change a value after it is constructed. The guarantee is what .NET allows it to be. It is shallow, so a read-only structure can still hold references to objects that are themselves mutable, and it binds ghūl code, so code written in another .NET language is not required to honour it. Stick to these types and shared data behaves predictably.
lists and maps are read-only views
The standard traits Collections.List[T] and Collections.Map[K, V] expose no mutating members. The mutable LIST and MAP implement them, so a function that accepts List[T] can read the list it is given but cannot change it.
arrays are read-only
The ghūl array type T[] has no assign indexer: elements can be read but not replaced. An array literal constructs a plain array, so the same applies to it.
tuples are immutable
Tuple elements have no assign accessors, and tuples are value types, so a tuple passed to other code is a copy: nothing can change a tuple you hold.
primary constructors define read-only members
The members a primary constructor generates are set at construction and, by default, not publicly assignable afterwards: a parameter opts in to a writable property with the public modifier. Fields declared on a union's variants are read-only in the same way.
properties are not publicly assignable by default
When defining properties in classes and structs, they are not publicly assignable by default
pipes support non mutating operations over lists
pipes make it easy to iterate over lists and generators producing transformed output without mutating the source data
list: System.Int32[]
expression oriented programming
Expression bodies and value-producing if, case, and val ... lav blocks help in writing pure functions; see expression oriented programming.
higher order functions
higher order generically typed global functions
higher order generically typed methods:
higher order anonymous functions:
Anonymous functions take a single concrete type from context; there is no generic equivalent to the two preceding forms. For polymorphic behaviour, declare a generic global function or method.
union types and pattern matching
A union holds one of several variants, and the if let and case patterns take one apart. They are how functional ghūl code models data, and they have their own page: unions and pattern matching.
currying
partial application
add_5(3): 8 add_10(3): 13
lazy sequences
Lazy infinite and finite sequences are expressed with the Ghul.Pipes.STREAM[T, S] union and the stream(initial, advance) factory. State type S and output type T are independent, so the state of a stream is hidden from its consumers; stream() returns a plain Pipe[T].
advance is a pure step function: it receives the current state and returns either DONE (sequence is over) or YIELD(value, next_state), the yielded element and the state to feed back in on the next step. The || infix is parser sugar for YIELD(value, next_state), so a step body usually reads value || next_state.
Type arguments to stream are inferred from the initial-state value and the anonymous function's yield expression.
The factory returns Pipe[T] directly so combinators like take, filter, map, zip, and index chain straight onto a stream value. State shape never appears in the type a consumer sees of a stream(...)-produced pipe.