Skip to content

Instantly share code, notes, and snippets.

@beoliver
Last active August 29, 2015 13:56
Show Gist options
  • Save beoliver/9089805 to your computer and use it in GitHub Desktop.
Save beoliver/9089805 to your computer and use it in GitHub Desktop.
ifElse function
function ifElse(pred,true_fn,else_fn,xs) {
for (var i = 0; i < xs.length; i++) {
if (pred(xs[i]) === true) {
// return matched item, index, array to yes_func
return true_fn(xs[i],i,xs) }}
// return array to no_func
return else_fn(xs) }
// what does this do?
function returnSliceOnMatch(match,xs) {
return ifElse(
function(x) { return x == match }
,function(matched_item,item_index,xs) { return xs.slice(item_index) }
,function() { return [] } // ignore values passed to function and return empty list
,xs) }
returnSliceOnMatch(5,[1,2,3,4,5,6,7,8,9]) === [5,6,7,8,9]
returnSliceOnMatch(5,[1,2,3,4,6,7,8,9]) === []
function say_hello_if_true(xs) {
return ifElse(
function(x) { return x === true }
,function() {alert("hello")}
,function() {}
,xs) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment