4 ms·
You don't have to add a loop to do your validation: if not all(is_valid, the_values): inform_user("You supplied bad data") Or perhaps bad_val
by Zak 12y ago
You don't have to add a loop to do your validation:
if not all(is_valid, the_values):
inform_user("You supplied bad data")
Or perhaps
bad_values = select(is_not_valid, the_values)
if some(bad_values):
inform_user("The following values were bad", bad_values)
Yes, sometimes you might want to do several things in a single iteration for reasons of efficiency, but in that case you can compose() the functions representing the per-element operations.
This code is only "cryptic" to people not used to functional programming. If the intended audience is the set of everyone who knows Python, using these abstractions will reduce the number of people who understand the code. If it's your startup's team of three programmers, there's a good chance the time saved is worth the learning curve.