Skip to content

Instantly share code, notes, and snippets.

@LPGhatguy
Created April 10, 2018 23:20
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save LPGhatguy/28dfad42d1897e0b3d9e7283f013193d to your computer and use it in GitHub Desktop.
Copying the Reselect API in ~40 lines of code. Does not allocate on call. Untested.
local function applySelector(selector, ...)
if typeof(selector) == "string" then
return (...)[selector]
else
return selector(...)
end
end
local function listShallowEqual(a, b, len)
for i = 1, len do
if a[i] ~= b[i] then
return false
end
end
return true
end
local function createSelector(inputSelectors, fn)
local lastValues = {}
local values = {}
local result = nil
local numInputSelectors = #inputSelectors
return function(...)
for i = 1, numInputSelectors do
values[i] = applySelector(inputSelectors[i], ...)
end
if listShallowEqual(values, lastValues, numInputSelectors) then
return result
end
result = fn(unpack(values))
values, lastValues = lastValues, values
return result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment