7 ms·
Cool story... until you want to BREAK out of the loop.
by PhrosTT 9y ago
Cool story... until you want to BREAK out of the loop.
- addicted 9y agoWouldn't a while loop be a better option there? From what I understand the point of a for over a while is that you will iterate for a certain known number of times. But if you aren't doing that (break) isn't the while a better option so you can encapsulate the break in the condition as opposed to random locations in the block.
- tynpeddler 9y agoSay you have 10,000 lines of text, and you want to find the index of the line that contains the 302nd 'U'. You could do this with a while loop, but in the process you would find yourself setting up an index and dutifully incrementing it by 1 to keep track of the line you're currently looking at. A for loop is simpler here.
- jackjeff 9y agoWouldn’t a filter() achieve the same result?
- neurotrace 9y agoNo, filter() still visits everything in the list, it just takes a predicate to decide if that item is added to a new list. some() or every() can be used for early termination though.
- tytytytytytytyt 9y agoAre you saying you can't do the equivalent with FP?
- jim-jim-jim 9y agoWell I can't speak for JS, but I use folds with call/cc for this purpose in Scheme.