Skip to content

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.


Returns true if all entries in the given container are "truthy".

>>> all([true, 1, "ok"])
>>> all([true, 0, "ok"])


Returns true if any of the entries in the given container are "truthy".

>>> any([false, 0, "ok"])
>>> any([false, 0, ""])

assert(x, message)

Generates an error if x is "falsy". If a message is provided, it is used as the assertion error message.

>>> assert(1 == 1, "check failed")
>>> assert(1 == 2, "check failed")
check failed


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.

>>> bool(1)
>>> bool(0)
>>> bool([1])
>>> bool([])

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.

>>> func inc(x) { x + 1 }
>>> call(inc, 99)
>>> inc | call(41)


Converts an Int to the corresponding unicode rune, which is returned as a String. The ord built-in performs the inverse transformation.

>>> chr(8364)
>>> chr(97)

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}


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.

>>> err("failed operation")
err("failed operation")


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.

>>> error("kaboom")


Converts a String or Int object to a Float. An error is generated if the operation fails.

>>> float("4.4")

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")
>>> append(4)
[1, 2, 3, 4]
>>> getattr(l, "unknown", "that doesn't exist")
"that doesn't exist"


Converts a String or Float to an Int. An error is generated if the operation fails.

>>> int(4.4)
>>> int("123")


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"})


Returns a list of all keys for items in the given map or list container.

>>> m := {one: 1, two: 2}
{"one": 1, "two": 2}
>>> keys(m)
["one", "two"]


Returns the size of the string, list, map, or set.

>>> len("ab")        // string length
>>> len([1,2,3])     // list length
>>> len({foo:"bar"}) // map length
>>> len({1,2,3,4})   // set length


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.

>>> s := {"a", "b", "c"}
{"a", "b", "c"}
>>> list(s)
["a", "b", "c"]


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()
>>> result.unwrap()
"that worked"


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.

>>> ord("€")
>>> ord("a")
>>> chr(ord("€"))


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.

>>> print(42, "is the answer")
42 is the answer

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.

>>> printf("name: %s age: %d\n", "joe", 32)
name: joe age: 32


Returns a new list which is a reversed copy of the provided list.

>>> l := ["a", "b", "c"]
["a", "b", "c"]
>>> reversed(l)
["c", "b", "a"]
>>> l
["a", "b", "c"]


Returns a new set containing the items from the given container object.

>>> set("aabbcc")
{"a", "b", "c"}
>>> set([4,4,5])
{4, 5}
>>> set({one:1, two:2})
{"one", "two"}


Returns a sorted list of items from the given container object.

>>> sorted("cba")
["a", "b", "c"]
>>> sorted([10, 3, -5])
[-5, 3, 10]

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]"


Returns a string representation of the given Tamarin object.

>>> string({one:1, two:2})
"{\"one\": 1, \"two\": 2}"
>>> string(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")
>>> 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"


Returns the type name of the given object as a String.

>>> type(1)
>>> type(2.2)
>>> type("hi")
>>> type([])
>>> type({})
>>> type({1,2,3})
>>> type(ok("success"))
>>> type(err("failed"))

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")
>>> unwrap_or(err("boom"), "fallback")


Returns the wrapped "ok" value from the given Result object. If the Result contains an Error, an Error is generated instead.

>>> unwrap(ok("success"))
>>> unwrap(err("boom"))
result error: unwrap() called on an error: error("boom")