langur

built-in functions

Built-in functions include the following.

Functions that accept regex use auto-stringification on the values to match. Some other functions use this as well.

general is to I/O
file number string regex
general regex
is to
I/O file
number string

general functions

  • all(FUNCTION, ARRAY)
  • all(REGEX, ARRAY)
  • all(FUNCTION, HASH)
  • all(REGEX, HASH)

returns Boolean indicating whether a validation FUNCTION or REGEX returns true for all values in an ARRAY or HASH

...or returns null when given an empty ARRAY or HASH

  • any(FUNCTION, ARRAY)
  • any(REGEX, ARRAY)
  • any(FUNCTION, HASH)
  • any(REGEX, HASH)

returns Boolean indicating whether a validation FUNCTION or REGEX returns true for any value in an ARRAY or HASH

...or returns null when given an empty ARRAY or HASH

  • arr(INTEGER)
  • arr(INTEGER, ANYTHING)

creates a new array with count of elements specified by a positive INTEGER; sets each element to null or to ANYTHING

  • benchmark(FUNCTION)
  • benchmark(FUNCTION, COUNT)

returns string representing time required to execute the FUNCTION, the number of times given by the COUNT (default 1)

The FUNCTION must be one that takes no parameters. COUNT must be a positive integer.

  • best(FUNCTION, ARRAY)
  • best(FUNCTION, HASH)
  • best(FUNCTION, STRING)
  • best(FUNCTION, ARRAY, ALT_VALUE)
  • best(FUNCTION, HASH, ALT_VALUE)
  • best(FUNCTION, STRING, ALT_VALUE)

returns "best" element, comparing each by a FUNCTION taking two variables and returning a Boolean such as f .a < .b

alternatively, compares using the less than operator, taking a FUNCTION taking one variable and returing some value

returns ALT_VALUE for an empty ARRAY, HASH, or STRING (an exception if no ALT_VALUE specified)

see also bestkey(), nn(), ns(), good()

  • bestkey(FUNCTION, ARRAY)
  • bestkey(FUNCTION, HASH)
  • bestkey(FUNCTION, STRING)
  • bestkey(FUNCTION, ARRAY, ALT_VALUE)
  • bestkey(FUNCTION, HASH, ALT_VALUE)
  • bestkey(FUNCTION, STRING, ALT_VALUE)

returns key of "best" element, comparing each by a FUNCTION taking two variables and returning a Boolean such as f .a < .b

alternatively, compares using the less than operator, taking a FUNCTION taking one variable and returing some value

returns ALT_VALUE for an empty ARRAY, HASH, or STRING (an exception if no ALT_VALUE specified)

see also best(), nn(), ns(), good()

  • bestTag(PREF_TAGS, AVAIL_TAGS)

takes 2 arrays of language tag strings (IETF BCP 47); picks closest available and returns hash with results (including fields "tag", "index", and "confidence")

  • count(FUNCTION, ARRAY)
  • count(REGEX, ARRAY)
  • count(FUNCTION, HASH)
  • count(REGEX, HASH)

returns count of values that the FUNCTION or REGEX returns true for

see also where(), wherekeys(), any(), all()

  • execT(STRING)

executes a system command STRING, using a trusted source (not for use with user-provided data)

returns a result or throws an exception

see also execTh()

  • execTh(STRING)

executes a system command STRING, using a trusted source (not for use with user-provided data)

returns a hash instead of a result or exception

see also execT()

  • exit()
  • exit(CODE)

exits a script with an integer CODE (default 0), or with 1 if not an integer (does not throw)

  • first(ARRAY)
  • first(STRING)
  • first(RANGE)

returns first element of the ARRAY, STRING, or RANGE

see also last()

  • fold(FUNCTION, ARRAY)

returns value folded by the FUNCTION from the ARRAY

example:
val .sum = f(.arr) fold f .x+.y, .arr

see also foldfrom(), map(), zip()

  • foldfrom(FUNCTION, INIT, ARRAYS...)

returns value folded by a FUNCTION from 1 or more ARRAYS, starting with an INIT value

FUNCTION parameter count == number of ARRAYS + 1 for the result (result as first parameter in FUNCTION)

see also fold(), map(), zip()

  • good(FUNCTION, ARRAY)
  • good(REGEX, ARRAY)
  • good(FUNCTION, ARRAY, ALT_VALUE)
  • good(REGEX, ARRAY, ALT_VALUE)

returns first element from an ARRAY that a validation FUNCTION or REGEX returns true for

returns ALT_VALUE if no suitable value found (an exception if no ALT_VALUE specified)

