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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: friendly_sequences-1.7.tar.gz
  • Upload date:
  • Size: 4.4 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.7.tar.gz
Algorithm Hash digest
SHA256 ff94c7307d6ff4e4f595269872d13841f655548cb797ebe1fda6f86ca9cd70f4
MD5 ac55c09f6ee36e5cd739a444435b5ea0
BLAKE2b-256 1bd246fbcdc92a40fc25794ac4520b3305703e771440f456f92f68de5bfedc2f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: friendly_sequences-1.7-py3-none-any.whl
  • Upload date:
  • Size: 4.0 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.7-py3-none-any.whl
Algorithm Hash digest
SHA256 8b7fbd17c7665a92c7c80c386e74a2d83e16cb5ddc934deab072b552316b8661
MD5 bbb28059e254439c6da8a6612ee5e4b6
BLAKE2b-256 9240f9b90ec3187c624fa07e2624cc0ac8d3a7d067f6771d7a20e224d8448c65

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