Functions

Every builtin function that isn't a method of a type. A free function is called by name - range(0, 3) - or through #builtin - #builtin.range(0, 3), which reaches the builtin even if your own code defines a range too. A namespace's functions are called on the namespace's name - Math.min(1, 2). The methods each type comes with, like "abc".size(), are listed on that type's card on Builtin Types, and a walkthrough of them in real programs is in the Examples.

The primitive number operations - ADD_U64_U64, CAST_F64_S32, ... - aren't free functions: they're reached through #builtin.intrinsic and listed on Intrinsics.

Free functions

range

#range
free function#builtin.range

The numbers from start up to, but not including, end - the same as start..<end. Empty when end <= start.

Regex

#regex
free function#builtin.Regex

Compiles a regular expression from a pattern and its flags - the same as Regex.new. An invalid pattern is a ParseError.

format

#format
free function#builtin.format

Fills each {} in template with the next value's toString(), and each {n} with the nth value (from 0). {{ and }} are literal braces. A placeholder without a matching value is a ParseError.

joinPath

#joinpath
free function#builtin.joinPath

Joins path segments with /, dropping duplicate separators - joinPath("a/", "b", "c.rfn") is "a/b/c.rfn". A segment starting with / starts over from the root.

baseName

#basename
free function#builtin.baseName

The last segment of a path - baseName("src/main.rfn") is "main.rfn".

dirName

#dirname
free function#builtin.dirName

Everything before the last segment of a path - dirName("src/main.rfn") is "src", and "." when there's no directory part.

extension

#extension
free function#builtin.extension

The last segment's extension without its dot - "rfn" for "src/main.rfn", and "" when there's none.

exit

#exit
free function#builtin.exit

Ends the program with the given exit code. Pending defer blocks still run first.

panic

#panic
free function#builtin.panic

Hands message to &panicHandler, which by default prints it on standard error and stops the program - for states that mean a bug, not a failure the caller should handle (return an Error for those). Pending defer blocks still run first.

todo

#todo
free function#builtin.todo

A placeholder for code that isn't written yet: type-checks as anything, and panics with message when reached.

unreachable

#unreachable
free function#builtin.unreachable

Marks a branch that can't happen - panics with message if it does.

typeOf

#typeof
free function#builtin.typeOf

