6.3.9. Common Utilities
These utility functions are provided in the utils package.
You can access the following functions in the substrate REPL To run the REPL:
Returns the first item of the first list of the outer list.
Takes the first inner list and returns the item after the first value.
Returns the first item from the outer list after the first value.
Returns the last item of the last item of a list.
Interprets a truthy value, returning a boolean.
Perform logical implies on the two arguments:
- If the first argument is truthy, return the boolean value of the second argument
- If the first argument if false, return true
when macro acts as a "one-armed if" expression but also allows multiple consequent expressions which are evaluated as if in a progn.
unless macro acts as a "one-armed if not" expression but also allows multiple consequent expressions which are evaluated as if in a progn.
while macro emulates the C "while" block. If predicate-expression evaluates to true, consequent-expressions are evaluated and then the process repeats. If and when the loop terminates, the predicate-expression will have just evaluated to false. The macro always evaluates to
x if truthy, and
x if the string isn't empty (
Returns a function which will call FUN passing it INITIAL-ARGS and then any other args.
Returns a empty vector if
x is nil, and
Returns false for non-boolean values or
x for booleans.
Checks if the value can be interpreted as a int and is non-zero.
Checks if the value can be interpreted as a float and is non-zero.
Checks if the value is a string and isn't empty.
Returns nil for empty strings, otherwise returns the input string.
Checks if the value is a number.
Checks if the value can be interpreted as a number and is non-zero.
Checks if the value is a sequence (list or vector).
Checks if the value is a non-empty sequence.
Checks if the value is a non-empty map.
Checks the value is non-empty and substantial across types.
Zero pads the number with a width of 20.
Zero pads to penny width.
Checks if the string is in the string sequence.
Determines that all strings in seq
s are in seq
Checks if the string has the given prefix.
Gets a string substring.
Checks if the string contains only numeric digits.
Checks if the string contains only hexadecimal digits.
Checks if the string is a universally unique identifier.
Checks if the strings match, supports
? to mach any character.
Counts the items matched by the predicate function.
Returns the sequence values with indexes.
foldl sequence pairs.
Deduplicate a sequence of hashable values.
deduplicate, but only for list sequences.
deduplicate, but only for vector sequences.
Creates a sorted-map with string keys mapping to true values.
elps> (make-string-set '("A" "B" "C" "D" "C" "B" "A")) (sorted-map "A" true "B" true "C" true "D" true)
Constructs a new map by taking the union of keys, vals of maps m1 and m2, where m2 value override m1 values where both maps share a common key.
Create a sorted-map containing the subset of m's values associated with keys in the sequence, map-keys. Any elements of map-keys that are not keys of m will not be keys of the returned submap either.
Returns true if all strings in sequence s1 are unique.
Performs a union of two string sequences.
Returns unique elements present in both sequences.
Returns unique elements present in both sequences.
Returns unique elements of sequence minuend which are not present in sequence subtrahend.
Returns unique strings that are in seqa or seqb, but are not in both.
Returns true if the unique strings in seqa are exactly same in seqb.
Clears map values, mutating the input map.
Works closer to the way that
equal? is supposed to for structured data.
Filters out nil values from a sequence.
Joins a list of strings with commas, and adds an "and" before the last element. It uses oxford comma style.
Remove leading spaces from a string.
Remove trailing spaces from a string.
Removes leading and trailing spaces from a string.
Tries to construct a single full name string based on optional name fields.
Encode a map to be used as part of a storage key delimited by ":". The encoding doesn't use ":" so that two maps may be used side-by-side in a storage key without ambiguity.
Map a function on a sequence, where the first argument to the function is
i and the second argument to the function is the
i-th element. Returns a list.
Performs intersection across a sequence of string sequences.
find-pos iterates across seq and returns the last position that fn returned true.
If no element is found, 0 is returned, which requires special handling when the element found is the first element.
Add 2 decimal strings, and return a decimal string. DEPRECATED: Please use
libdecimal instead of these decimal-money functions.
An optimization technique by storing the results of expensive function calls and returning the cached result when the same inputs occur again.
Returns current UTC time.
Returns current time as hexidecimal of the unix epoch time in seconds.
Returns everything but the first item of the first list, of the outer list.
Returns number range.
Returns number prefix.
Returns pennies range.
Returns pennies prefix.
Returns a substring without a provided prefix.
Returns digit to true sorted map.
Returns hex digit to true sorted map.
Returns hex digit to number sorted map.
Return current unix epoch time in seconds.
Mutates the first vector to include the second vector as the last elements.
Get metadata (transaction metadata is stored in the state database under the transaction ID) with string key from the transaction context.
Returns a specified entry in the metadata field of an entity.
Returns the string value for a specific entry in the metadata field of an entity.
Returns the integer value for a specific entry in the metadata field of an entity.
Ensures the 2 arguments are deeply equal. Used in testing.
Ensures the 2 arguments are NOT deeply equal. Used in testing.
Expects an exception or error to be returned and handles accordingly.
Converts a timestamp to a date.
Returns today's date.
Returns the number of years, months, and days in between 2 dates as a vector.
Extracts the year, month, and date and returns that as a vector.
Returns the integer input in money format.
Takes a vector of the dollar amount and cent amount as input and returns in money format.
Takes a money amount as input and returns a vector of the dollar amount and the cent amount.
Whether the first money amount is larger than the second.