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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file predica-0.1.1.tar.gz.

File metadata

  • Download URL: predica-0.1.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for predica-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e4da5f9d4b3bcdc99fe0b3770e89f3a41aa7c8fae26339b7886bbe725afd4929
MD5 e9159ca10b5a19f2076d2ab36bd9c9aa
BLAKE2b-256 97f60af531b36a3532697e19f81e2d43a08de09b32e5ee228d06d04217555c52

See more details on using hashes here.

File details

Details for the file predica-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: predica-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.13

File hashes

Hashes for predica-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cd170f2e1320ba9ce0bcd5d99ae774f76c91c4caca6a0b36e1f603a4abcd6166
MD5 59d4b57cf11a8b4dc69cb2001f88948b
BLAKE2b-256 852cacc0812ca1e224f80b1dbe2493b4b1142ef1d9d9841f8f3d7e72ad472629

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