I have a for loop in python like this:
for item in items: onwards = make_flow_decision(item) if onwards == 'break': break elif onwards == 'continue': continue elif onwards == 'carry_on': process_the_item(item)
So idea is that flow control for the loop is factored out because it is really complex.
Is there a better way of doing this than passing strings around and checking for special strings like 'continue', 'break', and 'carry_on'?
Looking for ideas generally that make this a little more elegant.
continuecases can be
filter()ed out and the "normal" case can be
map()ed, but the
breakmay be tricky depending on how much you care about leaving some but not all items processed at the end.
make_flow_decision()was cheap to check once a break was needed. Then, instead of actually
breaking out of the loop, I could just continue through it until it's done. It'd mean needless iteration, but if cheap enough that's not so bad. The resulting code could be simplified to,
if press_on: process_the_item(). And the check would just emulate a break by continuing until the last item.