Skip to content

Instantly share code, notes, and snippets.

@jonathanKingston
Last active December 16, 2015 20:53
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 jonathanKingston/4df71289a2cd8dd8306a to your computer and use it in GitHub Desktop.
Save jonathanKingston/4df71289a2cd8dd8306a to your computer and use it in GitHub Desktop.
Explanation of pipeline alternative

Explanation of: https://twitter.com/katyemoe/status/677122509645455360

function doubleSay (str) {
  return str + ", " + str;
}
function capitalize (str) {
  return str[0].toUpperCase() + str.substring(1);
}
function exclaim (str) {
  return str + '!';
}

This covers the basic use case:

function pipeline(input, ...methods) {
  for (let method of methods) {
    input = method(input);
  }
  return input;
}

let result = pipeline('hello', doubleSay, capitalize, exclaim)

Would be the same as:

let result = "hello"
  |> doubleSay
  |> capitalize
  |> exclaim;

Multiple arguments could be solved like:

let newScore = pipeline(
  person.score,
  double,
  _ => add(7, _),
  _ => boundScore(0, 100, _)
)

Would be the same as:

let newScore = person.score
  |> double
  |> _ => add(7, _)
  |> _ => boundScore(0, 100, _);

This could also work async too with something like:

function pipeline(input, ...methods) {
  for (let method of methods) {
    input = await Promise.resolve(method(input));
  }
  return input;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment