Skip to main content

Predicates for use in higher-order functions.

Project description

predica


Predicates for use in higher-order functions.

Installation

pip install predica

Usage

Let's illustrate usage with three examples, gradually improving the composability of the code:

Simple example -

from predica import P  # you can import `Predicates` too

ages = [1, 10, 100, 1000, '1']

# before
ages_no_str = list(filter(lambda x: not isinstance(x, str), ages)) # [1, 10, 100, 1000]

# after
new_ages_no_str = list(filter(P.not_instanceof(str), ages)) # [1, 10, 100, 1000]

Suppose you have a pre-existing function is_str -

def is_str(x: str) -> bool:
    return isinstance(x, str)

# before
ages_no_str = list(filter(lambda x: not is_str(x), ages))  # [1, 10, 100, 1000]

# after
new_ages_no_str = list(filter(P.negation(is_str), ages))  # [1, 10, 100, 1000]

IMO this works best with returns library (and is why I made this library to begin with) -

from returns.result import Result
from functools import partial

ages = Result.from_value([1, 10, 100, 1000, '1'])

ages.map(partial(filter, P.not_instanceof(str))).map(list)  # Success([1, 10, 100, 1000])
# or
ages.map(partial(filter, P.negation(is_str))).map(list)  # Success([1, 10, 100, 1000])

Much better 😮

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

predica-0.1.1.tar.gz (5.7 kB view hashes)

Uploaded Source

Built Distribution

predica-0.1.1-py3-none-any.whl (4.3 kB view hashes)

Uploaded Python 3

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