shine_tree
Types
Constants
pub const fold_right: fn(ShineTree(a), b, fn(c, a) -> c) -> c
This is an alias for fold_r
.
Functions
pub fn all(tree: ShineTree(a), f: fn(a) -> Bool) -> Bool
Returns true
if the predicate f
returns True
for each
element in the ShineTree. Returns False
otherwise.
Examples
shine_tree.all(shine_tree.empty, fn(x) { x == 0 })
// -> True
all(shine_tree.from_list([-1, -42]), fn(x) { x < 0 })
// -> True
all(shine_tree.from_list([2, 4, 6, 8, 10, 11]), int.is_even)
// -> False
pub fn any(tree: ShineTree(a), f: fn(a) -> Bool) -> Bool
Returns True
if the predicate f
returns True
for any
element in the ShineTree. Returns False
otherwise.
Examples
// No items in the tree will always be false
any(shine_tree.empty, int.is_even)
// -> False
any([10, 32, 42, 43, 44], fn(x) { x == 42 })
// -> True
any([3, 4], fn(x) { x + 1 == 42 })
// -> False
pub fn contains(tree: ShineTree(a), u: a) -> Bool
Returns True
if the given value is contained in the given tree.
pub fn count(tree: ShineTree(a), where f: fn(a) -> Bool) -> Int
Counts the number of elements in a given ShineTree satisfying a given predicate.
This function has to traverse the entire tree to determine the number of elements, so it runs in linear time.
Examples
count(shine_tree.from_list([2, 3, 4, 5, 6, 7, 8, 9, 10]), int.is_even)
// -> 5
pub fn each(tree: ShineTree(a), f: fn(a) -> b) -> Nil
Calls a function for each element in the tree, discarding the return value.
pub fn equals(a: ShineTree(a), b: ShineTree(a)) -> Bool
Compares two trees for equality. Since trees can have many shapes, the items can be in the correct order and values, but the tree is balanced differently.
This function reduces over the two trees and compares their values, immediately
returning False
if they are not equal.
pub fn filter(
tree: ShineTree(a),
f: fn(a) -> Bool,
) -> ShineTree(a)
Creates a new tree containing all the elements of the given tree,
for which the given predicate returns True
.
shine_tree.from_list([1, 2, 3, 4, 5, 6, 7])
|> filter(int.is_odd)
// -> shine_tree.from_list([1, 3, 5, 7])
pub fn filter_map(
tree: ShineTree(a),
f: fn(a) -> Result(b, c),
) -> ShineTree(b)
Returns a new tree containing only the elements from the first tree
where the given function returns Ok(b)
.
pub fn find(
tree: ShineTree(a),
f: fn(a) -> Bool,
) -> Result(a, Nil)
Finds the first element in a given tree that satisfies the given predicate.
shine_tree.from_list([1, 2, 3, 4, 5, 6, 7])
|> find(int.is_even)
// -> 2
pub fn find_map(
tree: ShineTree(a),
f: fn(a) -> Result(b, c),
) -> Result(b, Nil)
Finds the first element in a given tree for which the given function returns Ok(val)
.
shine_tree.from_list([1, 2, 3, 4, 5, 6, 7])
|> find_map(fn(u) {
case u > 5 {
True -> Ok(u)
False -> Error(Nil)
}
})
// -> 2
pub fn fold_l(
over tree: ShineTree(a),
from v: b,
with f: fn(c, a) -> c,
) -> c
Reduces all the elements of the given tree into a single value, made by calling a given function on all the elements, traversing the tree from left to right.
fold_l(shine_tree.from_list([1, 2, 3]), 0, int.add)
is the equivalent of
add(add(add(0, 1), 2), 3)
.
// calculate 20! (factorial)
let n = 20
shine_tree.fold_l(shine_tree.range(2, n), int.multiply)
// -> 2432902008176640000
pub fn fold_r(tree: ShineTree(a), v: b, f: fn(c, a) -> c) -> c
Reduces all the elements of the given tree into a single value, made by calling a given function on all the elements, traversing the tree from left to right.
fold_r(shine_tree.from_list([1, 2, 3]), 0, int.add)
is the equivalent of
add(add(add(0, 3), 2), 1)
.
// calculate 20! (factorial)
let n = 20
shine_tree.fold_r(shine_tree.range(2, n), int.multiply)
// -> 2432902008176640000
pub fn fold_until(
tree: ShineTree(a),
v: b,
with f: fn(b, a) -> ContinueOrStop(b),
) -> b
Fold a given value over the items of a given tree, starting with the beginning until the
given function returns Stop
.
The accumulated value is then returned.
Examples
fold_until(shine_tree.from_list([2, 3, 4, 5, 6, 7, 8, 9, 10]), 0, fn(acc, x) {
case x {
5 -> Stop(acc + 1)
_ -> Continue(acc + x)
}
})
// -> 10
pub fn from_iterator(iterable: Iterator(a)) -> ShineTree(a)
pub fn from_list(values: List(a)) -> ShineTree(a)
Creates a new tree containing all the elements of the given list.
shine_tree.from_list([1, 2, 3, 4, 5, 6, 7])
// -> It's a ShineTree with all the items in it!
// Were you expecting a list?
pub fn get(tree: ShineTree(a), index: Int) -> Result(a, Nil)
Get the value at the given index in the tree if it exists.
pub fn group(
tree: ShineTree(a),
f: fn(a) -> b,
) -> Dict(b, ShineTree(a))
This function takes the tree, and groups the values by a returned key into a Dict
.
It does not sort the values, or preserve the order.
pub fn map(
tree: ShineTree(a),
with f: fn(a) -> b,
) -> ShineTree(b)
Maps all the elements of the given tree into a new tree where each element has been transformed by the given function.
let tree = shine_tree.from_list([1, 2, 3])
shine_tree.map(tree, int.multiply(2, _))
// -> shine_tree.from_list([2, 4, 6])
pub fn pop(tree: ShineTree(a)) -> Result(#(a, ShineTree(a)), Nil)
Pops an element from the end of the given tree.
If there are no items, it returns Error(Nil)
, otherwise
it returns the popped item, and the resulting tree with the
element removed.
let tree = shine_tree.from_list([1, 2, 3])
shine_tree.pop(tree)
// -> Ok(3, shine_tree.from_list([1, 2]))
shine_tree.empty |> shine_tree.pop
// -> Error(Nil)
pub fn push(tree: ShineTree(a), value: a) -> ShineTree(a)
Pushes an element to the end of the given tree.
let tree = shine_tree.from_list([1, 2, 3])
shine_tree.push(tree, 4)
// -> shine_tree.from_list([1, 2, 3, 4])
pub fn range(start: Int, finish: Int) -> ShineTree(Int)
Creates a tree of n
consecutive integers, starting from start
.
and finishing with finish
.
Examples
let ten_items = shine_tree.range(1, 10)
pub fn shift(
tree: ShineTree(a),
) -> Result(#(a, ShineTree(a)), Nil)
Pops an element from the front of the given tree.
If there are no items, it returns Error(Nil)
, otherwise
it returns the popped item, and the resulting tree with the
element removed.
let tree = shine_tree.from_list([1, 2, 3])
shine_tree.shift(tree)
// -> Ok(1, shine_tree.from_list([2, 3]))
shine_tree.empty |> shine_tree.pop
// -> Error(Nil)
pub fn size(tree: ShineTree(a)) -> Int
Returns the number of elements in the given tree.
This is an O(1) operation, because the size is cached in the tree at the time it is created.
pub fn sort(
tree: ShineTree(a),
compare: fn(a, a) -> Order,
) -> ShineTree(a)
Sort a ShineTree
using the compare
function using a “quicksort” algorithm.
pub fn to_iterator(tree: ShineTree(a)) -> Iterator(a)
Creates an iterator from a given tree, yielding each element in succession until there are no elements left.
Examples
let iter = unfold(shine_tree.from_list([1, 2, 3]))
let #(num, iter) = iterator.step(iter)
// -> Next(1, Iterator(Int))
let #(num, iter) = iterator.step(iter)
// -> Next(2, Iterator(Int))
let #(num, iter) = iterator.step(iter)
// -> Next(3, Iterator(Int))
let a = iterator.step(iter)
// -> Done
pub fn to_list(tree: ShineTree(a)) -> List(a)
Creates a new list containing all the elements of the given tree.
shine_tree.from_list([1, 2, 3, 4, 5, 6, 7])
|> to_list
// -> [1, 2, 3, 4, 5, 6, 7]