Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Minimizing control flow in loops is a good idea, but if you can FP-ize the loop entirely that keeps it pretty readable IMO.

    things
        .iter()
        .filter(|t| !t.is_yellow())
        .take_while(|t| !t.is_rainbow())
        .for_each(|t| t.thingify());


That's the nicest counter-example to my example, thanks for that! I wasn't familiar with take_while() (looks like that's Rust, and looks like Python has a similar itertools.takewhile() ), TIL a neat FP version of break. My example was quite trivial, it's not always so obvious how to break up a convoluted loop, but it should always be possible.


That's the one I would prefer. If you're at all used to FP, this signals intent very clearly.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: