Created
April 10, 2016 15:00
-
-
Save neftaly/a3ffcec75043c6581d9d5713f218f177 to your computer and use it in GitHub Desktop.
mapWhile - cancellable map function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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