Skip to content

Instantly share code, notes, and snippets.

@sperand-io
Last active October 31, 2015 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sperand-io/6b5e0f77a3d0998bc256 to your computer and use it in GitHub Desktop.
Save sperand-io/6b5e0f77a3d0998bc256 to your computer and use it in GitHub Desktop.
<3 ES6
export default function applyFirst (fn, first) {
return (...args) => fn(first, ...args);
}
function variadic(fn) {
if (fn.length < 1) return fn;
return function() {
var ordinaryArgs = (1 <= arguments.length ? __slice.call(arguments, 0, fn.length - 1) : []),
restOfTheArgsList = __slice.call(arguments, fn.length - 1),
args = (fn.length <= arguments.length ? ordinaryArgs.concat([restOfTheArgsList]) : []);
return fn.apply(this, args);
}
};
module.exports = function applyFirst(fn, first) {
return variadic(function (args) {
return fn.apply(this, [first].concat(args))
})
}
import applyFirst from './applyFirst'
function greet(me, you, them) {
return `Hello, ${ you } and ${ them }, my name is ${ me }`;
}
const chrisGreets = applyFirst(greet, 'Chris');
chrisGreets('Nathan', 'everyone else');
// => "Hello, Nathan and everyone else, my name is Chris"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment