Skip to content

Instantly share code, notes, and snippets.

@konradhalas
Last active October 21, 2015 10:23
Show Gist options
  • Save konradhalas/143bd193eae816b60d34 to your computer and use it in GitHub Desktop.
Save konradhalas/143bd193eae816b60d34 to your computer and use it in GitHub Desktop.
# return double of first number from collection which is greater than 3 and even
# this is Python 3 srcipt, in Python 2 you have to use itertools.imap and itertools.ifilter
def is_gt_3(number):
print('is_gt_3 called...')
return number > 3
def is_even(number):
print('is_even called...')
return number % 2 == 0
def double_it(number):
print('is_double_it called...')
return number * 2
data = [1, 2, 3, 5, 4, 6, 7, 8, 9, 10]
result = map(double_it, filter(is_even, filter(is_gt_3, data)))
print(next(result))
$ python3 lazy.py
is_gt_3 called...
is_gt_3 called...
is_gt_3 called...
is_gt_3 called...
is_even called...
is_gt_3 called...
is_even called...
is_double_it called...
8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment