Zelda Wiki

OoT Navi.png

Hey! Listen!

This wiki contains spoilers! Read at your own risk!

READ MORE

Zelda Wiki
Zelda Wiki
10,308
pages
Line 1: Line 1:
 
local p = {}
 
local p = {}
   
function p.peek(tbl)
+
function p.identity(...)
 
return ...
mw.logObject(tbl)
 
return tbl
 
end
 
 
function p.noop(...)
 
return ...
 
 
end
 
end
   
Line 14: Line 9:
 
return not fn(...)
 
return not fn(...)
 
end
 
end
 
end
  +
 
function p.noop()
  +
return nil
  +
end
  +
  +
function p.peek(tbl)
 
mw.logObject(tbl)
 
return tbl
 
end
 
end
   

Revision as of 22:38, 16 March 2020

The functions in this module return other functions, namely iterators. It also contains utility functions for functional programming. Lua error in Module:Documentation/Module at line 419: attempt to call field 'memoize' (a nil value).


local p = {}

function p.identity(...)
	return ...
end

function p.negate(fn)
	return function(...)
		return not fn(...)
	end
end

function p.noop() 
	return nil
end

function p.peek(tbl)
	mw.logObject(tbl)
	return tbl
end

-- @args {fn1, fn2, ..., fnN}
-- @returns A single function whose arguments are passed to fn1. Output of fn1 is passed to fn2, and so on.
-- Similar to https://lodash.com/docs/#flow
function p.pipe(...)
	local functions = {...}
	return function(...)
		local state = {...}
		for _, fn in ipairs(functions) do
			state = {fn(unpack(state))}
		end
		return unpack(state)
	end
end

-- http://lua-users.org/wiki/RangeIterator
function p.range(a, b, step)
  if not b then
    b = a
    a = 1
  end
  step = step or 1
  local f =
    step > 0 and
      function(_, lastvalue)
        local nextvalue = lastvalue + step
        if nextvalue <= b then return nextvalue end
      end or
    step < 0 and
      function(_, lastvalue)
        local nextvalue = lastvalue + step
        if nextvalue >= b then return nextvalue end
      end or
      function(_, lastvalue) return lastvalue end
  return f, nil, a - step
end

return p