Skip to content

Instantly share code, notes, and snippets.

@arkenidar
Created April 3, 2018 14:11
Show Gist options
  • Save arkenidar/c4973e10b602d612c22af1b4b6391464 to your computer and use it in GitHub Desktop.
Save arkenidar/c4973e10b602d612c22af1b4b6391464 to your computer and use it in GitHub Desktop.
select first or all callables based on check of conditions specified in callables
#select.py
#select first or all callables based on check of conditions specified in callables
def select(cases, mode='first'): # all or first
returned=[]
for icase in cases:
if icase[0]():
returned.append(icase[1]())
if mode=='first': return returned[0]
return returned
def testSelect(x, mode):
print(x,
select([
[lambda: x<10 and x>0, lambda: 'x<10 and x>0'],
[lambda: x==10, lambda: 'x==10'],
[lambda: x>=10, lambda: 'x>=10'],
[lambda: True, lambda: 'always true'],
], mode)
)
print('***************************************************************')
testSelect(20,'all')
print('***************************************************************')
testSelect(0,'first')
print('***************************************************************')
testSelect(10,'all')
print('***************************************************************')
testSelect(10,'first')
print('***************************************************************')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment