Built-in Functions
Tamarin includes this set of default built-in functions. The set of available built-ins is easily customizable, depending on the goals for your project.
all(container)
Returns true
if all entries in the given container are "truthy".
any(container)
Returns true
if any of the entries in the given container are "truthy".
assert(x, message)
Generates an error if x
is "falsy". If a message is provided, it is used as
the assertion error message.
bool(object)
Returns true
or false
depending on whether the object is considered "truthy".
Container types including lists, maps, sets, and strings evaluate to false
when
empty and true
otherwise.
call(function, ...any)
Calls the function with given arguments. This is primarily useful in pipe expressions when a function is being passed through the pipe as a variable.
chr(int)
Converts an Int to the corresponding unicode rune, which is returned as a String.
The ord
built-in performs the inverse transformation.
delete(map, key)
Deletes the item with the specified key from the map. This operation has no effect if the key is not present in the map.
>>> m := {one: 1, two: 2}
{"one": 1, "two": 2}
>>> delete(m, "one")
{"two": 2}
>>> delete(m, "foo")
{"two": 2}
err(message)
Returns a new Result object containing the given error message. Results may
contain an ok
value or an err
value, and this built-in is used to construct
the latter. This is similar in some ways to Rust's result type.
error(message)
Generates an Error containing the given message. Errors in Tamarin stop evaluation
when they are generated, unless a try
call is used to stop error propagation.
float(object)
Converts a String or Int object to a Float. An error is generated if the operation fails.
getattr(object, name, default)
Returns the named attribute from the object, or the default value if the attribute does not exist. The returned attribute is always a Tamarin object, which may be a function. This is similar to getattr in Python.
>>> l := [1,2,3]
[1, 2, 3]
>>> append := getattr(l, "append")
builtin(list.append)
>>> append(4)
[1, 2, 3, 4]
>>> getattr(l, "unknown", "that doesn't exist")
"that doesn't exist"
int(object)
Converts a String or Float to an Int. An error is generated if the operation fails.
iter(container)
Returns an iterator for the given container object. This can be used to iterate
through items in a for loop or interacted with more directly. The returned
iterator has a next()
method that will return iter_entry
objects.
>>> s := {"a", "b", "c"}
{"a", "b", "c"}
>>> iterator := iter(s)
set_iter({"a", "b", "c"})
>>> iterator.next().key
"a"
keys(container)
Returns a list of all keys for items in the given map or list container.
len(container)
Returns the size of the string, list, map, or set.
>>> len("ab") // string length
2
>>> len([1,2,3]) // list length
3
>>> len({foo:"bar"}) // map length
1
>>> len({1,2,3,4}) // set length
4
list(container)
Returns a new list populated with items from the given container. If a list is provided, a shallow copy of the list is returned. It is also commonly used to convert a set to a list.
ok(object)
Returns a new Result containing the given ok value. Results may contain an ok
value or an err
value, and this built-in is used to construct the former. This
is similar in some ways to Rust's result type.
>>> result := ok("that worked")
ok("that worked")
>>> result.is_ok()
true
>>> result.unwrap()
"that worked"
ord(string)
Converts a unicode character to the corresponding Int value. The chr
built-in
performs the inverse transformation. An error is generated if a multi-rune string is
provided.
print(...any)
Prints the provided objects to stdout after converting them to their String
representations. Spaces are inserted between each object and a trailing newline
is output. This is a wrapper around fmt.Println
in Go.
printf(string, ...any)
Printf wraps fmt.Printf
in order to print the formatted string and arguments
to stdout. In the Tamarin REPL you will currently not see the printf
output
unless the string ends in a newline character.
reversed(list)
Returns a new list which is a reversed copy of the provided list.
set(container)
Returns a new set containing the items from the given container object.
sorted(container)
Returns a sorted list of items from the given container object.
sprintf(string, ...any)
Wraps fmt.Sprintf
to format the string with the provided arguments. Tamarin
objects are converted to their corresponding Go types before being passed to
fmt.Sprintf
.
>>> sprintf("name: %s age: %d", "fred", 18)
"name: fred age: 18"
>>> sprintf("%v", [1, "a", 3.3])
"[1 a 3.3]"
string(object)
Returns a string representation of the given Tamarin object.
>>> string({one:1, two:2})
"{\"one\": 1, \"two\": 2}"
>>> string(4.4)
"4.4"
>>> string([1,2,3])
"[1, 2, 3]"
try(expression, fallback)
Evaluates the expression and return the fallback value if an error occurs. This works equally well with Error objects and Results containing an error value.
If a function is provided as the fallback value, that function is called when
an error occurs with the error message as the first argument. The value returned
from the function is used as the return value from the try
call.
>>> try("ok", "fallback")
"ok"
>>> try(error("boom"), "that failed")
"that failed"
>>> try(error("boom"), error("transformed err"))
transformed err
>>> r := err("err result")
err("err result")
>>> try(r, "the result contained an error")
"the result contained an error"
>>> try(r, func(msg) { 'failure: {msg}' })
"failure: err result"
type(object)
Returns the type name of the given object as a String.
>>> type(1)
"int"
>>> type(2.2)
"float"
>>> type("hi")
"string"
>>> type([])
"list"
>>> type({})
"map"
>>> type({1,2,3})
"set"
>>> type(ok("success"))
"result"
>>> type(err("failed"))
"result"
unwrap_or(result, fallback)
Returns the wrapped "ok" value from the given Result object or the fallback value if the Result contains an Error.
>>> unwrap_or(ok("success"), "fallback")
"success"
>>> unwrap_or(err("boom"), "fallback")
"fallback"
unwrap(result)
Returns the wrapped "ok" value from the given Result object. If the Result contains an Error, an Error is generated instead.