Last active
August 29, 2015 13:56
-
-
Save beoliver/9089805 to your computer and use it in GitHub Desktop.
ifElse function
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
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