std.collections
API summary (generated from the Beans source by npm run coverage): 11 package functions.
std.collections adds common algorithms on top of the builtin List and Map
types. The storage lives in the compiler; these functions are plain Beans on top.
Read the source at
stdlib/std/collections/collections.b.
import std.collectionsSome functions are generic and use trait bounds like T implements Eq. That
means the element type must support that trait (for example, Eq for equality,
Hash to be a map key, Clone to be copied). The builtin types you normally use
already support these.
List helpers
Section titled “List helpers”pub fn sum_int(values: List<int>) -> intpub fn frequencies(values: List<string>) -> Map<string, int>pub fn count<T implements Eq>(values: List<T>, needle: T) -> intpub fn filter<T implements Clone>(values: List<T>, keep: fn(T) -> bool) -> List<T>pub fn transform<T implements Clone, U>(values: List<T>, apply: fn(T) -> U) -> List<U>pub fn unique<T implements Eq & Hash & Clone>(values: List<T>) -> List<T>sum_intadds up a list of ints.frequenciescounts how often each string appears.countcounts how many items equalneedle.filterkeeps the items for whichkeepreturns true.transformmakes a new list by applyingapplyto each item.uniquedrops duplicates, keeping first-seen order.
import std.ioimport std.collections
fn main() { let nums: List<int> = [3, 1, 3, 2, 1] io.println(collections.sum_int(nums)) // 10 io.println(collections.count(nums, 3)) // 2 let odds: List<int> = collections.filter(nums, fn(n: int) -> bool { return n % 2 == 1 }) io.println(odds) // [3, 1, 3, 1] io.println(collections.unique(nums)) // [3, 1, 2]}Map helpers
Section titled “Map helpers”Several of these take inout maps. inout means the function changes the
caller’s own map in place; you do not get a copy back. After the call, your map
holds the new state.
pub fn increment<K implements Eq & Hash>(inout values: Map<K, int>, key: K, delta: int) -> intpub fn get_or_insert_with<K implements Eq & Hash, V implements Clone>(inout values: Map<K, V>, key: K, make: fn() -> V) -> Vpub fn merge_with<K implements Eq & Hash & Clone, V implements Clone>(inout target: Map<K, V>, source: Map<K, V>, combine: fn(V, V) -> V)pub fn remove_if<K implements Eq & Hash & Clone, V implements Clone>(inout values: Map<K, V>, remove: fn(K, V) -> bool) -> intpub fn map_values_with_key<K implements Eq & Hash & Clone, V implements Clone, U>(values: Map<K, V>, apply: fn(K, V) -> U) -> Map<K, U>incrementaddsdeltatokey’s value, starting from 0 for a missing key, and returns the new count.get_or_insert_withreturns the value atkey, or inserts whatmake()gives and returns that.merge_withfoldssourceintotarget. On a key that exists in both, it keepscombine(old, new).remove_ifdrops each entry whereremovereturns true and returns how many were dropped.map_values_with_keybuilds a new map with the same keys and values taken fromapply(key, value).
import std.ioimport std.collections
fn main() { var counts: Map<string, int> = {} collections.increment(inout counts, "a", 1) collections.increment(inout counts, "a", 2) // counts["a"] is now 3 io.println(counts.get("a")) // some(3)}See also
Section titled “See also”- Collections, the builtin
ListandMaptypes these helpers work on.