functional-streams
Writing concise functional code in python
Demo
list(map(lambda user: user['first_name'],
filter(lambda user:user['salary'] > 80000,
filter(lambda product: product['gender'] == 'Male',
users))))
from streams.Stream import Stream
from streams.operations.operators import item
(Stream
.create(users)
.filter(item['salary'] > 80000)
.filter(item['gender'] == 'Female')
.map(item['first_name'])
.asList())
(Stream
.create(users)
.filter(lambda user:user['salary'] > 80000)
.filter(lambda product: product['gender'] == 'Male')
.map(lambda user: user['first_name'])
.asList())
from streams.Stream import Stream
from streams.operations.operators import item
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
}
]
results = (Stream
.create(users)
.filter(item['salary'] > 80000)
.map(item['first_name'])
.asList())
results = (Stream
.create(users)
.flatmap(item['loves'] )
.distinct()
.asList())
results = (Stream
.create(users)
.skip(1)
.take(1)
.map(item['first_name'])
.asList())
results = (Stream
.create(users)
.peek(lambda data:print("User",data))
.map(item['first_name'])
.asList())
(Stream
.create(users)
.peek(item.print)
.map(item['first_name'])
.asList())
(Stream
.create(range(5))
.map(item * 2)
.asList())
babynames.csv
Id,Male name,Female name
1,Liam,Olivia
2,Noah,Emma
from streams.FileStream import FileStream
from streams.operations.operators import item
(FileStream.createFromCsv(full_path_of_input_csv)
.filter(item['Female name'].startswith("A"))
.map(item['Female name'])
.peek(item.print)
.asCSV(full_path_of_output_csv))
from streams.FileStream import FileStream
(FileStream.createFromText(full_path_of_input_text)
.filter(lambda value: value.startswith("A"))
.peek(lambda val: print(val))
.asTextFile(full_path_of_output_text))
Additional Information
Design
Most of the functions underneath uses the same functions available in python (map uses map , filter uses filter etc..).
Only we have added wrapper to make the code concise
Abstractions
If you need to use abstract items, use the same chaining and just invoke the stream when you are using it
as the generators used get corrupted by the very first expansion
For Example
from streams.Stream import Stream
from streams.operations.operators import item
stream_of_users = (Stream
.create(users)
)
total_users = (stream_of_users
.length())
firstname_of_users = (stream_of_users
.map(lambda user: user['first_name'])
.asList())
total_users = (stream_of_users
.stream()
.length())
firstname_of_users = (stream_of_users
.stream()
.map(lambda user: user['first_name'])
.asList())
Transducers
If you need to use transducers, create with Stream.transducer and connect with pipe whenever required
For Example
skip_five_and_take_three_items = (Stream
.transducer()
.skip(5)
.take(3)
)
skip_five_and_take_three_items_within_zero_to_hundred = (Stream
.createFromText(range(100))
.pipe(skip_five_and_take_three_items)
.asList()
)
skip_five_and_take_three_items_within_700_to_800 = (Stream
.createFromText(range(700, 800))
.pipe(skip_five_and_take_three_items)
.asList()
)
Known Constraints
This section will list down the constraints of library
Single Operator with item
The item object will support only one operation, for more than one operations use lambda or refactor code
from streams.Stream import Stream
from streams.operations.operators import item
(Stream
.create(range(5))
.map(item + 1)
.reduce(item.sum)
.asSingle())
(Stream
.create(range(5))
.map(item + 1)
.reduce(item.sum)
.asSingle())
(Stream
.create(range(10))
.filter(item.isodd)
.asList()
)
(Stream
.create(range(10))
.filter(item % 2 == 1)
.asList()
)
(Stream
.create(range(10))
.filter(lambda value: value % 2 == 1)
.asList()
)
Contributors
This is just a syntactic sugar, with no other third party software involved.
Everything has been written with built-in modules, Because of very hard fights
with yawpitch. I started taking performance,space complexity seriously.
Thanks for the extremely valuable suggestions. I would like to appreciate him for all his suggestions