Last active
July 7, 2023 03:00
-
-
Save matheusmv/dfc282a756943a4cc7ebe7a54ba19616 to your computer and use it in GitHub Desktop.
simple memoization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const put = (cache, params, result) => { | |
let node = cache; | |
for (const param of params) { | |
const key = param.toString(); | |
if (!node[key]) { | |
node[key] = {}; | |
} | |
node = node[key] || {}; | |
} | |
node['result'] = result; | |
}; | |
const get = (cache, params) => { | |
let node = cache; | |
for (const param of params) { | |
const key = param.toString(); | |
node = node[key]; | |
if (!node) { | |
return null; | |
} | |
} | |
return node['result']; | |
}; | |
const memoize = (func) => { | |
const cache = {}; | |
return (...args) => { | |
const params = args; | |
const result = get(cache, params); | |
if (result === null) { | |
const computedResult = func(...args); | |
put(cache, params, computedResult); | |
return computedResult; | |
} | |
return result; | |
}; | |
}; | |
const addFunc = memoize((a, b) => { | |
console.log("computed and cached"); | |
return a + b; | |
}); | |
console.log(addFunc(1, 1)); | |
console.log(addFunc(1, 1)); | |
console.log(addFunc(1, 1)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local unpack = table.unpack | |
local M = {} | |
M.memo = setmetatable({ | |
put = function(cache, params, result) | |
local node = cache | |
for i = 1, #params do | |
local param = params[i] | |
node.children = node.children or {} | |
node.children[param] = node.children[param] or {} | |
node = node.children[param] | |
end | |
node.result = result | |
end, | |
get = function(cache, params) | |
local node = cache | |
for i = 1, #params do | |
local param = params[i] | |
node = node.children and node.children[param] | |
if not node then | |
return nil | |
end | |
end | |
return node.result | |
end, | |
}, { | |
__call = function(self, func) | |
local cache = {} | |
return function(...) | |
local params = { ... } | |
local result = self.get(cache, params) | |
if not result then | |
result = { func(...) } | |
self.put(cache, params, result) | |
end | |
return unpack(result) | |
end | |
end, | |
}) | |
M.add = M.memo(function(a, b) | |
if not a or type(a) ~= "number" then | |
a = 0 | |
end | |
if not b or type(b) ~= "number" then | |
b = 0 | |
end | |
print(string.format("computing and caching: %d + %d", a, b)) | |
return a + b | |
end) | |
print(M.add(1, 1)) | |
print(M.add(1, 1)) | |
print(M.add(1, 1)) | |
return M |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type MemoCache = { | |
[key: string]: any; | |
}; | |
function put(cache: MemoCache, params: any[], result: any): void { | |
let node: MemoCache = cache; | |
for (const param of params) { | |
const key = param.toString(); | |
if (!node[key]) { | |
node[key] = {}; | |
} | |
node = node[key]; | |
} | |
return node['result'] = result; | |
} | |
function get(cache: MemoCache, params: any[]): any { | |
let node: MemoCache = cache; | |
for (const param of params) { | |
const key = param.toString(); | |
node = node[key]; | |
if (!node) { | |
return null; | |
} | |
} | |
return node['result']; | |
} | |
function memoize(func: (...args: any) => any): ((...args: any) => any) { | |
const cache: MemoCache = {}; | |
return function(...args: any): any { | |
const params = args; | |
const result = get(cache, params); | |
if (result === null) { | |
const computedResult = func(...args); | |
put(cache, params, computedResult); | |
return computedResult; | |
} | |
return result; | |
} | |
} | |
const addFunc = memoize((a: number, b: number): number => { | |
console.log("computed and cached"); | |
return a + b; | |
}) as (a: number, b: number) => number; | |
console.log(addFunc(1, 1)); | |
console.log(addFunc(1, 1)); | |
console.log(addFunc(1, 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment