Last active
October 21, 2015 10:23
-
-
Save konradhalas/143bd193eae816b60d34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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