Gjallarhorn


A Brief Introduction to Gjallarhorn

Gjallarhorn is centered around two core types - Signals and Mutables.

A signal represents a value that changes over time, and has the following properties: - A current value, which can always be fetched - A mechanism to signal changes to subscribers that the value has been updated

A Mutable is a holder for mutable variables, similar to a reference cell. They are typically created via Mutable.create:

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
#r "Gjallarhorn.dll"
open Gjallarhorn

// Create a mutable variable
let variable = Mutable.create "Foo"
printfn "hello = %s" variable.Value
variable.Value <- "Bar"
printfn "hello = %s" variable.Value

In this simple example, we show creating, reading from, and updating a Mutable.

Mutable variables in Gjallarhorn have some distict features:

  • Very low memory overhead: When created, a mutable like the one above has no memory overhead above that of a standard reference cell. It is effectively nothing but a thin wrapper around the current value, without any extra fields.
  • They expose themselves as a signal - there is always a current value and they can notify subscribers of changes

Once your core data is held within a Mutable or pulled in via something that provides a signal, you can use the Signal module to transform the data

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
// Create mutable variables
let first = Mutable.create ""
let last = Mutable.create ""
// Map these two variables from a first and last name to a full name
let full = Signal.map2 (fun f l -> f + " " + l) first last

printfn "initial = %s" full.Value
first.Value <- "Reed"
last.Value <- "Copsey"
// Prints: "Hello Reed Copsey!"
printfn "Hello %s!" full.Value

Next, we'll go into more details about signals.

val variable : obj

Full name: Intro.variable
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val first : obj

Full name: Intro.first
val last : obj

Full name: Intro.last
val full : obj

Full name: Intro.full
Fork me on GitHub