Module utils

The Utils module provides a collection of utility functions for functional programming in Lua.

It includes functions for array manipulation such as concatenation, mapping, reduction, filtering, and finding elements, as well as a property equality checker.

Functions

matchesPattern (pattern, value, msg) Given a pattern, a value, and a message, returns whether there is a pattern match.
matchesSpec (msg, spec) Given a message and a spec, returns whether there is a spec match.
curry (fn, arity) Curries a function.
concat (a, b) Concat two Array Tables
reduce (fn, initial, t) Applies a function to each element of a table, reducing it to a single value.
map (fn, data) Applies a function to each element of an array table, mapping it to a new value.
filter (fn, data) Filters an array table based on a predicate function.
find (fn, t) Finds the first element in an array table that satisfies a predicate function.
propEq (propName, value, object) Checks if a property of an object is equal to a value.
reverse (data) Reverses an array table.
compose (...) Composes a series of functions into a single function.
prop (propName, object) Returns the value of a property of an object.
includes (val, t) Checks if an array table includes a value.
keys (t) Returns the keys of a table.
values (t) Returns the values of a table.

Tables

utils The utils table


Functions

matchesPattern (pattern, value, msg)
Given a pattern, a value, and a message, returns whether there is a pattern match.

Parameters:

  • pattern The pattern to match
  • value The value to check for in the pattern
  • msg The message to check for the pattern

Returns:

    {boolean} Whether there is a pattern match

Usage:

    utils.matchesPattern(pattern, value, msg)
matchesSpec (msg, spec)
Given a message and a spec, returns whether there is a spec match.

Parameters:

  • msg The message to check for the spec
  • spec The spec to check for in the message

Returns:

    {boolean} Whether there is a spec match

Usage:

    utils.matchesSpec(msg, spec)
curry (fn, arity)
Curries a function.

Parameters:

  • fn {function} The function to curry
  • arity {number} The arity of the function

Returns:

    {function} The curried function
concat (a, b)
Concat two Array Tables

Parameters:

Returns:

    {table} The concatenated array

Usage:

  • utils.concat(a)(b)
  • utils.concat({1, 2})({3, 4}) --> {1, 2, 3, 4}
reduce (fn, initial, t)
Applies a function to each element of a table, reducing it to a single value.

Parameters:

  • fn {function} The function to apply
  • initial The initial value
  • t {table} The table to reduce

Returns:

    The reduced value

Usage:

  • utils.reduce(fn)(initial)(t)
  • utils.reduce(function(acc, x) return acc + x end)(0)({1, 2, 3}) --> 6
map (fn, data)
Applies a function to each element of an array table, mapping it to a new value.

Parameters:

  • fn {function} The function to apply to each element
  • data {table} The table to map over

Returns:

    {table} The mapped table

Usage:

  • utils.map(fn)(t)
  • utils.map(function(x) return x * 2 end)({1, 2, 3}) --> {2, 4, 6}
filter (fn, data)
Filters an array table based on a predicate function.

Parameters:

  • fn {function} The predicate function to determine if an element should be included.
  • data {table} The array to filter

Returns:

    {table} The filtered table

Usage:

  • utils.filter(fn)(t)
  • utils.filter(function(x) return x > 1 end)({1, 2, 3}) --> {2,3}
find (fn, t)
Finds the first element in an array table that satisfies a predicate function.

Parameters:

  • fn {function} The predicate function to determine if an element should be included.
  • t {table} The array table to search

Returns:

    The first element that satisfies the predicate function

Usage:

  • utils.find(fn)(t)
  • utils.find(function(x) return x > 1 end)({1, 2, 3}) --> 2
propEq (propName, value, object)
Checks if a property of an object is equal to a value.

Parameters:

  • propName {string} The property name to check
  • value {string} The value to check against
  • object {table} The object to check

Returns:

    {boolean} Whether the property is equal to the value

Usage:

  • utils.propEq(propName)(value)(object)
  • utils.propEq("name")("Lua")({name = "Lua"}) --> true
reverse (data)
Reverses an array table.

Parameters:

  • data {table} The array table to reverse

Returns:

    {table} The reversed array table

Usage:

  • utils.reverse(data)
  • utils.reverse({1, 2, 3}) --> {3, 2, 1}
compose (...)
Composes a series of functions into a single function.

Parameters:

  • ... {function} The functions to compose

Returns:

    {function} The composed function

Usage:

  • utils.compose(fn1)(fn2)(fn3)(v)
  • utils.compose(function(x) return x + 1 end)(function(x) return x * 2 end)(3) --> 7
prop (propName, object)
Returns the value of a property of an object.

Parameters:

  • propName {string} The property name to get
  • object {table} The object to get the property from

Returns:

    The value of the property

Usage:

  • utils.prop(propName)(object)
  • utils.prop("name")({name = "Lua"}) --> "Lua"
includes (val, t)
Checks if an array table includes a value.

Parameters:

  • val The value to check for
  • t {table} The array table to check

Returns:

    {boolean} Whether the value is in the array table

Usage:

  • utils.includes(val)(t)
  • utils.includes(2)({1, 2, 3}) --> true
keys (t)
Returns the keys of a table.

Parameters:

  • t {table} The table to get the keys from

Returns:

    {table} The keys of the table

Usage:

  • utils.keys(t)
  • utils.keys({name = "Lua", age = 25}) --> {"name", "age"}
values (t)
Returns the values of a table.

Parameters:

  • t {table} The table to get the values from

Returns:

    {table} The values of the table

Usage:

  • utils.values(t)
  • utils.values({name = "Lua", age = 25}) --> {"Lua", 25}

Tables

utils
The utils table

Fields:

  • _version The version number of the utils module
  • matchesPattern The matchesPattern function
  • matchesSpec The matchesSpec function
  • curry The curry function
  • concat The concat function
  • reduce The reduce function
  • map The map function
  • filter The filter function
  • find The find function
  • propEq The propEq function
  • reverse The reverse function
  • compose The compose function
  • prop The prop function
  • includes The includes function
  • keys The keys function
  • values The values function
generated by LDoc 1.5.0 Last updated 2024-10-29 14:41:25