The runtime type of value - its actual type, not the static one (see #runtime.typeof).

hash

#hash
free function#builtin.hash

The same as value.hash(), as a function to pass around.

clone

#clone
free function#builtin.clone

The same as value.clone(), as a function to pass around.

toString

#tostring
free function#builtin.toString

The same as value.toString(), as a function to pass around - e.g. xs.map(toString).

compare

#compare
free function#builtin.compare

How a compares to b - the same as a.compare(b), as a function to pass to sorting.

swap

#swap
free function#builtin.swap

Its two arguments in the other order: (b, a). set (x, y) := swap(x, y) exchanges two variables.

identity

#identity
free function#builtin.identity

Returns its argument unchanged - a no-op to pass where a function is required.

constant

#constant
free function#builtin.constant

A function that ignores its argument and always returns value.

compose

#compose
free function#builtin.compose

f after g: a function that runs g and passes its result to f.

pipe

#pipe
free function#builtin.pipe

The functions run left to right, each on the previous one's result - pipe(f, g)(x) is g(f(x)), the same as x |> f |> g. With no functions it's identity.

memoize

#memoize
free function#builtin.memoize

Wraps f so each distinct argument is computed once, and later calls with an equal argument return the stored result.

zip

#zip
free function#builtin.zip

Pairs up the elements of a and b by position, stopping at the end of the shorter one.

enumerate

#enumerate
free function#builtin.enumerate

Each element paired with its position, from 0.

repeat

#repeat
free function#builtin.repeat

value, count times - endlessly when count is undefined.

iterate

#iterate
free function#builtin.iterate

start, then next(start), then next of that, and so on - ending when next returns undefined.

chain

#chain
free function#builtin.chain

Every element of the first source, then every element of the next, and so on.

cartesian

#cartesian
free function#builtin.cartesian

Every pair of an element of a with an element of b, with a's element changing slowest.

count

#count
free function#builtin.count

start, start + step, start + 2 * step, ... - endlessly.

Math

Math.min

#math-min
Math namespace

The smallest of its arguments.

Math.max

#math-max
Math namespace

The largest of its arguments.

Math.pi

#math-pi
Math namespace

π, the ratio of a circle's circumference to its diameter - 3.14159....

Math.tau

#math-tau
Math namespace

τ = 2π, a full turn in radians.

Math.e

#math-e
Math namespace

Euler's number, the base of the natural logarithm - 2.71828....

Math.infinity

#math-infinity
Math namespace

Positive infinity. Its negation is negative infinity.

Math.nan

#math-nan
Math namespace

Not-a-number - the result of an undefined operation like 0 / 0. It isn't equal to anything, itself included; check with Math.isNan.

Math.abs

#math-abs
Math namespace

The absolute value of x.

Math.sign

#math-sign
Math namespace

-1, 0 or 1, matching the sign of x.

Math.clamp

#math-clamp
Math namespace

x limited to the range low to high, both included.

Math.sum

#math-sum
Math namespace

All the arguments added together - T.zero() when there are none.

Math.product

#math-product
Math namespace

All the arguments multiplied together - T.one() when there are none.

Math.mean

#math-mean
Math namespace

The arithmetic mean of the arguments. No arguments is an EmptyError.

Math.median

#math-median
Math namespace

The middle argument once they're sorted - the mean of the two middle ones when there's an even number. No arguments is an EmptyError.

Math.floor

#math-floor
Math namespace

The largest whole number not greater than x.

Math.ceil

#math-ceil
Math namespace

The smallest whole number not less than x.

Math.round

#math-round
Math namespace

x rounded to decimals places after the point, halves away from zero.

Math.trunc

#math-trunc
Math namespace

x with its fractional part dropped - rounded toward zero.

Math.fract

#math-fract
Math namespace

The fractional part of x: x - Math.trunc(x).

Math.sqrt

#math-sqrt
Math namespace

The square root of x - NaN below zero.

Math.cbrt

#math-cbrt
Math namespace

The cube root of x, negative for a negative x.

Math.hypot

#math-hypot
Math namespace

The square root of the sum of the squares of the arguments - the length of a vector with those components - computed without overflowing on the way.

Math.pow

#math-pow
Math namespace

base raised to exponent - the same as base ^ exponent.

Math.exp

#math-exp
Math namespace

e raised to x.

Math.ln

#math-ln
Math namespace

The natural (base-e) logarithm of x.

Math.log

#math-log
Math namespace

The logarithm of x in base.

Math.log2

#math-log2
Math namespace

The base-2 logarithm of x.

Math.log10

#math-log10
Math namespace

The base-10 logarithm of x.

Math.sin

#math-sin
Math namespace

The sine of an angle in radians.

Math.cos

#math-cos
Math namespace

The cosine of an angle in radians.

Math.tan

#math-tan
Math namespace

The tangent of an angle in radians.

Math.asin

#math-asin
Math namespace

The angle, in radians from -π/2 to π/2, whose sine is x.

Math.acos

#math-acos
Math namespace

The angle, in radians from 0 to π, whose cosine is x.

Math.atan

#math-atan
Math namespace

The angle, in radians from -π/2 to π/2, whose tangent is x.

Math.atan2

#math-atan2
Math namespace

The angle, in radians from -π to π, from the positive x-axis to the point (x, y) - unlike atan(y / x), it tells the quadrants apart.

Math.sinh

#math-sinh
Math namespace

The hyperbolic sine of x.

Math.cosh

#math-cosh
Math namespace

The hyperbolic cosine of x.

Math.tanh

#math-tanh
Math namespace

The hyperbolic tangent of x.

Math.toRadians

#math-toradians
Math namespace

An angle in degrees converted to radians.

Math.toDegrees

#math-todegrees
Math namespace

An angle in radians converted to degrees.

Math.lerp

#math-lerp
Math namespace

The point t of the way from a to b: a at t = 0, b at t = 1. t outside 0 to 1 extrapolates.

Math.inverseLerp

#math-inverselerp
Math namespace

How far x is along the way from a to b - the t for which Math.lerp(a, b, t) is x.

Math.remap

#math-remap
Math namespace

x moved from one range to the same relative place in another - Math.remap(5, 0..<10, 100..<200) is 150.

Math.gcd

#math-gcd
Math namespace

The greatest common divisor of a and b - never negative, and 0 only when both are.

Math.lcm

#math-lcm
Math namespace

The least common multiple of a and b - never negative, and 0 when either is.

Math.factorial

#math-factorial
Math namespace

1 * 2 * ... * n, and 1 for 0. A result too large for the type is an OverflowError.

Math.binomial

#math-binomial
Math namespace

The number of ways to choose k items out of n - 0 when k > n.

Math.isPrime

#math-isprime
Math namespace

Whether n is a prime number.

Math.isNan

#math-isnan
Math namespace

Whether x is not-a-number. x == Math.nan() is always false, so this is the only way to check.

Math.isFinite

#math-isfinite
Math namespace

Whether x is neither infinite nor not-a-number.

Math.approxEquals

#math-approxequals
Math namespace

Whether a and b differ by at most tolerance, relative to the larger of the two (absolute below 1) - for comparing results of float arithmetic, where == is too strict.