Module Meta

Produce function wrappers from other functions.

Module Status: Experimental 2020-10-31.

Usage:

    local Meta = require('__eradicators-library__/erlib/lua/Meta/!init')()
    

lua.Meta.Closurize Functions

Closurize(f, ...) Creates a function wrapper with partially fullfilled arguments.

lua.Meta.Compose Functions

Compose(...) Generates a closurized function chain.
Compose-fixme7 : undetermined behavior if any of the functions returns a sparse array?

lua.Meta.Memoize Functions

Memoize(f) A single-argument-function memory-table.
Memoize(arg_count, copy_result, f) A multi-argument-function memoizer.

lua.Meta.SwitchCase Functions

SwitchCase(analyzer, cases) Input-dependant handler function switching.


lua.Meta.Closurize Functions

Closurize(f, ...)
Creates a function wrapper with partially fullfilled arguments.

Parameters:

  • f function the function to be wrapped
  • ... AnyValue the first k arguments of f(...) that the created closure will store.

Returns:

    function the wrapper that takes the remaining arguments before calling f.

Usage:

    -- create a closure that will always use the same first three arguments
    local f1 = function(a,b,c,d,e,f,g) print(a,b,c,d,e,f,g) end
    local f2 = Closurize(f1,'clo','sur','ized')
    
    -- call it with the remaining 4
    print(f2('var',nil,'iable',nil))
    > clo sur ized var nil iable nil
    
    -- or call it with fewer, leaving the rest empty
    print(f2(nil,'variable'))
    > clo sur ized nil variable nil nil
    

lua.Meta.Compose Functions

Compose(...)
Generates a closurized function chain.

Takes any number of functions and binds them into a single function that applies all functions in reverse order given.

Functions are applied "from right to left". Compose(a,b,c)(x) == a(b(c(x)))

Parameters:

  • ... function the functions you want to apply in reverse order.

Returns:

    AnyValue the result of the final function call

Usage:

  • local a  = function(x) return 2+x end
    local b  = function(x) return 2*x end
    local c  = function(x) return 2/x end
    local fc = Compose(a,b,c)
    print(fc(10)) -- a(b(c(x)))
    > 2.4
    
  • -- each function recieves all return values of the previous function
    local d  = function(z,y,x) return 2+x,z,y,x end
    local e  = function(  y,x) return 2*x,  y,x end
    local f  = function(    x) return 2/x,    x end
    local fc = Compose(d,e,f)
    local r,z,y,x = fc(10) -- d(e(f(x)))
    print(x,y,z,r)
    > 10  0.2  20  12
    
Compose-fixme7
: undetermined behavior if any of the functions returns a sparse array?

lua.Meta.Memoize Functions

Memoize(f)
A single-argument-function memory-table. Very fast.

NOTE: if the result of calling a memoized function is a table then a reference to the cached table is returned. You must therefore copy any returned table before you alter them, or you will alter the cached table, and therefore all future results.

Parameters:

Returns:

  1. table a function-like indexable MemoTable.
  2. function A parameterless function that can be called to clear the cached results. In case you want to free up memory.

Usage:

    -- Simply call Memoize() with the function you want to memoize.
    local doubleup = function(a) return 2*a end
    local memodoubleup = Memoize(doubleup)
    -- You can use the memoized function exactly as before.
    -- But for very simple and fast operations like this example
    -- The function call overhead would outweight the performance gains.
    local four = memodoubleup(2)
    -- Instead you can also access the result like a table. This is actually
    -- a real native table lookup for every call after the first. So it
    -- is really fast! Even for this simple 2*a example it only takes 1/3rd
    -- of the time it would to call the un-memoized function.
    local four = memodoubleup[2]
    
Memoize(arg_count, copy_result, f)
A multi-argument-function memoizer.

Parameters:

  • arg_count NaturalNumber The number of arguments that f takes.
  • copy_result boolean If true then calling the memoized function will use Table.dcopy to copy the result before returning it. This is only useful if the returned value is a table. If false a direct reference will be passed.
  • f function The function to memoize. The function must return exactly one NotNil value when called.

Returns:

  1. function The memoized version of f. It must be called with exactly arg_count arguments of type NotNil. {args} -> result loopup uses lua object identity, so be careful when passing tables.
  2. function A parameterless function that can be called to clear the cached results. In case you want to free up memory.

lua.Meta.SwitchCase Functions

SwitchCase(analyzer, cases)
Input-dependant handler function switching. A switch case calls one of several functions depending on the output of a given analyzer function. In case the output of the analyzer does not match any case the default case will be returned instead.

Parameters:

  • analyzer function The result of analyzer(...) will be used as a key to cases to call cases[analyzer(...)](...).
  • cases table A map {key -> function}. Where every possible output of analyzer(...) is represented by a key.

Usage:

    local analyzer = function(x) return type(x) end
    local cases    = {
      table   = function(x) return tostring(x[1]) end,
      number  = function(x) return tostring(x-1 ) end,
      default = function(x) return          x..2  end,
      }
    
    local SC = SwitchCase(analyzer,cases)
    
    print(SC({42})) --table
    > 42
    
    print(SC(43))   --number
    > 42
    
    print(SC('4'))  --default
    > 42
    
generated by LDoc 1.4.6 Last updated 2021-09-10 19:51:19