fn.chars(string)
creates array with 8-bit chars extracted fromstring
fn.chars(string, pattern)
creates array with substrings fromstring
extracted usingpattern
fn.copy(value)
makes a shallow copy of thetable
or just returns any other valuefn.copyarray(array)
makes a shallow copy of thearray
fn.deepcopy(value)
makes a deep copy of thetable
or just returns any other valuefn.range(limit)
creates range from 1 tolimit > 0
or from -1 tolimit < 0
, forlimit == 0
returns empty arrayfn.range(init, limit)
creates range frominit
tolimit
, it's ok iflimit < init
fn.range(init, limit, step)
creates range frominit
tolimit
bystep
fn.utf8chars(string)
creates array with UTF-8 chars extracted fromstring
fn.chain(table)
wraps table with metatable for chaining calls; call:value()
to unwrap the result, reducerscount
,equal
,every
,max
,min
,fold
,foldl
,foldr
,product
,some
,str
,sum
,unpack
calls are always unwrapped
chunk(array, ...size)
returns array partitioned on chunks of passed sizecombinations(array, combination_size)
returns combinations with sizecombination_size
, i.e.combinations({a,b,c},2) => {{a,b},{a,c},{b,c}}
each(array, f)
callsf
on eacharray
element; returns passedarray
(helpful for chaining)exclude(array, ...values)
removesvalues
from thearray
filter(array, p)
filters elements from array which hold predicatep(value,index,array) => boolean
flat(array)
flattens the arrayflat(array, level)
flattens the array untillevel
flatmap(array, f)
transforms passed array by mapping with signaturef(value,index,array) => newvalue
and flattens on 1 level (same asflat(arr,1)
)frequencies(array)
return table filled with count of occurencies of specific item, i.e.frequencies({a,a,b,c}) => {a = 2, b = 1, c = 1}
fromentries(array)
transforms array with pairs{key, value}
to tableindexed(array)
returns array filled with array pairs{index,value}
inplace_filter(array, p)
filters elements from array inplace which hold predicatep(value,index,array) => boolean
inplace_map(array, f)
transforms passed array inplace by mapping with signaturef(value,index,array) => newvalue
inplace_reverse(array)
reverses array inplaceinplace_shuffle(array)
shuffles array inplace usingmath.random
generatorinplace_shuffle(array, random)
shuffles array inplace using custom RNGinplace_sort(array)
sorts inplace usingtable.sort
inplace_sort(array, cmp)
sorts inplace usingtable.sort
using custom orderinginsert(array, index, ...values)
insertsvalues
from before the specifiedindex
. Ifindex < 0
then place is counted from the end ofarray
, i.e.-1
is after the last item,-2
is before the last itemmap(array, f)
transforms passed array by mapping with signaturef(value,index,array) => newvalue
max(array)
returns maxmial element of thearray
min(array)
returns minimal element of thearray
partition(array, p)
splits array into 2 parts by predicatep(value,index,array) => boolean
and returns array with 2 inner arraypermutations(array)
returns all possible permutations, i.e.permitations{a,b,c} => {{a,b,c},{b,a,c},{c,a,b},{a,c,b},{b,c,a},{c,b,a}}
rep(array, n)
returns array containingarray
elementsn
timesrep(array, n, separator)
returns array containingarray
elementsn
times, separated bysep
elementreverse(array)
returns reversed arrayshuffle(array)
shuffles array usingmath.random
generatorshuffle(array, random)
shuffles array using custom RNGsort(array)
sorts thearray
copy by<
ordering and returns the result usingtable.sort
sort(array, cmp)
sorts thearray
copy using custom ordering usingtable.sort
sort(array, cmp, sort)
sorts thearray
copy using custom ordering and custom sort functionstablesort(array)
sort thearray
inplace by<
ordering using stable sortingstablesort(array, cmp)
sorts thearray
inplace using custom ordering using stable sortingsub(array, init)
create a slice of array starting frominit
to the end ofarray
, negative indices are allowedsub(array, init, limit)
create a slice of array frominit
tolimit
sub(array, init, limit, step)
create a slice of array frominit
tolimit
withstep
unique(array)
returns array without duplicate valuesunzip(array)
maps sequence of tuples into tuple of sequenceszip(...arrays)
maps tuple of sequences into a sequence of tuples, i.e.zip({a,b},{1,2},{x,y}) => {{a,1,x},{b,2,y}}
diff(table, table)
returns table with diff's (nested), if tables are equal returns an empty table; deleted fields marked asfn.NIL
entries(table)
returns array filled with table pairs{key,value}
inplace_update(table, upd)
updatestable
inplace from theupd
table, adding new values, to delete table entry one need to passfn.NIL
valuekeys(table)
returns array withtable
keyskvswap(table)
returns table with keys and values swappedvalues(table)
returns array withtable
valuessortedentries(table)
returns array filled with table pairs{key,value}
sorted by<
usingtable.sort
sortedentries(table, cmp)
returns array filled with table pairs{key,value}
sorted bycmp
usingtable.sort
sortedentries(table, cmp, sort)
returns array filled with table pairs{key,value}
sorted bycmp
usingsort
functionupdate(table, upd)
updatestable
content from theupd
table, adding new values, to delete table entry one need to passfn.NIL
valuepatch(table, table)
returns table with applied patch, merging two tables; to delete field one should passfn.NIL
; plays nice withdiff
difference(table, ...tables)
returnstable
without keys intables
intersection(table, ...tables)
returnstable
with keys which exist in alltables
issubset(table1, table2)
checks thattable1
is subset oftable2
union(table, ...tables)
returnstable
merged withtables
(values oftable
will not be overwritten)
concat
==table.concat
count(table)
returns total count of entries intable
count(table, p)
returns count of entries for whichp(value, index, array) => bool
holdscount(array, p)
counts number ofarray
items for which predicatep(value, index, array) => bool
holdsequal(v1, v2)
checksv1
andv2
on deep equality, nested tables are supported but without table keys (except some simple cases), also you can usefn._
as the wildcardevery(array, p)
returnstrue
if allarray
elements holdp(value, index, array) => bool
find(array, p)
linear search of the value which holdsp(value, index, array) => bool
fold(table, f, acc)
generic reduce, reduces by the functionf(acc, value, key, table) => acc, stop
where ifstop
is notfalsy the reducing process haltsfoldl(array, f, acc)
common reduce from the begining ofarray
, reduces by the functionf(acc, value, index, array) => acc, stop
where ifstop
is notfalsy the reducing process haltsfoldr(array, f, acc)
reduce from the end ofarray
getmetatable(tbl)
==getmetatable
indexof(array, value)
linear search of thevalue
in thearray
indexof(array, value, cmp)
binary search of thevalue
in the sortedarray
withcmp
orderinginplace_setmetatable(tbl, mt)
sets metatable fortbl
pack
==table.pack or {...}
product(array)
returns product ofarray
elementssetmetatable(tbl, mt)
sets metatable for copy oftbl
some(array, p)
returnstrue
if any of thearray
element holdsp(value, index, array) => bool
str(v)
returns jsony like representation of passed valuesum(array)
returns sum ofarray
elementsunpack
==table.unpack
fn.lambda(source)
, create simple string lambda fromsource
which has numbered arguments with @ prefix, i.e.@1
,@2
and so on which transforms into single expression.
local lt = fn.lambda[[@1 < @2]]
-- compiles to
local lt = function(__1__,__2__)
return __1__ < __2__
end
You can also use first argument without index:
local add2 = function(__1__)
return __1__+2
end
Varargs is used for partial application:
local lt2 = fn.lambda([[@2 < @1]], 2)
-- compiles to (something like, its closure actually)
local __1__ = 2
lt2 = function(__2__)
return __2__ < __1__
end
fn(table)
=>fn.chain(table)
fn(string)
=>fn.lambda(string)
fn(number)
=>fn.chain(fn.range(number))
fn(number, number)
=>fn.chain(fn.range(number, number))
fn(number, number, number)
=>fn.chain(fn.range(number, number, number))