Functions can be used in expressions in Cozo. All function arguments in Cozo are immutable. All functions except those having names starting with ``rand_`` are deterministic.
Internally, all function arguments are partially evaluated before binding variables to input tuples. For example, the regular expression in ``regex_matches(var, '[a-zA-Z]+')`` will only be compiled once during the execution of the query, instead of being repeatedly compiled for every input tuple.
Equality comparison. The operator form is ``x == y`` or ``x = y``. The two arguments of the equality can be of different types, in which case the result is ``false``.
Inequality comparison. The operator form is ``x != y``. The two arguments of the equality can be of different types, in which case the result is ``true``.
..function:: gt(x, y)
Equivalent to ``x > y``
..function:: ge(x, y)
Equivalent to ``x >= y``
..function:: lt(x, y)
Equivalent to ``x < y``
..function:: le(x, y)
Equivalent to ``x <= y``
..NOTE::
The four comparison operators can only compare values of the same value type. Integers and floats are of the same type ``Number``.
..function:: max(x, ...)
Returns the maximum of the arguments. Can only be applied to numbers.
..function:: min(x, ...)
Returns the minimum of the arguments. Can only be applied to numbers.
Variadic addition. The binary version is the same as ``x + y``.
..function:: sub(x, y)
Equivalent to ``x - y``.
..function:: mul(...)
Variadic multiplication. The binary version is the same as ``x * y``.
..function:: div(x, y)
Equivalent to ``x / y``.
..function:: minus(x)
Equivalent to ``-x``.
..function:: pow(x, y)
Raises ``x`` to the power of ``y``. Equivalent to ``x ^ y``. Always returns floating number.
..function:: mod(x, y)
Returns the remainder when ``x`` is divided by ``y``. Arguments can be floats. The returned value has the same sign as ``x``. Equivalent to ``x % y``.
..function:: abs(x)
Returns the absolute value.
..function:: signum(x)
Returns ``1``, ``0`` or ``-1``, whichever has the same sign as the argument, e.g. ``signum(to_float('NEG_INFINITY')) == -1``, ``signum(0.0) == 0``, but ``signum(-0.0) == -1``. Returns ``NAN`` when applied to ``NAN``.
..function:: floor(x)
Returns the floor of ``x``.
..function:: ceil(x)
Returns the ceiling of ``x``.
..function:: round(x)
Returns the nearest integer to the argument (represented as Float if the argument itself is a Float). Round halfway cases away from zero. E.g. ``round(0.5) == 1.0``, ``round(-0.5) == -1.0``, ``round(1.4) == 1.0``.
..function:: exp(x)
Returns the exponential of the argument, natural base.
..function:: exp2(x)
Returns the exponential base 2 of the argument. Always returns a float.
Computes with the `haversine formula <https://en.wikipedia.org/wiki/Haversine_formula>`_ the angle measured in radians between two points ``a`` and ``b`` on a sphere specified by their latitudes and longitudes. The inputs are in radians. You probably want the next function since most maps measure angles in radians.
Same as the previous function, but the inputs are in degrees instead of radians. The return value is still in radians. If you want the approximate distance measured on the surface of the earth instead of the angle between two points, multiply the result by the radius of the earth, which is about ``6371`` kilometres, ``3959`` miles, or ``3440`` nautical miles.
Returns the number of Unicode characters in the string.
Can also be applied to a list or a byte array.
..WARNING::
``length(str)`` does not return the number of bytes of the string representation. Also, what is returned depends on the normalization of the string. So if such details are important, apply ``unicode_normalize`` before ``length``.
..function:: concat(x, ...)
Concatenates strings. Equivalent to ``x ++ y`` in the binary case.
Can also be applied to lists.
..function:: str_includes(x, y)
Returns ``true`` if ``x`` contains the substring ``y``, ``false`` otherwise.
``starts_with(var, str)`` is prefered over equivalent (e.g. regex) conditions, since the compiler may more easily compile the clause into a range scan.
Converts ``str`` to the `normalization <https://en.wikipedia.org/wiki/Unicode_equivalence>`_ specified by ``norm``. The valid values of ``norm`` are ``'nfc'``, ``'nfd'``, ``'nfkc'`` and ``'nfkd'``.
Returns Unicode characters of the string as a list of substrings.
..function:: from_substrings(list)
Combines the strings in ``list`` into a big string. In a sense, it is the inverse function of ``chars``.
..WARNING::
If you want substring slices, indexing strings, etc., first convert the string to a list with ``chars``, do the manipulation on the list, and then recombine with ``from_substring``. Hopefully, the omission of functions doing such things directly can make people more aware of the complexities involved in manipulating strings (and getting the *correct* result).
Constructs a list from its argument, e.g. ``list(1, 2, 3)``. Equivalent to the literal form ``[1, 2, 3]``.
..function:: is_in(el, list)
Tests the membership of an element in a list.
..function:: first(l)
Extracts the first element of the list. Returns ``null`` if given an empty list.
..function:: last(l)
Extracts the last element of the list. Returns ``null`` if given an empty list.
..function:: get(l, n)
Returns the element at index ``n`` in the list ``l``. This function will raise an error if the access is out of bounds. Indices start with 0.
..function:: maybe_get(l, n)
Returns the element at index ``n`` in the list ``l``. This function will return ``null`` if the access is out of bounds. Indices start with 0.
..function:: length(list)
Returns the length of the list.
Can also be applied to a string or a byte array.
..function:: slice(l, start, end)
Returns the slice of list between the index ``start`` (inclusive) and ``end`` (exclusive). Negative numbers may be used, which is interpreted as counting from the end of the list. E.g. ``slice([1, 2, 3, 4], 1, 3) == [2, 3]``, ``slice([1, 2, 3, 4], 1, -1) == [2, 3]``.
..TIP::
The spread-unify operator ``var <- ..[1, 2, 3]`` is equivalent to ``is_in(var, [1, 2, 3])`` if ``var`` is bound.
..function:: concat(x, ...)
Concatenates lists. The binary case is equivalent to `x ++ y`.
Can also be applied to strings.
..function:: prepend(l, x)
Prepends ``x`` to ``l``.
..function:: append(l, x)
Appends ``x`` to ``l``.
..function:: reverse(l)
Reverses the list.
..function:: sorted(l)
Sorts the list and returns the sorted copy.
..function:: chunks(l, n)
Splits the list ``l`` into chunks of ``n``, e.g. ``chunks([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4], [5]]``.
..function:: chunks_exact(l, n)
Splits the list ``l`` into chunks of ``n``, discarding any trailing elements, e.g. ``chunks([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4]]``.
..function:: windows(l, n)
Splits the list ``l`` into overlapping windows of length ``n``. e.g. ``windows([1, 2, 3, 4, 5], 3) == [[1, 2, 3], [2, 3, 4], [3, 4, 5]]``.
..function:: union(x, y, ...)
Computes the set-theoretic union of all the list arguments.
..function:: intersection(x, y, ...)
Computes the set-theoretic intersection of all the list arguments.
..function:: difference(x, y, ...)
Computes the set-theoretic difference of the first argument with respect to the rest.
Convert ``x`` to a string: the argument is unchanged if it is already a string, otherwise its JSON string representation will be returned.
..function:: to_float(x)
Tries to convert ``x`` to a float. Conversion from numbers always succeeds. Conversion from strings has the following special cases in addition to the usual string representation:
*``INF`` is converted to infinity;
*``NEG_INF`` is converted to negative infinity;
*``NAN`` is converted to NAN (but don't compare NAN by equality, use ``is_nan`` instead);
*``PI`` is converted to pi (3.14159...);
*``E`` is converted to the base of natural logarithms, or Euler's constant (2.71828...).
Extracts the timestamp from a UUID version 1, as seconds since the UNIX epoch. If the UUID is not of version 1, ``null`` is returned. If ``x`` is not a UUID, an error is raised.