Skip to content

Instantly share code, notes, and snippets.

@muthuishere
Last active July 9, 2021 07:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save muthuishere/3ebc2f22cd43d320fc6f663506713139 to your computer and use it in GitHub Desktop.
Save muthuishere/3ebc2f22cd43d320fc6f663506713139 to your computer and use it in GitHub Desktop.
Python Functional Streams
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']
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]
list(map(lambda user: user['first_name'],
filter(lambda user:user['salary'] > 100000,users)
))
results = (Stream
.create(users)
.filter(lambda user:user['salary'] > 80000)
.map(lambda user: user['first_name'])
.asList())
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