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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

friendly_sequences-1.14-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: friendly_sequences-1.14.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.26.4 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for friendly_sequences-1.14.tar.gz
Algorithm Hash digest
SHA256 439ce21ed599986d0d84408e7fa024c0540abc5c3a8894558c86d11b4b1d0019
MD5 87e40104b83d50b170b5d2ce0ab56cc1
BLAKE2b-256 bd7cbcc52627f86e5ee2a6707f2bc1bdb095fcfd41b820740ab9163ffac731ba

See more details on using hashes here.

File details

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

File metadata

  • Download URL: friendly_sequences-1.14-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.26.4 CPython/3.12.3 Linux/6.11.0-1018-azure

File hashes

Hashes for friendly_sequences-1.14-py3-none-any.whl
Algorithm Hash digest
SHA256 97a8c55b3dd872b3466b2cd31975e85e8f4a0b6f2f5c312edc596fe46eb33abf
MD5 f6b6bf4e5bc584dbbee9ed55cb5f723c
BLAKE2b-256 7e83bd28ccd929a09d3f59548c8b3c73d800a70beea4969faeacdbfe48deed60

See more details on using hashes here.

Supported by

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