Last active
July 9, 2021 07:29
-
-
Save muthuishere/3ebc2f22cd43d320fc6f663506713139 to your computer and use it in GitHub Desktop.
Python Functional Streams
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
female_users = (Stream | |
.create(users) | |
.filter(lambda user: user['gender'] == 'Female')) | |
first_names_of_female_users = (female_users | |
.stream() | |
.map(lambda user: user['first_name']) | |
.asList()) | |
#first_names_of_female_users ['Mandy', 'Janessa'] | |
things_female_users_love = (female_users | |
.stream() | |
.flatmap(lambda user: user['loves']) | |
.asList()) | |
#things_female_users_love ['Soccer', 'Cricket', 'Golf', 'Cricket'] |
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
skip_five_and_take_three_items = (Stream | |
.transducer() | |
.skip(5) | |
.take(3) | |
) | |
skip_five_and_take_three_items_within_zero_to_hundred = (Stream | |
.create(range(100)) | |
.pipe(skip_five_and_take_three_items) | |
.asList() | |
) | |
# Result [5, 6, 7] | |
skip_five_and_take_three_items_within_700_to_800 = (Stream | |
.create(range(700, 800)) | |
.pipe(skip_five_and_take_three_items) | |
.asList() | |
) | |
#Result [705, 706, 707] |
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
list(map(lambda user: user['first_name'], | |
filter(lambda user:user['salary'] > 100000,users) | |
)) |
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
results = (Stream | |
.create(users) | |
.filter(lambda user:user['salary'] > 80000) | |
.map(lambda user: user['first_name']) | |
.asList()) |
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
users = [ | |
{ | |
"id": 1, | |
"first_name": "Mandy", | |
"last_name": "Gowan", | |
"email": "mgowan0@aol.com", | |
"gender": "Female", | |
"loves": ['Soccer', 'Cricket', 'Golf'], | |
"salary": 119885 | |
}, | |
{ | |
"id": 2, | |
"first_name": "Janessa", | |
"last_name": "Cotterell", | |
"email": "jcotterell1@aol.com", | |
"gender": "Female", | |
"loves": ['Cricket'], | |
"salary": 107629 | |
}, | |
{ | |
"id": 6, | |
"first_name": "Jasen", | |
"last_name": "Franzini", | |
"email": "jfranzini5@aol.com", | |
"gender": "Male", | |
"loves": ['Soccer', 'Golf'], | |
"salary": 78373 | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment