PtcRunner.Lisp.Runtime.Collection (PtcRunner v0.9.0)

Copy Markdown View Source

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

avg(coll)

avg_by(key, coll)

butlast(coll)

combinations(coll, n)

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)
[]

concat2(a, b)

conj(list, x)

contains?(set, val)

count(set)

difference(s1, s2)

distinct(coll)

distinct_by(key, coll)

drop(n, coll)

drop_last(coll)

drop_last(n, coll)

drop_while(key, coll)

empty?(set)

every?(key, coll)

ffirst(coll)

filter(pred, set)

find(key, coll)

first(coll)

flatten(coll)

fnext(coll)

frequencies(coll)

group_by(key, coll)

interleave(c1, c2)

interpose(sep, coll)

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]

intersection(s1, s2)

into(to, from)

last(coll)

map(key, coll)

map(f, coll1, coll2)

map(f, coll1, coll2, coll3)

map_indexed(f, coll)

mapcat(key, coll)

mapv(key, coll)

mapv(f, coll1, coll2)

mapv(f, coll1, coll2, coll3)

max_by(key, coll)

max_by_variadic(arg1)

Variadic version of max-by that supports both (max-by key coll) and (apply max-by key item1 item2 ...).

max_key_variadic(list)

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"

min_by(key, coll)

min_by_variadic(arg1)

Variadic version of min-by that supports both (min-by key coll) and (apply min-by key item1 item2 ...).

min_key_variadic(list)

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"

next(coll)

nfirst(coll)

nnext(coll)

not_any?(key, coll)

not_empty(coll)

nth(coll, idx)

partition(n, coll)

partition(n, step, coll)

partition(n, step, pad, coll)

partition_all(n, coll)

partition_all(n, step, coll)

pluck(key, coll)

postwalk(f, form)

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]]

prewalk(f, form)

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]]

range(end_val)

range(start, end_val)

range(start, end_val, step)

reduce(f, coll)

reduce(f, init, coll)

remove(pred, set)

rest(coll)

reverse(coll)

second(coll)

seq(coll)

some(key, coll)

sort(coll)

sort(comp, coll)

sort_by(key, coll)

sort_by(key, comp, coll)

sum(coll)

sum_by(key, coll)

take(n, coll)

take_last(n, coll)

take_while(key, coll)

tree_seq(branch?, children, root)

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]

union(s1, s2)

walk(inner, outer, form)

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

zip(c1, c2)