Created
February 28, 2018 16:03
-
-
Save snewell92/3ae9a295df3bc07b7b568682a8a1b941 to your computer and use it in GitHub Desktop.
Some thing to map but stop on a failure
This file contains 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
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