Friendly sequences made in Python with :love:
Project description
Friendly Sequences
Inspired by Scala Sequence class [1] and iterchain [2], but with a good typing support.
[1] https://alvinalexander.com/scala/seq-class-methods-examples-syntax/ [2] https://github.com/Evelyn-H/iterchain
Motivation
It is possible to compose functions in python with many functional programming primitives, like map, filter, reduce etc. But, in my opinion, looks a bit ugly and you need to get use to this structure. For example, you can write something like this:
import itertools
from functools import reduce
assert (
reduce(
lambda left, right: f"{left}{right}",
map(
str,
sorted(
filter(
lambda x: x != 2,
map(
lambda x: x + 1,
itertools.chain.from_iterable(
zip(
(1, 2),
(3, 4),
)
),
),
)
),
),
"",
)
== "345"
)
or even this:
import itertools
assert (
"".join(
sorted(
str(x)
for x in (
x
for x in (
x + 1
for x in itertools.chain.from_iterable(
zip(
(1, 2),
(3, 4),
)
)
)
if x != 2
)
)
)
== "345"
)
but with the friendly-sequences it is just this:
from friendly_sequences import Seq
assert (
Seq[int]((1, 2))
.zip(Seq[int]((3, 4)))
.flat_map(lambda x: x + 1)
.filter(lambda x: x != 2)
.sort()
.map(str)
.fold(lambda left, right: f"{left}{right}", "")
) == "345"
Installation
$ pip install freindly-sequences
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
friendly_sequences-1.2.tar.gz
(3.9 kB
view hashes)
Built Distribution
Close
Hashes for friendly_sequences-1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec5cee75b7b1f766e1b488a5d71e163a71ec9f511ad4a874f4258499f0a26aa2 |
|
MD5 | bc9df27a17f42066a0340fea1efa51bb |
|
BLAKE2b-256 | 30477ba5461d0c7b397b0f705b113fd367541208f9cb405ebc6899cd66dbc753 |