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

Surely you are joking? Your version is four times longer and repeats a large section of itself.

The way to avoid control flow here is to simply find the rainbow first, take the sequence up until then, filter by yellow, and thingify the remaining things.

    rainbow_idx = next(i for i, t in enumerate(things) if t.is_rbow())
    yellows = (t for t in things[:rainbow_idx] if t.is_ylw())
    for t in yellows:
        t.thngry()
Excuse formatting, cellphone.

Not sure this is any better than the obvious solution either.



Yes, I agree, that's the way to do it. Although it isn't always so obvious how to break it up into limit / filter steps, my example was quite trivial. But there should always be a way somehow.




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

Search: