std.prelude

The prelude: functions and constants every program has, written in Brim itself. min(a, b) and PI are used by name; the functions named list_... are the methods of lists: xs.sorted() is list_sorted(xs).

Types

struct StringBuilder

pub struct StringBuilder {
    // and private fields
}

Builds a string from many pieces. Joining with + in a loop copies the text so far each time; a builder keeps the pieces and joins them once:

let mut out = StringBuilder.new()
for name in names {
    out.add(name)
    out.add(", ")
}
print(out.build())

Methods

pub fn new() -> StringBuilder

An empty builder.

pub fn add(mut self, text: string)

Adds text at the end.

pub fn add_line(mut self, text: string)

Adds text and a line break.

pub fn is_empty(self) -> bool

Whether nothing has been added (or only empty strings).

pub fn build(self) -> string

The text added so far.

Constants

PI

pub const PI: f64 = 3.141592653589793

The ratio of a circle's circumference to its diameter.

E

pub const E: f64 = 2.718281828459045

The base of the natural logarithm.

Functions

fn min

pub fn min[T: Ord](a: T, b: T) -> T

The smaller of two values (a when they are equal).

fn max

pub fn max[T: Ord](a: T, b: T) -> T

The larger of two values (a when they are equal).

fn clamp

pub fn clamp[T: Ord](x: T, low: T, high: T) -> T

x, moved into the range from low to high.

fn option_is_some

pub fn option_is_some[T](o: Option[T]) -> bool

Whether there is a value.

fn option_is_none

pub fn option_is_none[T](o: Option[T]) -> bool

Whether there is no value.

fn option_unwrap_or

pub fn option_unwrap_or[T](o: Option[T], fallback: T) -> T

The value, or fallback when there is none.

fn option_map

pub fn option_map[T, U](o: Option[T], f: fn(T) -> U) -> Option[U]

Some(f(x)) for Some(x); None stays None.

fn result_is_ok

pub fn result_is_ok[T, E](r: Result[T, E]) -> bool

Whether it is Ok.

fn result_is_err

pub fn result_is_err[T, E](r: Result[T, E]) -> bool

Whether it is Err.

fn result_unwrap_or

pub fn result_unwrap_or[T, E](r: Result[T, E], fallback: T) -> T

The Ok value, or fallback for an Err.

fn result_ok

pub fn result_ok[T, E](r: Result[T, E]) -> Option[T]

The Ok value as an Option; the error is dropped.

fn result_map

pub fn result_map[T, E, U](r: Result[T, E], f: fn(T) -> U) -> Result[U, E]

Ok(f(x)) for Ok(x); an Err stays as it is.

fn string_char_count

pub fn string_char_count(s: string) -> i64

The number of characters (Unicode code points) in s; len() counts bytes.

fn list_sorted

pub fn list_sorted[T: Ord](xs: List[T]) -> List[T]

A sorted copy of xs, smallest first. The sort is stable: equal values keep their order.

fn list_sorted_by

pub fn list_sorted_by[T](xs: List[T], less: fn(T, T) -> bool) -> List[T]

A copy of xs sorted so that less(a, b) holds for each a before b, unless they are equal; stable, like sorted.

fn list_reversed

pub fn list_reversed[T](xs: List[T]) -> List[T]

A copy of xs with the elements in the opposite order.

fn list_slice

pub fn list_slice[T](xs: List[T], start: i64, end: i64) -> List[T]

The elements from index start up to, but not including, end. Indices outside the list are moved to its start or end, so the result is never an error: [1, 2, 3].slice(1, 10) is [2, 3].

fn list_index_of

pub fn list_index_of[T: Eq](xs: List[T], x: T) -> Option[i64]

The index of the first element equal to x, or None.

fn list_map

pub fn list_map[T, U](xs: List[T], f: fn(T) -> U) -> List[U]

f(x) for each element x, in a new list.

fn list_filter

pub fn list_filter[T](xs: List[T], keep: fn(T) -> bool) -> List[T]

The elements for which keep is true, in order.

fn list_any

pub fn list_any[T](xs: List[T], test: fn(T) -> bool) -> bool

Whether test is true for at least one element.

fn list_all

pub fn list_all[T](xs: List[T], test: fn(T) -> bool) -> bool

Whether test is true for every element (also for an empty list).

fn list_fold

pub fn list_fold[T, A](xs: List[T], init: A, f: fn(A, T) -> A) -> A

init combined with each element in turn: f(f(f(init, a), b), c).

fn list_find

pub fn list_find[T](xs: List[T], test: fn(T) -> bool) -> Option[T]

The first element for which test is true, or None.

fn list_each

pub fn list_each[T](xs: List[T], f: fn(T))

Calls f with each element, in order.

fn list_min

pub fn list_min[T: Ord](xs: List[T]) -> Option[T]

The smallest element, or None for an empty list.

fn list_max

pub fn list_max[T: Ord](xs: List[T]) -> Option[T]

The largest element, or None for an empty list.

fn list_sum_i64

pub fn list_sum_i64(xs: List[i64]) -> i64

The sum of the elements; 0 for an empty list. Overflow stops the program.

fn list_sum_i32

pub fn list_sum_i32(xs: List[i32]) -> i32

The sum of the elements; 0 for an empty list. Overflow stops the program.

fn list_sum_u8

pub fn list_sum_u8(xs: List[u8]) -> u8

The sum of the elements; 0 for an empty list. Overflow stops the program.

fn list_sum_u64

pub fn list_sum_u64(xs: List[u64]) -> u64

The sum of the elements; 0 for an empty list. Overflow stops the program.

fn list_sum_f64

pub fn list_sum_f64(xs: List[f64]) -> f64

The sum of the elements; 0 for an empty list.

fn set_from_list

pub fn set_from_list[T: Key](xs: List[T]) -> Set[T]

The distinct elements of xs, in the order they first appear: Set(xs).

fn set_union

pub fn set_union[T: Key](a: Set[T], b: Set[T]) -> Set[T]

The elements in a, b or both.

fn set_intersection

pub fn set_intersection[T: Key](a: Set[T], b: Set[T]) -> Set[T]

The elements in both a and b.

fn set_difference

pub fn set_difference[T: Key](a: Set[T], b: Set[T]) -> Set[T]

The elements in a but not in b.

fn set_is_subset

pub fn set_is_subset[T: Key](a: Set[T], b: Set[T]) -> bool

Whether every element of a is in b.