Equivalent break for the lapply function
You can't fix it, and there aren't any simple workarounds. lapply()
doesn't support break
. If you want break, you need to use a different kind of loop.
Here's one workaround:
le <- letters
fu <- local({
broken <- FALSE
function(i){
if (broken) return()
## some code
print(le[i])
if(i==5) broken <<- TRUE
}
})
res <- lapply(1:length(le) , fu)
#> [1] "a"
#> [1] "b"
#> [1] "c"
#> [1] "d"
#> [1] "e"
Created on 2021-09-05 by the reprex package (v2.0.0)
If you look at res
, you'll see that it contains 21 NULL
s, because it is still called 26 times. You can't stop that without very dangerous messing around in lapply()
internal variables.
How to break out or exit a method in Java?
Use the return
keyword to exit from a method.
public void someMethod() {
//... a bunch of code ...
if (someCondition()) {
return;
}
//... otherwise do the following...
}
From the Java Tutorial that I linked to above:
Any method declared void doesn't return a value. It does not need to contain a return statement, but it may do so. In such a case, a return statement can be used to branch out of a control flow block and exit the method and is simply used like this:
return;
What is the best way to exit a function (which has no return value) in python before the function ends (e.g. a check fails)?
You could simply use
return
which does exactly the same as
return None
Your function will also return None
if execution reaches the end of the function body without hitting a return
statement. Returning nothing is the same as returning None
in Python.
Why does Javascript forEach not allow early termination using break or return
These functional iterator methods don't "break" like normal "for" loops probably because when you want to do "forEach" they probably were thinking you intentionally want to do something "for each" value in the array. To do what you want to do there as in "finding" the correct item, you can use "find"
var theSecond = findTheSecond();console.log('theSecond is: ' + theSecond)
function findTheSecond(){ return ( [1,2,3].find(function(e1) { console.log('Item: ', e1); return e1 === 2 }) )}
Early exit from function?
You can just use return
.
function myfunction() {
if(a == 'stop')
return;
}
This will send a return value of undefined
to whatever called the function.
var x = myfunction();
console.log( x ); // console shows undefined
Of course, you can specify a different return value. Whatever value is returned will be logged to the console using the above example.
return false;
return true;
return "some string";
return 12345;
break statement not working in function in clips
You use a break statement to terminate a loop (or in some languages a switch statement). If you want the function to terminate after a condition has been satisfied, use a return statement.
CLIPS (6.31 6/12/19)
CLIPS>
(deffunction slabFunction (?q0 ?q1 ?q2)
(if (and (>= ?q0 36) (>= ?q1 36) (>= ?q2 36))
then
(printout t "slab 3" crlf)
(return))
(if (and (>= ?q0 24) (>= ?q1 24) (>= ?q2 24))
then
(printout t "slab 2" crlf)
(return))
(if (and (>= ?q0 12) (>= ?q1 12) (>= ?q2 12))
then
(printout t "slab 1" crlf)
(return)))
CLIPS> (slabFunction 40 40 40)
slab 3
CLIPS> (slabFunction 26 26 45)
slab 2
CLIPS> (slabFunction 56 13 33)
slab 1
CLIPS> (slabFunction 10 3 2)
FALSE
CLIPS>
Breaking out of a loop from within a function called in that loop
break
, like goto
, can only jump locally within the same function, but if you absolutely have to, you can use setjmp
and longjmp
:
#include <stdio.h>
#include <setjmp.h>
jmp_buf jump_target;
void foo(void)
{
printf("Inside foo!\n");
longjmp(jump_target, 1);
printf("Still inside foo!\n");
}
int main(void) {
if (setjmp(jump_target) == 0)
foo();
else
printf("Jumped out!\n");
return 0;
}
The call to longjmp
will cause a jump back to the setjmp
call. The return value from setjmp
shows if it is returning after setting the jump target, or if it is returning from a jump.
Output:
Inside foo!
Jumped out!
Nonlocal jumps are safe when used correctly, but there are a number of things to think carefully about:
- Since
longjmp
jumps "through" all the function activations between thesetjmp
call and thelongjmp
call, if any of those functions expect to be able to do additional work after the current place in execution, that work will simply not be done. - If the function activation that called
setjmp
has terminated, the behaviour is undefined. Anything can happen. - If
setjmp
hasn't yet been called, thenjump_target
is not set, and the behaviour is undefined. - Local variables in the function that called
setjmp
can under certain conditions have undefined values. - One word: threads.
- Other things, such as that floating-point status flags might not be retained, and that there are restrictions on where you can put the
setjmp
call.
Most of these follow naturally if you have a good understanding of what a nonlocal jump does on the level of machine instructions and CPU registers, but unless you have that, and have read what the C standard does and does not guarantee, I would advise some caution.
Related Topics
How to Get Rstudio to Show Function Arguments and Descriptions for Custom Functions
Filter Dataframe Using Global Variable with The Same Name as Column Name
How to Use Custom Cross Validation Folds with Xgboost
Ggplot2: Shape, Color and Linestyle into One Legend
R: How to Get Row and Column Names of The True Elements of a Matrix
R Markdown Add Tag to Head of HTML Output
Ggplot2: How to Separate Geom_Polygon and Geom_Line in Legend Keys
R Dplyr Mutate, Calculating Standard Deviation for Each Row
Group/Bin/Bucket Data in R and Get Count Per Bucket and Sum of Values Per Bucket
Removing/Replacing Brackets from R String Using Gsub
Mlogit: Missing Value Where True/False Needed
How to Keep Track of Total Transaction Amount Sent from an Account Each Last 6 Month
Integrate() Gives Totally Wrong Number
How to Convert Characters into Ascii Code