Skip to content

Instantly share code, notes, and snippets.

@rgrannell1
Created September 24, 2013 20:59
Show Gist options
  • Save rgrannell1/6691159 to your computer and use it in GitHub Desktop.
Save rgrannell1/6691159 to your computer and use it in GitHub Desktop.
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