Skip to content

Instantly share code, notes, and snippets.

@neftaly
Created April 10, 2016 15:00
Show Gist options
  • Save neftaly/a3ffcec75043c6581d9d5713f218f177 to your computer and use it in GitHub Desktop.
Save neftaly/a3ffcec75043c6581d9d5713f218f177 to your computer and use it in GitHub Desktop.
mapWhile - cancellable map function
import R from "ramda";
const mapWhile = R.curry((
predicate, // while fn - receives current partial list & value, returns bool
transformation, // map fn
list // source list
) => R.reduce((oldAcc, oldVal) => {
const newVal = transformation(oldVal);
const newAcc = [ ...oldAcc, newVal ];
return predicate(newAcc, newVal) ? newAcc : R.reduced(oldAcc);
}, [], list));
mapWhile(
x => x.length < 5,
x => x + 1,
[0, 0, 0, 1, 0, 0]
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment