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 friendly-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
Built Distribution
File details
Details for the file friendly_sequences-1.9.tar.gz
.
File metadata
- Download URL: friendly_sequences-1.9.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.20.1 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80105b6379980011c215ffaf1e06df8ff172eeede80194c8c7f1cb4478359151 |
|
MD5 | 778635746ce6c8f828e2447daee6e36e |
|
BLAKE2b-256 | 6a61975fbbad365b67dbfd08bc2f38577be1dd5ae57e921bfbdd54fe34a4ad53 |
File details
Details for the file friendly_sequences-1.9-py3-none-any.whl
.
File metadata
- Download URL: friendly_sequences-1.9-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: pdm/2.20.1 CPython/3.10.12 Linux/6.5.0-1025-azure
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 701578b3e7c313312d1687f99ed5dda16fc61b5ac7b8fc19537e3597e018dc2a |
|
MD5 | 2e4744291eceba5fc12f92f0a91e110d |
|
BLAKE2b-256 | 231098da674d36c3fbc0d597333bbafbf4299fd87bcd7ab523b3c0efcc717142 |