Skip to content

Instantly share code, notes, and snippets.

@ehrenmurdick
Created March 20, 2017 20:42
Show Gist options
  • Save ehrenmurdick/89fabca7852d34f90300eac73b598c72 to your computer and use it in GitHub Desktop.
Save ehrenmurdick/89fabca7852d34f90300eac73b598c72 to your computer and use it in GitHub Desktop.
partial function application
-- in haskell, this is all abstracted away by syntactic sugar.
-- although the function type syntax probably looks familiar.
add :: Integer -> Integer -> Integer
add x y = x + y
-- can curry as well
inc = add 1
-- haskell functions all only have one arg in reality,
-- further arguments are always handled this way
// partial function application
function add(a) {
return function(b) {
return a + b;
}
}
// call it like this
add(1)(2);
// es6 gives you syntactic sugar for this
const add = (a) => (b) => {
return a + b
}
// you can also "curry" a partial function, by pre-calling it with not enough args.
const inc = add(1);
inc(2);
// returns 3
// useful in js for doing stuff like parameterized buttons
const buttonPressed = (name) => () => {
alert(name + " pressed!");
}
// <button onclick="buttonPressed('A')">A</button>
// <button onclick="buttonPressed('B')">B</button>
// in js, the number of args that each nested function is
// not fixed, could easily do something like this:
// foo(a, b)(c, d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment