Skip to main content

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

friendly_sequences-1.6.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

friendly_sequences-1.6-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file friendly_sequences-1.6.tar.gz.

File metadata

  • Download URL: friendly_sequences-1.6.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.19.2 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for friendly_sequences-1.6.tar.gz
Algorithm Hash digest
SHA256 98afae63389550d7100842eb10098197b9ecab07f5d1081786b162a7b4de5bc3
MD5 07e3489ea64732dbdb25d9435e6ef7a7
BLAKE2b-256 e0b0bd726b5561acbc30a62dd662e44f42458f31a5df29dfb4bab55b5edcccba

See more details on using hashes here.

File details

Details for the file friendly_sequences-1.6-py3-none-any.whl.

File metadata

  • Download URL: friendly_sequences-1.6-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.19.2 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for friendly_sequences-1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 64dbecb074ec85da05f2c3326b50f7bd5694135e5250f7d1e79d07214c6a4034
MD5 cb2142f2b710824bff316c05faeed518
BLAKE2b-256 c56ee58c685e37d58a322217e4e9128e67c2d546042fc144723d1e1440f914f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page