see also nn(), ns(), best(), and bestkey()

  • group(FUNCTION, ARRAY)
  • group(MAX, ARRAY)

groups ARRAY elements into an array of arrays, based on the return value of a FUNCTION or a MAX count (per grouping)

  • haskey(ARRAY, INDEX)
  • haskey(STRING, INDEX)
  • haskey(HASH, INDEX)
  • haskey(OTHER, INDEX)

checks if complete INDEX exists in HASH, ARRAY, or STRING

INDEX may be anything valid (could include array of values, ranges, etc.)

returns Boolean if indexable, or null for OTHER

  • keys(HASH)
  • keys(ARRAY)
  • keys(STRING)

returns array of keys from a HASH (in no guaranteed order), or the 1-based indices of an ARRAY or STRING

  • last(ARRAY)
  • last(STRING)
  • last(RANGE)

returns last element of the ARRAY, STRING, or RANGE

see also first()

  • len(ARRAY)
  • len(STRING)
  • len(HASH)

returns the length (as integer) of an ARRAY, HASH, or STRING

STRING length in code points

  • less(ARRAY)
  • less(ARRAY, KEY)
  • less(ARRAY, KEYS)
  • less(STRING)
  • less(STRING, KEY)
  • less(STRING, KEYS)
  • less(HASH, KEY)
  • less(HASH, KEYS)

creates new array, string, or hash, removing the last element, or returns empty item if the length is already 0

...or leaving out by KEY or array of KEYS

does not alter the original ARRAY, STRING, or HASH

may return empty array, string, or hash

see also more() and rest()

  • map(FUNCTION, ARRAYS...)
  • map(FUNCTION, HASHES...)

returns array (or hash) of values mapped to a FUNCTION from 1 or more ARRAYS or HASHES

see also fold(), foldfrom(), zip()

  • more(ARRAY, ITEMS...)
  • more(HASH, HASHES...)
  • more(STRING, ITEMS...)

creates new array, hash, or string, adding 1 or more ITEMS or HASHES

does not alter the original ARRAY, HASH, or STRING

for a STRING, may add strings or code points

for a HASH, throws on duplicate keys (use concatenation to overwrite keys if desired)

see also less() and rest()

  • nn(ARRAY)
  • nn(ARRAY, ALT_VALUE)

returns first element from an ARRAY that is not null

returns ALT_VALUE if no suitable value found (an exception if no ALT_VALUE specified)

see also ns(), good(), best(), and bestkey()

  • ns(ARRAY)
  • ns(ARRAY, ALT_VALUE)

returns first element from an ARRAY that is not special (null, NaN, or Infinity)

returns ALT_VALUE if no suitable value found (an exception if no ALT_VALUE specified)

see also nn(), good(), best(), and bestkey()

  • pseries(RANGE)
  • pseries(RANGE, INCREMENT)
  • pseries(NUMBER)
  • pseries(NUMBER, INCREMENT)

generates an ascending (or "positive") array of numbers from a RANGE and INCREMENT (default 1)

for descending RANGE, returns empty array

may use NUMBER as implicit 1-based range

RANGE and INCREMENT not limited to integers

see also series()

  • rest(ARRAY)
  • rest(STRING)

creates new array or string, removing the first element, or returns empty item if the length is already 0

does not alter the original ARRAY or STRING

may return empty array or string

see also more() and less()

  • reverse(ARRAY)
  • reverse(HASH)
  • reverse(RANGE)

reverses order of ARRAY or RANGE elements

reverses keys/values of HASH if possible

  • rotate(ARRAY)
  • rotate(ARRAY, DISTANCE)
  • rotate(INTEGER, DISTANCE, RANGE)

rotates ARRAY elements by the DISTANCE given (default 1)

positive DISTANCE rotates to the "left" and negative to the "right"

for an INTEGER, rotates within RANGE; if outside RANGE, passed through unaltered

  • series(RANGE)
  • series(RANGE, INCREMENT)
  • series(NUMBER)
  • series(NUMBER, INCREMENT)

generates an array of numbers from a RANGE and INCREMENT (default 1 or -1)

may use NUMBER as implicit 1-based range

RANGE and INCREMENT not limited to integers

see also pseries()

  • sort(FUNCTION, ARRAY)
  • sort(ARRAY)
  • sort(FUNCTION, RANGE)
  • sort(RANGE)

returns a sorted array or range, comparing each element by a FUNCTION taking two variables and returning a Boolean such as f .a < .b

alternatively, compares using the less than operator, taking a FUNCTION taking one variable and returing some value, or no function at all

  • where(FUNCTION, ARRAY)
  • where(REGEX, ARRAY)
  • where(FUNCTION, HASH)
  • where(REGEX, HASH)

