Last active
November 11, 2017 16:36
-
-
Save koozdra/a5912b7fc9efb3b9f4e189f3e2504d3a to your computer and use it in GitHub Desktop.
implementing some lodash functions
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
// modification version | |
function once(f) { | |
let hasRun = false; | |
let result; | |
return () => { | |
if (!hasRun) { | |
result = f(); | |
hasRun = true; | |
} | |
return result; | |
}; | |
} | |
function init() { | |
console.log('initializing'); | |
} | |
const t = once(init); | |
t(); | |
t(); | |
function rearg(f, argumentIndeces) { | |
return (...args) => { | |
const reordered = _.map(argumentIndeces, index => _.nth(args, index)); | |
return f(...reordered); | |
}; | |
} | |
function orig(a, b, c) { | |
return [a, b, c]; | |
} | |
const rearged = rearg(orig, [2, 0, 1]); | |
console.log(rearged('b', 'c', 'a')); | |
function after(n, f) { | |
let runs = n; | |
return () => { | |
if (runs <= 1) { | |
return f(); | |
} else { | |
runs -= 1; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment