Anonymous
×
Create a new article
Write your page title here:
We currently have 97 articles on The Dream Shrine. Type your article name above or click on one of the titles below and start writing!



The Dream Shrine

Documentation for this module may be created at Module:UtilsFunction/doc

local p = {}

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

function p.memoize(fn)
	local cache = {}
	return function(...)
		local key = mw.dumpObject({...})
		if not cache[key] then
			cache[key] = fn(...)
		end
		return cache[key]
	end
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

function p.pipe(...)
	local state = {...}
	return function(functions)
		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