Skip to content

Instantly share code, notes, and snippets.

@koozdra
Last active November 11, 2017 16:36
Show Gist options
  • Save koozdra/a5912b7fc9efb3b9f4e189f3e2504d3a to your computer and use it in GitHub Desktop.
Save koozdra/a5912b7fc9efb3b9f4e189f3e2504d3a to your computer and use it in GitHub Desktop.
implementing some lodash functions
// 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