I am trying to figure out how to get a call call function for a function short circuit function to work with functions like lapply and Reduce.
Motivation
This would lead to a decrease and, accordingly, asymptotic efficiency> O (n), allowing you to exit the calculation earlier.
For example, if I search for a value in a list, I could display the “finder” function in the list, and the second one that is found will stop working, and that value will be returned (similar to breaking a loop, or using the return statement to break out earlier).
The problem is that I am having trouble writing the functions that apply, and the reduction should be done using the style that callCC requires.
Example
Let's say I'm trying to write a function to find the value "100" in a list: something equivalent
imperativeVersion <- function (xs) {
for (val in xs) if (val == 100) return (val)
}
The transition function to lapply will look like this:
find100 <- function (val) { if (val == 100) SHORT_CIRCUIT(val) }
functionalVersion <- function (xs) lapply(xs, find100)
This is (obviously) a failure since the short circuit function has not yet been defined.
callCC( function (SHORT_CIRCUIT) lapply(1:1000, find100) )
The problem is that it also crashes because the short circuiting function was not around when find100 was found. I would like something like this to work.
the following action is performed because SHORT_CIRCUIT IS is defined when the function passed to lapply is created.
callCC(
function (SHORT_CIRCUIT) {
lapply(1:1000, function (val) {
if (val == 100) SHORT_CIRCUIT(val)
})
)
How can I define SHORT_CIRCUIT in a function passed to lapply without defining it as a string?
I know that this example can be achieved using loops, reduction, or any other number of ways. I am looking for a solution to the problem of using callCC with lapply and shortening in concrete.
- , , . , - :)
:
" "; .