Module f.lua
f.lua aims to be the most complete functional extension library for Lua, whilst remaining fundamentally Lua.
It's fast, safe, unsurprising and fully-featured, with let statements, string lambdas, and currying. Whether you miss LISP or Haskell whilst working with Lua, this should scratch your itch, without making Lua's VM come to a screeching halt.
apply (functor, args) |
Apply a function recursively to a list. |
car (tbl) |
Access the first value of an array. |
cdr (tbl) |
Access the "tail" of a list |
cond (condlist) |
A better chained else-if function. |
cons (val[, tbl]) |
A list-creation tool |
curry (a, b) |
A currying function |
elif (predicate, a, b) |
A functional else-if construct |
exclude (ex_tbl, tbl) |
Remove a set of values from a table |
filter (functor, args) |
Filter a list |
foldr (functor, tbl, val) |
foldr |
ktov (tbl) |
Swap key and values in a table. |
let (values, functor) |
A localised value binding function |
map (functor, args) |
Create a list by recursively calling a function against a list |
nth (iterable, begin, fin) |
Access a range from an interable |
recur () |
A tail-call elimination safe way of calling the calling function. |
reverse (obj) |
Reverses an iterable |
set (tbl) |
Reduce a table to a set, that is, every item must be unique. |
shuffle (tbl) |
Shuffles a given table, using Fisher-Yates, a simple swapping algo. |
add (a, b) |
Addition operator |
clamp (x, n, y) |
Clamp a number between two others |
div (a, b) |
Division operator |
div.int (a, b) |
Integer division operator |
mul (a, b) |
Multiplication operator |
random ([x[, y]]) |
Wraps math.random, unless x is a table, in which case it gives a
random item from that table. |
random.weighted (tbl) |
Given a weighted table, (e.g. |
round (num, depth) |
Round a number to a certain number of places |
sub (a, b) |
Subtraction operator |
pollute () |
Pollute the global namespace with f.lua's functions. |
unpollute () |
Undo pollution of the global namespace by f.pollute. |
-
clone (o)
-
Clones an object & its metatable
Parameters:
Returns:
A copy of the object
-
fn (s)
-
String Lambda!
e.g. f.fn("(x, y) print(x, y)")(2, 3)
Parameters:
- s
string
A string starting with parentheses as shown in the example.
Returns:
function
-
guard ()
-
A simple type-guard system
Takes a series of strings, that should be types, in the order the function being guarded would receive them.
Then it optionally takes another string prepended with "->" for the return type.
Finally it must receive a function to guard.
Returns:
function
-
iter (iter)
-
Converts an iterable into a coroutine
Parameters:
- iter
string or table iterable
Returns:
thread
-
memoize (functor)
-
Return a self-caching version of a function
Parameters:
- functor
function
The function whose return data should get cached
Returns:
function
Returns a function that caches data the first time it is called, and just returns that if the arguments are the same the next time around
-
timeit (functor, ...)
-
A benchmarking tool, it is highly not recommended to run impure functions through it.
Parameters:
- functor
function
The function to benchmark. It should be pure.
- ...
Arguments to parse to functor
Returns:
number
Seconds taken on average across 100 runs.
-
vend (vendor)
-
Add a vendor path to Lua
Parameters:
Returns:
nil
-
with (entry, permissions, functor)
-
Autoclosing files and threads
Parameters:
- entry
Filenme string or thread object
- permissions
string
- functor
function
-
co (functor)
-
Wrap a function in a coroutine
Parameters:
Returns:
thread
Returns coroutine.wrap(functor)
-
co.c (functor)
-
Create a coroutine
Parameters:
Returns:
thread
Returns coroutine.create(functor)
-
co.r ()
-
Check a coroutine is running
Returns:
boolean
Returns coroutine.running()
-
co.t (functor)
-
Toggle a coroutine's resume
Parameters:
Returns:
Either returns the coroutine.resume of functor, or nil if the thread is dead.
-
apply (functor, args)
-
Apply a function recursively to a list.
Parameters:
- functor
The function to recursively call against the list
- args
table
The list of arguments to be recursively called.
Returns:
Returns a single value created by the recursive call.
-
car (tbl)
-
Access the first value of an array.
Parameters:
- tbl
table
The list to be accessed
Returns:
The first element of the list.
-
cdr (tbl)
-
Access the "tail" of a list
Parameters:
- tbl
table
The list to be accessed
Returns:
table
Returns all but the first element in the list.
-
cond (condlist)
-
A better chained else-if function.
Parameters:
- condlist
A condlist is a table, containing other tables. The inner tables are pairs, where the key is a boolean. If it is true, then it's value is returned.
Returns:
Returns a value where the key is true.
-
cons (val[, tbl])
-
A list-creation tool
Parameters:
- val
Any value
- tbl
table
The list to join to. If one doesn't exist, it will be created.
(optional)
Returns:
table
The list that is generated.
-
curry (a, b)
-
A currying function
Parameters:
Returns:
function
A function that merges a around b, returning a new function. e.g. curry(print, string.format) is a kind of printf.
-
elif (predicate, a, b)
-
A functional else-if construct
Parameters:
- predicate
boolean
- a
Returned if predicate is true
- b
Returned if predicate is false
Returns:
Either a or b
-
exclude (ex_tbl, tbl)
-
Remove a set of values from a table
Parameters:
- ex_tbl
table
The values to remove e.g. {1, 2, 3}
- tbl
table
The table being processed e.g. {1, 2, 3, 4, 5}
Returns:
table
The new table, e.g. {4, 5}
-
filter (functor, args)
-
Filter a list
Parameters:
- functor
function
A function that should return a boolean when given a value from the args list. If true, the value is added to the return list, if not, it gets dropped.
- args
table
The list to filter
Returns:
table
Returns the filtered list
-
foldr (functor, tbl, val)
-
foldr
Parameters:
- functor
function
- tbl
table
- val
The seed value
Returns:
The folded value
-
ktov (tbl)
-
Swap key and values in a table.
Parameters:
Returns:
table
A key-value swapped table
-
let (values, functor)
-
A localised value binding function
Parameters:
- values
table
An array of pairs of values, with the name on the left, and value on the right.
- functor
function
The function to call, with the new value bindings.
Returns:
Returns the return of the functor.
-
map (functor, args)
-
Create a list by recursively calling a function against a list
Parameters:
- functor
function
The function to be called against
- args
table
The arguments to call against the function
Returns:
table
Returns a list
-
nth (iterable, begin, fin)
-
Access a range from an interable
Parameters:
- iterable
An iterable, such as a string or table.
- begin
number
- fin
number
Returns:
A selection of the iterable
-
recur ()
-
A tail-call elimination safe way of calling the calling function.
e.g. "function() recur()() end" is a infinitely recursive function.
Returns:
function
Returns the containing function.
-
reverse (obj)
-
Reverses an iterable
Parameters:
Returns:
The reverse string or table
-
set (tbl)
-
Reduce a table to a set, that is, every item must be unique.
Parameters:
- tbl
table
A simple array e.g. {"Hello", "World", "Hello", "World"}
Returns:
table
A simple array, with only unique items. e.g. {"Hello", "World"}
-
shuffle (tbl)
-
Shuffles a given table, using Fisher-Yates, a simple swapping algo.
Parameters:
Returns:
table
A shuffled table
-
add (a, b)
-
Addition operator
Parameters:
Returns:
number
Return a + b
-
clamp (x, n, y)
-
Clamp a number between two others
Parameters:
- x
number
- n
number
- y
number
Returns:
number
Returns the number in the "middle" after sorting. e.g. clamp(5, 0, 10) == 5
-
div (a, b)
-
Division operator
Parameters:
Returns:
number
Returns a / b
-
div.int (a, b)
-
Integer division operator
Parameters:
Returns:
number
Returns math.floor(a/b)
-
mul (a, b)
-
Multiplication operator
Parameters:
Returns:
number
Return a * b
-
random ([x[, y]])
-
Wraps math.random, unless x is a table, in which case it gives a
random item from that table.
Parameters:
- x
(optional)
- y
(optional)
Returns:
object
-
random.weighted (tbl)
-
Given a weighted table, (e.g. {"hello" = 2, "cat" = 1, "dog" = 3}), returns a random item (e.g. "dog"), whilst respecting the weighted chance.
Parameters:
Returns:
Returns one key from the table.
-
round (num, depth)
-
Round a number to a certain number of places
Parameters:
- num
number
The number to round
- depth
number
The number of decimal places to round to
Returns:
number
The rounded number is returned
-
sub (a, b)
-
Subtraction operator
Parameters:
Returns:
number
Return a - b
-
gt (a, b)
-
Greater Than operator
Parameters:
Returns:
boolean
-
gte (a, b)
-
Great Than or Equal operator
Parameters:
Returns:
boolean
-
lt (a, b)
-
Less Than operator
Parameters:
Returns:
boolean
-
lte (a, b)
-
Less Than or Equal operator
Parameters:
Returns:
boolean
-
mod (a, b)
-
Mod Operator
Parameters:
Returns:
number
Returns a % b
-
ne (a, b)
-
Not Equal operator
Parameters:
Returns:
boolean
-
pow (a, b)
-
Powerto operator
Parameters:
Returns:
Returns a^b
-
unary (a)
-
Unary operator
Parameters:
Returns:
Returns -a
-
xnd (a, b)
-
And operator
Parameters:
Returns:
Returns a and b
-
xnt (a)
-
Not operator
Parameters:
Returns:
Returns not a
-
xor (a, b)
-
Or operator
Parameters:
Returns:
Returns a or b
-
port.from_string (str, functor)
-
Override io.read with a string
Parameters:
Returns:
Returns the output of functor
-
port.iter (port, n, data)
-
An iterator that can be used in for-loops to read over a port-like object
Parameters:
- port
A port-like object
- n
Position of iteration
- data
Where the data is stored in the port-like object
-
port.make_input (read_func, close_func)
-
Create a port that can be read from
Parameters:
- read_func
function
- close_func
function
Returns:
Returns port-like object for reading
-
port.make_output (write_func, read_func, close_func)
-
Create a port that can read and write
Parameters:
- write_func
function
- read_func
function
- close_func
function
Returns:
Retuns port-like object for writing and reading
-
port.with_input (port, functor)
-
Override io.read with port:read for a given port
Parameters:
- port
A port-like object
- functor
function
Returns:
Returns functor's return value.
-
port.with_output (port, functor)
-
Override print and io.write with port:write for a given port
Parameters:
- port
A port-like object
- functor
function
Returns:
Returns functor's return value.
-
eq (a, b)
-
Test equivalence
Parameters:
Returns:
boolean
Returns true if the two given values are equivalent, even if they are different tables.
-
inarray (tbl, v)
-
Check if a value occurs in a list
Parameters:
- tbl
table
Table to process
- v
Value to check for
Returns:
boolean
-
isboolean (x)
-
Predicate to test boolean type
Parameters:
- x
The object to test if is a boolean
Returns:
boolean
-
isfile (x)
-
Predicate to test file type
Parameters:
- x
The object to test if is a file
Returns:
boolean
-
isfunction (x)
-
Predicate to test function type
Parameters:
- x
The object to test if is a function
Returns:
boolean
-
isnegative (x)
-
Returns true or false, given any object, if it is a negative number
Parameters:
Returns:
boolean
-
isnil (x)
-
Predicate to test nil type
Parameters:
- x
The object to test if is a nil
Returns:
boolean
-
isnumber (x)
-
Predicate to test number type
Parameters:
- x
The object to test if is a number
Returns:
boolean
-
ispositive (x)
-
Returns true or false, given any object, if it is a positive number
Parameters:
Returns:
boolean
-
isstring (x)
-
Predicate to test string type
Parameters:
- x
The object to test if is a string
Returns:
boolean
-
istable (x)
-
Predicate to test table type
Parameters:
- x
The object to test if is a table
Returns:
boolean
-
isthread (x)
-
Predicate to test thread type
Parameters:
- x
The object to test if is a thread
Returns:
boolean
-
isuserdata (x)
-
Predicate to test userdata type
Parameters:
- x
The object to test if is a userdata
Returns:
boolean
-
iszero (x)
-
Returns true or false, given any object, if it is 0
Parameters:
Returns:
boolean
-
pollute ()
-
Pollute the global namespace with f.lua's functions.
Returns:
nil
No return value.
-
unpollute ()
-
Undo pollution of the global namespace by f.pollute.
Returns:
nil
No return value.