List
-- Lists are immutable values: every operation returns a new list.
a : List<String> = []
(println (concat "list: " (show a)))
b : List<String> = (push "Hello" a)
(println (concat "list: " (show b)))
(println (concat "list size: " (show (length b))))
c : List<String> = (push "World" b)
(println (concat "list: " (show c)))
-- Reading an index that may be out of bounds gives an `Option`.
(println (concat "list[1]: " (show (get 1 c))))
(println (concat "list[5]: " (show (get 5 c))))
(println (concat "list[5] or a default: " (withDefault "none" (get 5 c))))
d : List<String> = (push "Foo" c)
(println (concat "list: " (show d)))
(println (concat "without index 2: " (show (remove 2 d))))
(println (concat "without index 5: " (show (remove 5 d))))
(println (concat "unchanged: " (show d)))
Output
list: []
list: ["Hello"]
list size: 1
list: ["Hello", "World"]
list[1]: (Some "World")
list[5]: None
list[5] or a default: none
list: ["Hello", "World", "Foo"]
without index 2: ["Hello", "World"]
without index 5: ["Hello", "World", "Foo"]
unchanged: ["Hello", "World", "Foo"]
Run it yourself from the repository root: lbd run examples/list.lbd