Skip to content

6) cm (Standard Library) Reference

Benjamin Pang edited this page Mar 22, 2018 · 17 revisions

Built-in functions are always prefixed and will only contain 1 or 2 arguments.

I/O

Name Arguments Info
eX boolean sets whether to output last evaluated result
rl n/a string from user input
rf file path as string file contents as string
os expression outputs (no trailing newline) as source, returns arg
ol expression outputs (no trailing newline) as string, returns arg
oN expression outputs (with trailing newline) as string, returns arg
wf file path as string, edits as string overwrites file with edit, returns edit

Math/Number

Almost all of these functions are done with the help of decimal.js. All trig-related functions use radians.

Name Arguments Info
E number sets decimal precision of numbers
fix number, number forces first argument to have decimal precision specified second argument, adding trailing zeroes if necessary
abs number absolute value of number
asin, acos, atan, asinh, acosh, atanh number arctrig functions
atant number, number uses 2nd arg to determine sign of the atan function
flr, trunc, ceil, round number rounding functions
sin, cos, tan, sinh, cosh, tanh number trig functions
add, sub, mul, div, mod, pow number, number arithmetic operations
exp number e^number
lt, ln number base 10 and natural log
log number, number 2nd number is base
sign number -1, 0, or 1 depending on the number's sign
neg number negates number
rnd number generates random float with given integer precision
hypot number, number (1st arg)^2 + (2nd arg)^2
hx, bn, ot number decimal to hex/binary/octal
fac number factorization
pr number primality test
pfac number prime factorization
gcd, lcm number, number factor-related functions

Logic

Name Arguments Info
cmp number, number -1, 0, or 1 if x is <, =, or > 1
lt, gt, lteq, gteq number, number comparison functions
eq expression, expression checks if 2 expressions are loosely equal
Eq expression, expression checks if 2 items are strictly equal (including types)
if expression, list w/ 2 functions if the expression is truthy, apply T to the 1st function in the list; otherwise, apply F to the 2nd function in the list
not expression logical NOT
and, or, xor boolean, boolean logical AND, OR, and XOR

Conversion

Most functions will do conversion for you; these are still quite useful though.

Name Arguments Info
eval string evaluate as expression
sh string execute shell command
js string execute Node.js code
fc number / list from charcode
tc string to charcode
bool expression to boolean
tru expression to boolean, but converts 0 to true
num string to number
str expression to string
src expression to source as string
type expression expression type
var variable, expression store expression under variable name
obj list w/ iterables of length>2 convert list to object w/ each iterable as a key-value pair
obl list convert list to object with indices as keys

Iterables

This includes lists, strings, numbers, and objects. Strings (and sometimes numbers) will often be treated as single-char lists.

Name Arguments Info
len iterable length; may hang on really large lists
con iterable, expression concatenation; if the second argument is a list, it will concatenate each item in the list rather than the list itself
rev iterable reverse
get number/string, iterable indexing; modular behavior on non-objects
iget, iset, iins number/string, iterable indexing; no modular behavior, safe for infinite lists
set list w/ index and expression, iterable change item at index to expression; also works on pattern matches
ins list w/ index and expression, iterable add expression to list at index; same as set when called on objects and pattern matches
tk number, iterable get the first n items in the iterable
dp number, iterable remove the first n items in the iterable
map function, iterable apply each item in list to function
fold list w/ 2-arg function and expression, iterable expression is accumulator, folds w/ function and iterable
foldr list w/ 2-arg function and expression, iterable same as fold but from the right
tkwl function, iterable takes elements from iterable until function returns truthy
drwl function, iterable drops elements from iterable until function returns truthy
fltr function, iterable remove elements that return falsy when applied to function
find function, iterable returns first element that returns truthy when applied to function
findi function, iterable returns index of first element that returns truthy when applied to function
every function, iterable returns whether every element return truthy when applied to function
some function, iterable returns whether some elements return truthy when applied to function
sort function, iterable sort iterable based on the results of applying each item to function
iO expression, iterable gets all indices in the iterable where the expression is equal to the item
fiO expression, iterable returns the first element in the list result of iO or F on failure; works on infinite lists
sum iterable add all items in iterable
prod iterable multiply all items in iterable
chunk number, iterable chunk the iterable into parts with lengths of number
shuf iterable randomizes iterable's order
unq iterable removes duplicates
inx iterable, iterable set intersection
uni iterable, iterable set union
dff iterable, iterable set difference
cns number, iterable get consecutive overlapping sequences of length number
ind iterable wrap each item in the iterable with a list containing the index of the item and the item itself
cyc iterable generate an infinite list that repeats each item in the iterable
perm, comb number, iterable generates all possible length-n permutations/combinations of the iterable's items
pows iterable generates a power set of the iterable's items
cprd list of iterables with length > 1 performs Cartesian product on the iterables
base number, iterable generates a length-n "number" base system using the iterable's items as digits

List

Name Arguments Info
tK number sets the amount of list items to output
max, min list of numbers maximum and minimum numbers
rng number, number exclusive range; can be an infinite range
join string, list join list using string
gen function generates an infinite list from applying each index to the function
genc function, expression generates an infinite list from repeatedly applying the previous list item to the function
rpt number, expression generates a list of length n with the expression as every item
tsp list w/ 2 iterables transpose; swaps rows with columns
zip function, list w/ 2 lists map sS over the 2nd argument transposed
flat list flatten list by 1 level

Object

Some of these function can be used on lists.

Name Arguments Info
key object list of keys
val object list of values
pk list of keys,object pick key-value pairs whose key appear in the list
om list of keys,object omit key-value pairs whose key appear in the list

String

Some of these functions may apply to numbers, too.

Name Arguments Info
split regex, string split 1st arg using 2nd arg
mstr regex, string returns an object with all regex matches on the string, excluding capture group matches
xstr regex, string returns an object with the first regex match on the string along with other data
sstr regex, string returns the index of the first match or F on fail
rstr list w/ regex and substitution function, string replace any matches in the string using the substitution function (which should take in a xstr-style list and return a substitution string/data) or string
Rstr list w/ regex and substitution function, string repeatedly applies rstr with arguments until regex no longer matches string
gstr regex, string returns a list of lines that the regex matches
Gstr regex, string returns a list of lines that the regex doesn't match
lc string lowercase
uc string uppercase

Function

Name Arguments Info
S expression, expression apply 2nd arg to 1st arg
ss list w/ functions, expression creates a composition of functions to apply to expression
sS function, list applies list items to function
K expression, expression return 1st arg
I expression return expression
tt function/partial application returns a function that applies arguments in reverse order; only works on builtins

Program Flow

Name Arguments Info
sleep number halt the program for number of microseconds
exit expression stops the program completely
while list w/ 2 functions, expression repeatedly applies expression to second function while expression applied to first function returns truthy
pkg string imports a package from the local dpm folder
Clone this wiki locally