Skip to content

Instantly share code, notes, and snippets.

@stefanfrede
Last active June 27, 2016 05:34
Show Gist options
  • Save stefanfrede/0ac81b70d56f34bdc1543ac0278106a1 to your computer and use it in GitHub Desktop.
Save stefanfrede/0ac81b70d56f34bdc1543ac0278106a1 to your computer and use it in GitHub Desktop.
Quickly and simply apply a single argument.
// leftmost
const callFirst = (fn, larg) =>
function (...rest) {
return fn.call(this, larg, ...rest);
}
// rightmost
const callLast = (fn, rarg) =>
function (...rest) {
return fn.call(this, ...rest, rarg);
}
const greet = (me, you) =>
`Hello, ${you}, my name is ${me}`;
const heliosSaysHello = callFirst(greet, 'Helios');
heliosSaysHello('Eartha')
//=> 'Hello, Eartha, my name is Helios'
const sayHelloToCeline = callLast(greet, 'Celine');
sayHelloToCeline('Eartha')
//=> 'Hello, Celine, my name is Eartha'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment