Last active
March 1, 2024 06:16
-
-
Save nolros/722377abc6449effaba6c427dbb96743 to your computer and use it in GitHub Desktop.
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
function trampoline(fn) { | |
return function trampolined(...args) { | |
let result = fn(...args); | |
while (typeof result === 'function') { | |
result = result(); | |
} | |
return result; | |
}; | |
} | |
const setObjectPropsTrampoline = (accumulator, obj) => { | |
const go = (acc, keys) => { | |
if (keys.length === 0) { | |
return acc; | |
} | |
const [firstKey, ...restKeys] = keys; | |
const newAcc = { ...acc, [firstKey]: obj[firstKey] }; | |
return () => go(newAcc, restKeys); | |
}; | |
return trampoline(go)(accumulator, Object.keys(obj)); | |
}; | |
const result = setObjectPropsTrampoline({ path: 'home' }, { name: 'Nolan', last: 'Rosie' }); | |
console.log('result:', result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment