Monocurl
Download

std.util

util.mcl

Reference for the symbols exported by util.mcl.

Index

Declarations

Map Helpers

String Utilities

Type Coercion

operator set_defaultimportant Pre-fill one named default argument on a callable target. function to_string Convert a value to a string. function to_int Convert an integer, float, or numeric string to an integer. function to_float Convert an integer, float, or numeric string to a float. function real Real part of a number; non-complex numbers return themselves. function imag Imaginary part of a number; non-complex numbers return 0. function type_of Runtime type name for a value. function has_attr Test whether a live function/operator value has a named argument attribute. function get_attr Read a named argument attribute from a live function/operator value. function set_attr Return a value with a named live-call argument changed. function get_defaults Read callable default arguments. operator set_defaults Pre-fill multiple named default arguments on a callable target. function runtime_error Raise a runtime error with the given message. function is_nil Test whether a value is nil. function is_int Test whether a value is an integer. function is_float Test whether a value is a float. function is_complex Test whether a value is complex. function is_number Test whether a value is numeric. function is_string Test whether a value is a string. function is_list Test whether a value is a list. function is_map Test whether a value is a map. function is_mesh Test whether a value is a mesh. function is_primitive_anim Test whether a value is a primitive animation. function is_anim_block Test whether a value is an animation block. function is_function Test whether a value is a function. function is_operator Test whether a value is an operator. function is_live_function Test whether a value is a live function invocation. function is_live_operator Test whether a value is a live operator invocation. function is_callable Test whether a value can be called. function is_stateful Test whether a value is stateful.

Declarations

importantfunction

Build a half-open numeric range, optimized by the compiler in simple for loops.

range(start, stop, step) includes start and stops before stop; a negative step counts down and a zero step is an error. When all three inputs are integral, the returned values are integers.

let range = |start, stop, step = 1| ...
stepincrement; may be negative, but not zero
for (i in range(0, 4)) { print i }
for (i in range(3, -1, -1)) { print i }
importantfunction

Build uniformly spaced samples including both endpoints.

Use this for plotted sample points where the final endpoint must be present. sample(a, b, 1) returns [a], not [b].

let sample = |start, stop, sample_count| ...
let xs = sample(-2, 2, 9)
importantfunction

Build uniformly spaced samples including start but excluding stop.

Useful for periodic samples where including both endpoints would duplicate the seam.

let sample_clopen = |start, stop, sample_count| ...
let angles = sample_clopen(0, TAU, 12)
importantfunction

Build a list containing n copies of item.

let const_list = |item, n| {
    var out = []
    for (i in range(0, n)) { out .= item }
    return out
}
let placeholders = const_list([], 3)
importantfunction

Return the length of a list, map, or string.

String length counts characters, not bytes.

let len = |container| ...
function

Return the input unchanged, usually as a default mapper or sort key.

let identity = |x| x
let same = map(values, identity)
function

Reverse a list or string.

let reverse = |v| ...
function

Stable sort with an optional key function.

The key lambda is evaluated once per element; elements with equal keys keep their input order.

let sort = |v, key = identity| ...
let ordered = sort(points, |p| p[0])
function

Map a function over a list.

let map = |v, f| {
    var out = []
    for (x in v) { out .= f(x) }
    return out
}
let dots = map(sample(-1, 1, 5), |x| center{[x, 0, 0]} Dot())
function

Keep only elements for which predicate returns truthy.

let filter = |v, predicate| {
    var out = []
    for (x in v) {
        if (predicate(x)) { out .= x }
    }
    return out
}
let positives = filter(values, |x| x > 0)
function

Fold a list from left to right with an explicit seed.

reduce([a, b, c], seed, f) evaluates like f(f(f(seed, a), b), c).

let reduce = |v, seed, binary| {
    var acc = seed
    for (x in v) { acc = binary(acc, x) }
    return acc
}
let total = reduce(values, 0, |acc, x| acc + x)
function

Pair corresponding elements from two lists, truncating to the shorter length.

let zip = |u, v| ...
let stops = zip(sample(0, 1, 3), [BLUE, CYAN, WHITE])
function

Pair every list element with its zero-based index.

let enumerate = |v| ...
for ([i, piece] in enumerate(pieces)) { print [i, piece] }
function

Return truthy when every element is truthy.

let all = |v| {
    for (x in v) { if (not x) { return 0 } }
    return 1
}
function

Return truthy when any element is truthy.

let any = |v| {
    for (x in v) { if (x) { return 1 } }
    return 0
}
function

Count elements satisfying a predicate.

let count = |v, predicate| {
    var n = 0
    for (x in v) { if (predicate(x)) { n = n + 1 } }
    return n
}
function

Sum a list; list values are added element-wise by the language.

let sum = |v| {
    var s = 0
    for (x in v) { s = s + x }
    return s
}
let centroid = sum(points) / len(points)
function

