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.8.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

friendly_sequences-1.8-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: friendly_sequences-1.8.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

Hashes for friendly_sequences-1.8.tar.gz
Algorithm Hash digest
SHA256 fb0e897f0aa365b0e100fdc4f341739cc16ca3c224c8201db091e25c45fe50c9
MD5 d8104fbd56147ca2a09818ac60935064
BLAKE2b-256 04e293460b7b144c69db21748b5edae8f5627edee2d40857bbba73ff300501c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: friendly_sequences-1.8-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

Hashes for friendly_sequences-1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 053d04ea565369456a14f4e5c727908be6f8ae4629940305646e97680a07b672
MD5 ba4fd63a913395a15f0a120982b14963
BLAKE2b-256 670ec5022748ec3c09c0e9fda6771bfd2257d78c7eb53347d2806a0f6972772d

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