Module oo
general purpose functions for object-oriented programming and more Version: 20230621
Functions
div (a, b) | div function for lua version prior to 5.3. |
makeclass (c, inh) | Core of the class system from oo.lua. |
concatWithToString (t1, t2) | Standard __conact method for classes: obj .. |
cpmetatable (f, t) | copying the metatable in one or two steps returns target thus "return cpmt(f,t)" is possible |
cpmt () | shortcut for cpmetatable |
savemt (f) | returns a function that sets the metatable of its argument to the current metatable of f |
dcopy (t, l) | copy tables until depth l (only copies values, not keys!). |
rawipairs (tab) | mmipairs shall respect metamethods for all LUA versions, while rawipairs shall ignore them |
mmipairs (tab) | mmipairs shall respect metamethods for all LUA versions, while rawipairs shall ignore them |
dump (t, l, pre, post, sep) | printable table representation until depth l |
dumpNoKeys (t, l, pre, post, sep) | printable table representation until depth l (only values) |
idump (t, l, pre, post, sep) | printable table repr. |
statefull (sl, t) | create statefull iterator from stateless one |
seq (f, t, by) | returns table filled with entries v from for v=f,t,by do ... |
rangemany (it, rmin, rmax) | checks if an iterator delivers a number n of values where n is in [rmin,rmax] |
tablerange (t, f, l) | returns a subtable of a given table with indices from f to l |
gluetables (t1, t2) | glues together two array-tables t1 and t2 to a single table t such that #t=#t1+#t2. |
settablerange (t, f, r) | sets entries t[f+i-1] to r[i] for 1<=i<=#r |
rev (t) | rerverses the table |
keys (t) | table of keys, s.t. |
ikeys (t) | table of keys, s.t. |
kvswap (t) | table of keys, s.t. |
ikeyssort (t, f) | like ikeys, but the returned table is sorted accoding to f(k1,k2,t) not that t is an argument for f, thus the comparision can access the corresponding values, see numval for an example. |
numval (how) | sort keys according to their corresponding value |
map (f, t) | classical functional map, but f might have two arguments v,k such that the key can also be considered. |
mapi (f, it) | map function for iterator constructors
EXAMPLE: vipairs = oo.mapi(function(k,v) return v,k end,ipairs)
combined with for k,v in vipairs({2,1}) do print(k) end prints
2 and then 1. |
mapmod (f, t) | like map, but modifies t in place |
reduce (f, t, init, iter) | classic Reduce function. |
prod (t, iter) | product over all t[k] in order of iter. |
sum (t, iter) | sum over all t[k] in order of iter. |
where (t, what) | returns |
transp (t, inplace) | transpose 2-dim table |
subsetsWith (t, withv) | interator over all subsets of t conaining values in withv. |
partitions (t, startidx) | interator over all partitions of t. |
table.maxn (t) | |
linkmodules (...) | linkmodules(m1,m2,...) gives each module access to functionality of the other modules without creating require-loops. |
expectlink (to, from, n) | returns n placeholder functions to be linked later. |
globalize (module) | inserts every element MODINFO of M into the global namespace. |
Fields
OO_USE_GLOBALS | If set to true before require "oo" , all oo functions etc will
be part of the global namespace. |
Functions
- div (a, b)
-
div function for lua version prior to 5.3.
Parameters:
- a
- b
Returns:
-
number a div b
- makeclass (c, inh)
-
Core of the class system from oo.lua.
A class has is a table equipped for usage as a metatable.
__index is set to the class itself or to a function
to look for entries in ancestor classes.
The class is callable, where MyClass(...) invokes
MyClass.new(...).
EXAMPLE:
MyClass = {new = ..., __tostring = ...}
afterwards:makeclass(MyClass)
Parameters:
- c
- inh
- concatWithToString (t1, t2)
-
Standard __conact method for classes: obj .. "" = tostring(obj)
Parameters:
- t1 any
- t2 any
Returns:
-
string
- cpmetatable (f, t)
-
copying the metatable in one or two steps
returns target thus "return cpmt(f,t)" is possible
Parameters:
- f any from
- t any to
Returns:
-
any t with metatable of f
- cpmt ()
- shortcut for cpmetatable
- savemt (f)
-
returns a function that sets the metatable of its argument to
the current metatable of f
Parameters:
- f
- dcopy (t, l)
-
copy tables until depth l (only copies values, not keys!).
Parameters:
- t table
- l integer depth
Returns:
-
table 'copy of t'
- rawipairs (tab)
-
mmipairs shall respect metamethods for all LUA versions,
while rawipairs shall ignore them (tab : table)
Parameters:
- tab table argument for ipairs
- mmipairs (tab)
-
mmipairs shall respect metamethods for all LUA versions,
while rawipairs shall ignore them (tab : table)
Parameters:
- tab table argument for ipairs
- dump (t, l, pre, post, sep)
-
printable table representation until depth l
Parameters:
- t
- l
- pre
- post
- sep
- dumpNoKeys (t, l, pre, post, sep)
-
printable table representation until depth l (only values)
Parameters:
- t
- l
- pre
- post
- sep
- idump (t, l, pre, post, sep)
-
printable table repr. until depth l (only values of integer keys)
Parameters:
- t
- l
- pre
- post
- sep
- statefull (sl, t)
-
create statefull iterator from stateless one
Parameters:
- sl
- t
- seq (f, t, by)
-
returns table filled with entries v from for v=f,t,by do ...
Parameters:
- f
- t
- by
- rangemany (it, rmin, rmax)
-
checks if an iterator delivers a number n of values
where n is in [rmin,rmax]
Parameters:
- it
- rmin
- rmax
- tablerange (t, f, l)
-
returns a subtable of a given table with indices from f to l
Parameters:
- t
- f
- l
- gluetables (t1, t2)
-
glues together two array-tables t1 and t2 to a single table t
such that #t=#t1+#t2.
Parameters:
- t1
- t2
- settablerange (t, f, r)
-
sets entries t[f+i-1] to r[i] for 1<=i<=#r
Parameters:
- t
- f
- r
- rev (t)
-
rerverses the table
Parameters:
- t
- keys (t)
-
table of keys, s.t. keys(t)[k]=k
Parameters:
- t
- ikeys (t)
-
table of keys, s.t. ikeys(t)[i]=k, i in 1,num. of keys
Parameters:
- t
- kvswap (t)
-
table of keys, s.t. keys(t)[k]=k
Parameters:
- t
- ikeyssort (t, f)
-
like ikeys, but the returned table is sorted accoding to f(k1,k2,t)
not that t is an argument for f, thus the comparision can access
the corresponding values, see numval for an example.
EXAMPLE:
idump(ikeyssort({baa=1,aab=2}))
returns{aab, baa}
Parameters:
- t
- f
- numval (how)
-
sort keys according to their corresponding value
Parameters:
- how
- map (f, t)
-
classical functional map, but f might have two arguments v,k
such that the key can also be considered.
Parameters:
- f
- t
- mapi (f, it)
-
map function for iterator constructors
EXAMPLE:
vipairs = oo.mapi(function(k,v) return v,k end,ipairs)
combined withfor k,v in vipairs({2,1}) do print(k) end
prints 2 and then 1.Parameters:
- f
- it
- mapmod (f, t)
-
like map, but modifies t in place
Parameters:
- f
- t
- reduce (f, t, init, iter)
-
classic Reduce function. The iter argument ensures a particular
order, the default value is ipairs.
Parameters:
- f
- t
- init
- iter
- prod (t, iter)
-
product over all t[k] in order of iter.
Parameters:
- t
- iter
- sum (t, iter)
-
sum over all t[k] in order of iter.
Parameters:
- t
- iter
- where (t, what)
-
returns
Parameters:
- t
- what
- transp (t, inplace)
-
transpose 2-dim table
Parameters:
- t
- inplace
- subsetsWith (t, withv)
-
interator over all subsets of t conaining values in withv.
This iterator also works if some values in withv are not in t,
it simply prefixes all subsets of (t \ withv) with withv.
Parameters:
- t table (assumed to be an array)
- withv values to be contained in all subsets
Returns:
-
function iterator
- partitions (t, startidx)
-
interator over all partitions of t.
Parameters:
- t table (assumed to be an array)
- startidx use indices startidx,startidx+1,... instead of 1,2,3 This parameter is mostly used for recursion.
Returns:
-
function iterator
- table.maxn (t)
-
Parameters:
- t
- linkmodules (...)
-
linkmodules(m1,m2,...) gives each module access to functionality
of the other modules without creating require-loops.
If module m1 wants to
access m2.someFunction and m2.MODINFO.name = "secondMod", then
m1.MODINFO.links["secondMod"] shall be a function(m) that
assigns m.someFunction to a local variable in m1.
Parameters:
- ...
- expectlink (to, from, n)
-
returns n placeholder functions to be linked later.
use it as
func1,func2,func3 = oo.expectlink("secondMod","firstMod",3)
If any of this functions are called before linking with oo.linkmodules, then an error occurs:"use linkmodules() with firstMod and secondMod module."
Parameters:
- to
- from
- n
- globalize (module)
-
inserts every element MODINFO of M into the global namespace.
Parameters:
- module