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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: friendly_sequences-1.5.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.5.tar.gz
Algorithm Hash digest
SHA256 3d0e1391c0809ffa0b0e18860497c2173c10238f6ee89e8a1b8e532e54118286
MD5 1d1d4337efc03aff0e22174098b5952e
BLAKE2b-256 27f988f5977a1777561c0e399b8a474e3f815020ae84602d200b9aeda48cfb10

See more details on using hashes here.

File details

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

File metadata

  • Download URL: friendly_sequences-1.5-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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 de898e2103afce3652dd2b176c35e2ea0f3b81abc018bbc972bde23287822ea2
MD5 52032acf5794e0a34c1b3954fdb49133
BLAKE2b-256 7eb253299edcb0d001be19864af8af00f0b9decaec372e32a900974d63012574

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