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

Uploaded Source

Built Distribution

friendly_sequences-1.3-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: friendly_sequences-1.3.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.15.0 CPython/3.10.12 Linux/6.5.0-1018-azure

File hashes

Hashes for friendly_sequences-1.3.tar.gz
Algorithm Hash digest
SHA256 201deb9abae14abb771720626c2f5ee06413f5a5190c4c17b5040dfeb6def7dc
MD5 142c94cff21c30e1400932a06504b994
BLAKE2b-256 e7c7cd04a11d51cbecf962a597cf08f8ec4a2e4732335147489a35f3894db8e2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for friendly_sequences-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 990e43815bbb57e62f4a67aea49d3e2568c2ece7582d0f9baa9955c65b205ff7
MD5 6c78ec2c27acb6ad5ef4dbf2ee362738
BLAKE2b-256 c052175b798f5f571a734f156661f0fe26ea8def2b66d5fc525896603f3500fc

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