Skip to content

Instantly share code, notes, and snippets.

@jrsinclair
Last active January 10, 2018 22:01
Show Gist options
  • Save jrsinclair/e6a9767bce66aa8797770f527e69ae5c to your computer and use it in GitHub Desktop.
Save jrsinclair/e6a9767bce66aa8797770f527e69ae5c to your computer and use it in GitHub Desktop.
import {curry} from 'lodash/fp';
/**
* Chain (flatMap) for Functions.
*
* chainf: (a -> x -> b) -> (x -> a) -> x -> b
*
* @param {Function} g A function that takes two parameters of type a and x.
* @param {Function} h A function that takes one paramter of type x.
* @param {*} x A value of type x.
* @return {*} g(h(x), x)
*/
const chainf = curry(function chainf(g, h, x) { // eslint-disable-line prefer-arrow-callback
return g(h(x), x);
});
// This can also be written in the following way, which might be clearer.
// chainf: (a -> x -> b) -> (x -> a) -> (x -> b)
// const chainf = curry(function chainf(g, h) {
// return x => g(h(x), x);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment