Skip to content

Instantly share code, notes, and snippets.

@snewell92
Created February 28, 2018 16:03
Show Gist options
  • Save snewell92/3ae9a295df3bc07b7b568682a8a1b941 to your computer and use it in GitHub Desktop.
Save snewell92/3ae9a295df3bc07b7b568682a8a1b941 to your computer and use it in GitHub Desktop.
Some thing to map but stop on a failure
class ContinueOnSuccess {
items = [];
length = 0;
constructor(newItems) {
this.items = newItems;
length = this.items.length;
}
map(f) {
let i = 0;
let cont = true;
let currItem;
for(; i < length; i++) {
if(!cont) {
return; // or return something meaningful
}
currItem = this.items[0];
f(currItem); // this function can mutuate them if you want
cont = currItem.IS_STATUS_GOOD; // <-- mutation
}
}
}
// SUPER SEXY REDUCE WAY
// name?
const Continuation = xs => ({
items: xs,
map: f => xs.reduce(
(cont, item) => {
if(!cont) {
return;
}
f(item);
return item.IS_STATUS_GOOD;
},
true)
});
const newItems = Continutation(getItems());
newItems.map(i => { /* blah */})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment