Predicator.Functions.SystemFunctions (predicator v6.0.0)

Copy Markdown View Source

Built-in helper functions for use in predicator expressions.

This module provides a collection of utility functions that can be called within predicator expressions using function call syntax. These functions operate on values from the evaluation context and return computed results.

Available Functions

String Functions

  • len(string) - Returns the length of a string
  • upper(string) - Converts string to uppercase
  • lower(string) - Converts string to lowercase
  • trim(string) - Removes leading and trailing whitespace
  • starts_with(string, prefix) - Tests whether a string starts with a prefix
  • ends_with(string, suffix) - Tests whether a string ends with a suffix
  • substring(string, start[, len]) - Extracts a substring from a start index
  • index_of(string, sub) - Finds the index of a substring, or -1

List Functions

  • concat(list1, list2) - Concatenates two lists

Examples

iex> Predicator.evaluate("len('hello')", %{})
{:ok, 5}

iex> Predicator.evaluate("upper('world')", %{})
{:ok, "WORLD"}

iex> Predicator.evaluate("starts_with('hello world', 'hello')", %{})
{:ok, true}

iex> Predicator.evaluate("ends_with('hello world', 'world')", %{})
{:ok, true}

iex> Predicator.evaluate("substring('hello world', 6)", %{})
{:ok, "world"}

iex> Predicator.evaluate("substring('hello world', 0, 5)", %{})
{:ok, "hello"}

iex> Predicator.evaluate("index_of('hello world', 'world')", %{})
{:ok, 6}

iex> Predicator.evaluate("index_of('hello world', 'nope')", %{})
{:ok, -1}

iex> Predicator.evaluate("concat([1, 2], [3])", %{})
{:ok, [1, 2, 3]}

Summary

Functions

Concatenates two lists.

Tests whether a string ends with a suffix.

Finds the index of a substring, or -1 if it is not present.

Returns the length of a string.

Converts a string to lowercase.

Tests whether a string starts with a prefix.

Extracts a substring from a start index, with an optional length.

Removes leading and trailing whitespace from a string.

Converts a string to uppercase.

Returns all system functions as a Predicator.FunctionProvider map.

Types

function_result()

@type function_result() :: {:ok, Predicator.Types.value()} | {:error, binary()}

Functions

call_concat(list, context)

Concatenates two lists.

call_ends_with(arg1, context)

@spec call_ends_with([Predicator.Types.value()], Predicator.Context.t()) ::
  function_result()

Tests whether a string ends with a suffix.

call_index_of(arg1, context)

Finds the index of a substring, or -1 if it is not present.

call_len(arg1, context)

Returns the length of a string.

call_lower(arg1, context)

Converts a string to lowercase.

call_starts_with(arg1, context)

@spec call_starts_with([Predicator.Types.value()], Predicator.Context.t()) ::
  function_result()

Tests whether a string starts with a prefix.

call_substring(arg1, context)

@spec call_substring([Predicator.Types.value()], Predicator.Context.t()) ::
  function_result()

Extracts a substring from a start index, with an optional length.

call_trim(arg1, context)

Removes leading and trailing whitespace from a string.

call_upper(arg1, context)

Converts a string to uppercase.

functions()

@spec functions() :: %{
  required(Predicator.FunctionProvider.name()) =>
    Predicator.FunctionProvider.entry()
}

Returns all system functions as a Predicator.FunctionProvider map.

Returns

A map where keys are function names and values are {arity, atom} pairs - atom naming a public call_*/2 function on this module.

Examples

iex> functions = Predicator.Functions.SystemFunctions.functions()
iex> Map.has_key?(functions, "len")
true
iex> {arity, _atom} = functions["len"]
iex> arity
1