Hacker News new | comments | show | ask | jobs | submit login

Thanks, up voted you for the explanation via code sample. It helped click the concept for me. As you mentioned there is an ability to refactor forEach to support early termination and as you state I agree is inelegant--however, I think for different reasoning. Due to the natural language of the method name "for each" there should not be any case where the iteration should end early. That's why I think adding a sentinel return value from the call function is inappropriate. However, if the method name were forEachUntil, then the natural language of the method "for each until" suggests that control flow can, and in at least some cases, should end prematurely. In that case, a sentinel return value is no longer inelegant--its appropriate. To me, that doesn't seem like "going out of the way".

I do have a question regarding early returning the outer function scope, what happens if in your forEach definition, you actually do work on some (or all) elements that needs to be undone. If the outer forEach function is returned early by the anonymous function, how will any of the clean up code get called?

Applications are open for YC Winter 2018

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact