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

Uploaded Source

Built Distribution

friendly_sequences-1.4-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: friendly_sequences-1.4.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.18.2 CPython/3.10.12 Linux/6.5.0-1025-azure

File hashes

Hashes for friendly_sequences-1.4.tar.gz
Algorithm Hash digest
SHA256 4690b97dba7af809d699058b1ab7c102f711497248c8318ff544a5065ba225b0
MD5 804f69d902ff5890e3fbe6290cfd00af
BLAKE2b-256 0edb2bdcbf4e6e0cada977f45201c36c8453afde1115647ce1b7f50ecb2bc78e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for friendly_sequences-1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ed40e64b07e77a1b4789ef1b102f3f10e9cf4fc4c83e1752595f907cf716b8be
MD5 cb10066704cec1bc495ff6c1dd3168c8
BLAKE2b-256 0ba4143fa585f78470d3e880ecbb2b90b5d9781991d14d3b1f88bfb943458b23

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