returns array or hash of values that the FUNCTION or REGEX returns true for

may return empty array or hash

see also wherekeys() and count()

  • wherekeys(FUNCTION, ARRAY)
  • wherekeys(REGEX, ARRAY)
  • wherekeys(FUNCTION, HASH)
  • wherekeys(REGEX, HASH)

returns array of keys for values that the FUNCTION or REGEX returns true for

may return empty array

see also where() and count()

  • zip(ARRAYS...)
  • zip(FUNCTION, ARRAYS...)

returns new array built by interleaving ARRAYS

alternatively, takes a FUNCTION to determine set of elements to append

The functionality of zip() could be duplicated with foldfrom() in a more verbose manner.

see also map(), fold(), foldfrom()

number functions

  • abs(NUMBER)

returns the absolute value of a NUMBER

  • ceiling(NUMBER)

returns the least integer equal to or greater than a NUMBER

see also floor()

  • floor(NUMBER)

returns the greatest integer equal to or less than a NUMBER

see also ceiling()

  • max(ARRAY)
  • max(HASH)
  • max(RANGE)
  • max(STRING)
  • max(FUNCTION)

returns the maximum item from an ARRAY, HASH, RANGE, STRING, or FUNCTION

for STRING, returns the maximum code point number

for FUNCTION, returns the maximum parameter count (-1 for no maximum)

see also min()

  • mean(ARRAY)
  • mean(HASH)
  • mean(RANGE)

returns the mean (average) from an ARRAY, HASH, or RANGE of values

see also mid()

  • mid(ARRAY)
  • mid(HASH)
  • mid(RANGE)

returns the mid-point from an ARRAY, HASH, or RANGE of values

see also mean()

  • min(ARRAY)
  • min(HASH)
  • min(RANGE)
  • min(STRING)
  • min(FUNCTION)

returns the minimum item from an ARRAY, HASH, RANGE, STRING, or FUNCTION

for STRING, returns the minimum code point number

for FUNCTION, returns the minimum parameter count

see also max()

  • round(NUMBER)
  • round(NUMBER, COUNT)

rounds NUMBER to specified digit COUNT after decimal point (default 0)

COUNT < 0 means to simplify (remove trailing zeros)

Rounding is always done with the round-half-even method. A future version might allow for other rounding methods.

see also truncate()

  • random(INTEGER)
  • random(RANGE)
  • random(ARRAY)
  • random(HASH)
  • random(STRING)

for an INTEGER, returns 0 if 0, a random number from 1 to the INTEGER if positive, or -1 to the INTEGER if negative

for a non-empty ARRAY, HASH, or STRING, returns 1 element at random (code point for a STRING)

all numbers within limits of a signed 64-bit integer

  • simplify(NUMBER)

simplifies NUMBER, removing trailing zeros

  • truncate(NUMBER)
  • truncate(NUMBER, COUNT)

truncates NUMBER to specified digit COUNT after decimal point (default 0)

COUNT < 0 means to simplify (remove trailing zeros)

see also round()

string functions

Some functions, such as split() and replace(), are listed under regex functions, though they aren't just for use with regex.

  • b2s(INTEGER)
  • b2s(INTEGER_ARRAY)

converts UTF-8 byte or byte array (integers) into a string

see also s2b(), s2cp(), cp2s(), s2s()

  • cp2s(INTEGER)
  • cp2s(INTEGER_ARRAY)

converts code points (integers) into a string

see also s2cp(), s2s(), s2b(), b2s()

  • join(ARRAY)
  • join(DELIMITER, ARRAY)

creates a string from ARRAY, using the specified DELIMITER string between each entry