Product of all elements in a list.

let product = |v| {
    var p = 1
    for (x in v) { p = p * x }
    return p
}
function

Maximum element of a non-empty list, optionally by key.

Errors on an empty list. Use std.math.max(a, b) for pairwise numeric maximum.

let max_of = |v, key = identity| ...
function

Minimum element of a non-empty list, optionally by key.

Errors on an empty list. Use std.math.min(a, b) for pairwise numeric minimum.

let min_of = |v, key = identity| ...

Map Helpers

function

Keys from a map in insertion order.

let map_keys = |m| ...
function

Values from a map in insertion order.

let map_values = |m| ...
function

Key/value pairs from a map in insertion order.

let map_items = |m| ...
for ([key, value] in map_items(stats)) { print [key, value] }
function

First n elements of a list.

Negative counts are treated as zero.

let take = |v, n| ...
function

List with the first n elements removed.

Negative counts are treated as zero.

let drop = |v, n| ...
function

Select list elements by index list.

Indexes are zero-based and must be in bounds; duplicates are allowed and preserve the order of indexes.

let list_subset = |src, indexes| ...
let corners = list_subset(points, [0, 2, 3])
function

Concatenate one level of nested lists.

let flatten = |v| {
    var out = []
    for (x in v) {
        for (y in x) { out .= y }
    }
    return out
}
let all_terms = flatten(rows)
function

Maximum list nesting depth of a value.

Atoms have depth 0; [1, 2] has depth 1.

let depth = |v| ...

String Utilities

function

Character length of a string.

let str_len = |s| ...
function

Replace all occurrences of needle with with.

let str_replace = |s, needle, with| ...
function

Split a string by a separator.

let str_split = |s, sep| ...
function

Uppercase a string.

let str_upper = |s| ...
function

Lowercase a string.

let str_lower = |s| ...
function

Join strings with a separator.

let str_join = |parts, sep = ""| ...
let label = str_join(["x", "y", "z"], ", ")

Type Coercion

importantoperator

Pre-fill one named default argument on a callable target.

This is mainly for building configuration operators such as axis_style. Because it is an operator, it interpolates from the callable's original default to the replacement during Lerp.

let set_default = operator |target, name, value| ...
play set_default{"similar_topo_hint", 1} TagTrans(1.5, [&shape])
function

Convert a value to a string.

Uses the same stringification rules as text constructors, so lists become concatenated text fragments.

let to_string = |x| ...
function

Convert an integer, float, or numeric string to an integer.

Float conversion truncates toward zero; strings are trimmed before parsing.

let to_int = |x| ...
function

Convert an integer, float, or numeric string to a float.

let to_float = |x| ...
function

Real part of a number; non-complex numbers return themselves.

let real = |x| ...
function

Imaginary part of a number; non-complex numbers return 0.

let imag = |x| ...
function

Runtime type name for a value.

let type_of = |x| ...
function

Test whether a live function/operator value has a named argument attribute.

let has_attr = |x, name| ...
function

Read a named argument attribute from a live function/operator value.

let get_attr = |x, name| ...
function

Return a value with a named live-call argument changed.

This is the dynamic form of ball.radius = 0.8; it returns the updated value so it works inside expressions.

let set_attr = |x, name, value| ...
ball = set_attr(ball, "radius", 0.8)
function

Read callable default arguments.

Returns the names that can be pre-filled with set_default or set_defaults.

let get_defaults = |x| ...
operator

Pre-fill multiple named default arguments on a callable target.

Map keys must be strings naming defaults on the target.

let set_defaults = operator |target, defaults| ...
function

Raise a runtime error with the given message.

let runtime_error = |message| ...
function

Test whether a value is nil.

let is_nil = |x| ...
function

Test whether a value is an integer.

let is_int = |x| ...
function

Test whether a value is a float.

let is_float = |x| ...
function

Test whether a value is complex.

let is_complex = |x| ...
function

Test whether a value is numeric.

let is_number = |x| ...
function

Test whether a value is a string.

let is_string = |x| ...
function

Test whether a value is a list.

let is_list = |x| ...
function

Test whether a value is a map.

let is_map = |x| ...
function

Test whether a value is a mesh.

let is_mesh = |x| ...
function

Test whether a value is a primitive animation.

let is_primitive_anim = |x| ...
function

Test whether a value is an animation block.

let is_anim_block = |x| ...
function

Test whether a value is a function.

let is_function = |x| ...
function

Test whether a value is an operator.

let is_operator = |x| ...
function

Test whether a value is a live function invocation.

let is_live_function = |x| ...
function

Test whether a value is a live operator invocation.

let is_live_operator = |x| ...
function

Test whether a value can be called.

let is_callable = |x| ...
function

Test whether a value is stateful.

let is_stateful = |x| ...