Collection operations for PTC-Lisp runtime.
Provides filtering, mapping, sorting, and other collection manipulation functions.
Summary
Functions
Generate all n-combinations from a collection.
Returns a list with sep inserted between each element.
Variadic version of max-by that supports both (max-by key coll) and (apply max-by key item1 item2 ...).
Returns the x for which (f x) is greatest. Matches Clojure's max-key.
Variadic version of min-by that supports both (min-by key coll) and (apply min-by key item1 item2 ...).
Returns the x for which (f x) is least. Matches Clojure's min-key.
Transform a tree bottom-up by applying f to each node after recursing into children.
Transform a tree top-down by applying f to each node before recursing into children.
Returns a depth-first lazy sequence of all nodes in a tree.
Generic tree walker. Applies inner to each element of form, then applies outer to the result.
Functions
Generate all n-combinations from a collection.
Works with any seqable: lists, strings, maps.
Examples
iex> PtcRunner.Lisp.Runtime.Collection.combinations([1, 2, 3, 4], 3)
[[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]
iex> PtcRunner.Lisp.Runtime.Collection.combinations([1, 2], 0)
[[]]
iex> PtcRunner.Lisp.Runtime.Collection.combinations([1, 2], 3)
[]
Returns a list with sep inserted between each element.
Examples
iex> PtcRunner.Lisp.Runtime.Collection.interpose(", ", ["a", "b", "c"])
["a", ", ", "b", ", ", "c"]
iex> PtcRunner.Lisp.Runtime.Collection.interpose(:x, [1])
[1]
iex> PtcRunner.Lisp.Runtime.Collection.interpose(:x, [])
[]
iex> PtcRunner.Lisp.Runtime.Collection.interpose(:x, nil)
[]
iex> PtcRunner.Lisp.Runtime.Collection.interpose(nil, [1, 2, 3])
[1, nil, 2, nil, 3]
Variadic version of max-by that supports both (max-by key coll) and (apply max-by key item1 item2 ...).
Returns the x for which (f x) is greatest. Matches Clojure's max-key.
Examples
iex> PtcRunner.Lisp.Runtime.Collection.max_key_variadic([&String.length/1, "a", "abc", "ab"])
"abc"
Variadic version of min-by that supports both (min-by key coll) and (apply min-by key item1 item2 ...).
Returns the x for which (f x) is least. Matches Clojure's min-key.
Examples
iex> PtcRunner.Lisp.Runtime.Collection.min_key_variadic([&String.length/1, "a", "abc", "ab"])
"a"
Transform a tree bottom-up by applying f to each node after recursing into children.
Examples
iex> PtcRunner.Lisp.Runtime.Collection.postwalk(&Function.identity/1, [1, [2, 3]])
[1, [2, 3]]
Transform a tree top-down by applying f to each node before recursing into children.
Examples
iex> PtcRunner.Lisp.Runtime.Collection.prewalk(&Function.identity/1, [1, [2, 3]])
[1, [2, 3]]
Returns a depth-first lazy sequence of all nodes in a tree.
branch? is a predicate that returns true if a node has children. children returns the children of a branch node.
When branch? or children is a keyword, it is used to access that key from maps. For branch?, the result is checked for truthiness.
Examples
iex> tree = %{id: 1, children: [%{id: 2, children: []}, %{id: 3, children: []}]}
iex> result = PtcRunner.Lisp.Runtime.Collection.tree_seq(
...> fn node -> is_map(node) && Map.has_key?(node, :children) end,
...> fn node -> Map.get(node, :children, []) end,
...> tree
...> )
iex> Enum.map(result, & &1.id)
[1, 2, 3]
Generic tree walker. Applies inner to each element of form, then applies outer to the result.
For lists, walks each element. For maps, walks each [key, value] pair. For sets, walks each element and reconstructs the set. For scalars, just applies outer.
Examples
iex> PtcRunner.Lisp.Runtime.Collection.walk(&Function.identity/1, &Function.identity/1, [1, 2, 3])
[1, 2, 3]
iex> PtcRunner.Lisp.Runtime.Collection.walk(&Function.identity/1, &Enum.sum/1, [1, 2, 3])
6