Created
September 24, 2013 20:59
-
-
Save rgrannell1/6691159 to your computer and use it in GitHub Desktop.
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
prod <- function (coll) { | |
# callCC lets you jump out of ANY function early, | |
# by specifying a breakpoint function (often called 'k'). | |
# if I used return instead of breakpoint here the | |
# program would take a long time to terminate. | |
# 1; breakpoint jumps up to this level if it is called, evalulating to zero | |
callCC( | |
function (breakpoint) { | |
Reduce ( | |
function (acc, new) { | |
# 2; using return would only break this step of the Reduce | |
# function; it wouldn't exit reduce altogether. | |
if (new == 0) { | |
breakpoint (0) | |
} else { | |
Sys.sleep(0.1) | |
acc * new | |
} | |
}, | |
coll | |
) | |
}) | |
} | |
prod(c(1, 2, 0, 1:100000)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment