python - Shortcut OR-chain applied on list -


i'd this:

x = f(a[0]) or f(a[1]) or f(a[2]) or f(a[3]) or … 

with given list a , given function f. unlike built-in any function need first value of list considered true; 0 or "foo" or 3.2 need "foo", not true.

of course, write small function like

def returnfirst(f, a):   in a:     v = f(i)     if v:       return v   return false  x = returnfirst(f, a) 

but that's not nicest solution, reasons given in this question. mention other thread, of course use code based on solution given there, e.g.

x = next((f(x) x in if f(x)), false) 

but don't see simple way circumvent doubled calling of f then.

is there simple solution missing or don't know? an

or((f(x) x in a)) 

maybe?

i tried find other questions concerning this, searching keywords or bit problematic in so, maybe didn't find appropriate.

this should work:

next((x y in x in (f(y),) if x),false) 

Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -