r - How to call top level from lapply loop (skip/pass) -


i want create lapply loop if attends requirement, stop...
example:

score <- 0 lapply(1:100, function(z){      score <<- score + 1      if(score >=10){         break     } }) 

however, there no stop argument break/pass in lapply loop.

i know example sounds stupid. however, original code has many dependencies understood... original loop removes item vector object every time, however, if there nothing else removed can stop. i've gain @ least 0.10 seconds in normal loop short sized function

with normal "for loop" skip argument

> time <- system.time({cyclopeptide_score(sequence, spectrum)}) > time   usuário   sistema decorrido       6.58      0.00      6.58  

with laplly no skip argument

> time <- system.time({cyclopeptide_score2(sequence, spectrum)}) > time   usuário   sistema decorrido       6.72      0.00      6.72  

to directly answer question, here option (assuming have control of code lapply happens, , of function being applied):

withrestarts(   lapply(     1:10,      function(x) {       cat(x)       if(x > 5) invokerestart("stoploop")   } ),   stoploop=function() message("loop stopped") ) 

produces:

123456 loop stopped 

basically, withrestarts/invokerestart acts little bit goto statement, allows break out of loop.

all said, wouldn't base big code refactorings efforts on 0.1 second improvement on 6.7 seconds run time.

also, if can above, can turn code for loop, seems more appropriate given desire a) break out of loop, b) use <<- operator cause side effects.


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -