Scarlet Industries

Collections

Arrays and maps are persistent values: an operation returns a new collection and the original keeps its contents.

Arrays

A range a..b is an array, end-exclusive.

xs = [3, 1, 4, 1, 5, 9, 2, 6]

println('map ${array.map(xs, fn(x) x * x)}')
println('filter ${array.filter(xs, fn(x) x % 2 == 0)}')
println('fold ${array.fold(xs, 0, fn(acc, x) acc + x)}')

head = [0]
tail = [7, 8]
println('spread ${[..head, 42, ..tail]}')

Maps

Maps are built with map.new() or map.from_list. Iteration order over a hash map is unspecified.

defaults = map.from_list([('mode', 'fast'), ('retries', '3')])
overrides = map.from_list([('retries', '5')])
settings = map.merge(defaults, overrides)

counts = array.fold(words, map.new(), fn(m, w) map.set(m, w, { map.get(m, w) or 0 } + 1))