uses auto-stringification on all elements of the ARRAY passed (so you don't have to explicitly map them to strings first)

if no DELIMITER passed, defaults to zero-length string

see also split(), cp2s()

  • lcase(STRING)

converts STRING to lowercase

see also ucase() and tcase()

  • ltrim(STRING)

removes preceding Unicode spacing

see also trim() and rtrim()

  • nfc(STRING)

converts STRING to normalization form NFC

see also nfd(), nfkc(), nfkd()

  • nfd(STRING)

converts STRING to normalization form NFD

see also nfc(), nfkc(), nfkd()

  • nfkc(STRING)

converts STRING to normalization form NFKC

see also nfc(), nfd(), nfkd()

  • nfkd(STRING)

converts STRING to normalization form NFKD

see also nfc(), nfd(), nfkc()

  • rtrim(STRING)

removes trailing Unicode spacing

see also trim() and ltrim()

  • s2cp(STRING)
  • s2cp(STRING, INDEX)
  • s2cp(STRING, INDEX, ALTERNATE)

returns code point (integer) or array of code points, or ALTERNATE value for an invalid INDEX

see also cp2s(), s2s(), s2b(), b2s()

  • s2s(STRING)
  • s2s(STRING, INDEX)
  • s2s(STRING, INDEX, ALTERNATE)

returns string built by index, or ALTERNATE value for an invalid INDEX

see also s2cp(), cp2s(), s2b(), b2s()

  • s2b(STRING)

returns UTF-8 byte array

see also b2s(), s2cp(), cp2s(), s2s()

  • tcase(STRING)

converts STRING to titlecase

see also ucase() and lcase()

  • trim(STRING)

removes preceding/trailing Unicode spacing

see also ltrim() and rtrim()

  • ucase(STRING)

converts STRING to uppercase

see also lcase() and tcase()

regex

The regex functions understand all regex types available in langur.

To generate a regex object for these functions, you use a regex literal (such as re/pattern/) or a compile function (such as reCompile()). You could also assign regex to a variable that may be passed to these functions. The regex is compiled before the functions see it.

For the pattern, some of these functions accept plain strings (not strictly regex functions).

These functions use auto-stringification on the argument to match. That's why they specify ANYTHING as an argument in the descriptions below.

  • index(REGEX, ANYTHING)
  • index(STRING, ANYTHING)
  • index(REGEX, ANYTHING, ALT_RETURN)
  • index(STRING, ANYTHING, ALT_RETURN)

returns 1-based code point range for match

for no match, returns null or ALT_RETURN value

  • indices(REGEX, ANYTHING)
  • indices(STRING, ANYTHING)
  • indices(REGEX, ANYTHING, MAX)
  • indices(STRING, ANYTHING, MAX)

returns array of ranges (empty array if no matches)

  • match(REGEX, ANYTHING)
  • match(REGEX, ANYTHING, ALT_RETURN)

returns matching string

for no match, returns null or ALT_RETURN value

  • matches(REGEX, ANYTHING)
  • matches(REGEX, ANYTHING, MAX)

returns array of matches (empty array if no matches)

  • matching(REGEX, ANYTHING)
  • matching(STRING, ANYTHING)

returns Boolean indicating whether there is a match

  • replace(ANYTHING, REGEX)
  • replace(ANYTHING, STRING)
  • replace(ANYTHING, REGEX, REPLACEMENT)
  • replace(ANYTHING, STRING, REPLACEMENT)
  • replace(ANYTHING, REGEX, FUNCTION)
  • replace(ANYTHING, STRING, FUNCTION)
  • replace(ANYTHING, REGEX, REPLACEMENT, MAX)
  • replace(ANYTHING, STRING, REPLACEMENT, MAX)
  • replace(ANYTHING, REGEX, FUNCTION, MAX)
  • replace(ANYTHING, STRING, FUNCTION, MAX)

returns string with replacements made progressively by REPLACEMENT string or by FUNCTION

if no REPLACEMENT or FUNCTION passed, defaults to zero-length string (replace with nothing)

  • split(ANYTHING)
  • split(REGEX, ANYTHING)
  • split(STRING, ANYTHING)
  • split(REGEX, ANYTHING, MAX)
  • split(STRING, ANYTHING, MAX)

returns array of strings split by either a REGEX or plain STRING delimiter

if passed 1 argument, delimiter defaults to zero-length string (split into code point strings)

if passed at least 2 arguments, the second one is never MAX

see also join(), s2cp()

  • subindex(REGEX, ANYTHING)

returns array of submatch ranges (empty array if not a match)

  • subindices(REGEX, ANYTHING)
  • subindices(REGEX, ANYTHING, MAX)

returns array of arrays of submatch ranges (empty array if no matches)

  • submatch(REGEX, ANYTHING)

returns array of submatches (empty array if not a match)

  • submatchH(REGEX, ANYTHING)

returns hash of whole match and submatches (empty hash if not a match)

includes whole match with key 0

includes named captures twice (once with string key and once with number key)

  • submatches(REGEX, ANYTHING)
  • submatches(REGEX, ANYTHING, MAX)

returns array of arrays of submatches (empty array if no matches)

  • submatchesH(REGEX, ANYTHING)
  • submatchesH(REGEX, ANYTHING, MAX)

returns array of hashes of whole matches and submatches (empty array if no matches)

includes whole match with key 0

includes named captures twice (once with string key and once with number key)

  • reCompile(STRING)

returns re2 regex compiled from a pattern STRING

  • reEsc(ANYTHING)

converts ANYTHING to a string, escapes re2 regex metacharacters and returns string

progressive matching

What is often called "global" matching we're calling "progressive." You can limit the results of these functions by passing a maximum count.

functions progressive?
matching, match, submatch, submatchH, index, subindex no
matches, submatches, submatchesH, indices, subindices, split, replace yes

is functions

These functions return a Boolean. They do not throw.

  • isArray
  • isBool
  • isBoolOrNull
  • isBuiltIn

checks for built-in function

  • isCallable

checks for built-in or user-defined function

  • isHash
  • isInfinity
  • isInteger

note: may have zeros after a decimal point and still be an integer

  • isNaN
  • isNull
  • isNumber

excludes NaN and Infinity
also does not check for numeric strings

  • isNumType

includes NaN and Infinity
does not check for numeric strings

  • isPosNumType

checks for a positive number type
includes positive Infinity
does not check for numeric strings

  • isRange
  • isRe

checks for an re2 regex

  • isRegex

checks for any type of regex available in langur

  • isSpecial

checks for a special value (null, NaN, or Infinity)

  • isString

to functions

These functions convert, or attempt to convert a value to another type. Some of them may throw exceptions.

  • toBool(ANYTHING)

The effect of toBool() is different than the "truthiness" used in logical operations.

  • true, "true": true
  • false, "false": false
  • null, "null", NaN, "NaN": false
  • 0, "0": false
  • non-zero number or trimmed non-zero number string: true
  • empty regex: false
  • empty array: false
  • empty hash: false
  • trimmed number string: converted to number and tested
  • unknown string: false
  • other: true

does not throw

  • toBoolOrNull(ANYTHING)
  • true, "true": true
  • false, "false": false
  • null, "null", NaN, "NaN": null
  • 0, "0": false
  • non-zero number or trimmed non-zero number string: true
  • empty regex: false
  • empty array: false
  • empty hash: false
  • trimmed number string: converted to number and tested
  • unknown string: null
  • other: true

does not throw

  • toHash(KEY_VALUE_ARRAY)
  • toHash(KEYS_ARRAY, VALUES_ARRAY)

attempts to create a hash from a single array of keys and values in series, or from parallel arrays of keys and values

see also keys()

  • toNumber(NUMBER)
  • toNumber(BOOLEAN)
  • toNumber(STRING)
  • toNumber(STRING, BASE)

attempts to convert value to a number

excludes NaN and Infinity

see also toNumType() and toString()

  • toNumType(NUMBER)
  • toNumType(BOOLEAN)
  • toNumType(STRING)
  • toNumType(STRING, BASE)

attempts to convert value to a number type

includes NaN and Infinity

see also toNumber() and toString()

  • toString(ANYTHING)
  • toString(INTEGER, BASE)

converts to and returns string

does not throw if no BASE given

see also toNumber() and toNumType()

I/O functions

  • read()
  • read(PROMPT)
  • read(PROMPT, REGEX)
  • read(PROMPT, FUNCTION)
  • read(PROMPT, REGEX, ERR_STRING)
  • read(PROMPT, FUNCTION, ERR_STRING)
  • read(PROMPT, REGEX, ERR_STRING, MAX)
  • read(PROMPT, FUNCTION, ERR_STRING, MAX)
  • read(PROMPT, REGEX, ERR_STRING, MAX, ALT_VALUE)
  • read(PROMPT, FUNCTION, ERR_STRING, MAX, ALT_VALUE)

reads a string from standard in

may print a PROMPT string to standard out for each attempt

may use validation via a FUNCTION taking one argument such as f len(.in) < 20 or a REGEX such as RE/^\d+$/

prints an ERR_STRING if validation fails (default empty string)

tries the MAX number of times given (default 1; -1 == infinite)

may return an ALT_VALUE on final failure instead of an exception

  • write(ARGS...)

writes a string of all arguments to standard out

uses auto-stringification

  • writeErr(ARGS...)

writes a string of all arguments to standard error

uses auto-stringification

  • writeln(ARGS...)

writes a string of all arguments to standard out, appending one system newline

uses auto-stringification

  • writelnErr(ARGS...)

writes a string of all arguments to standard error, appending one system newline

uses auto-stringification

file functions

New file permissions, if not passed, default to 664. In langur, you would write this as 8x664 (NOT 0664 or 664, which would give the wrong number).

  • appendfile(FILENAME, STRING)
  • appendfile(FILENAME, STRING, PERMISSIONS)

appends STRING to given file; creates file if it doesn't exist

see also writefile()

  • readfile(FILENAME)

returns string from given file

  • writefile(FILENAME, STRING)
  • writefile(FILENAME, STRING, PERMISSIONS)

writes STRING to given file

see also appendfile()