Source code for aspis.common.any_pass

from typing import Callable, Iterable, Sequence, TypeVar

from .any import any_satisfy
from .compose import compose
from .curry import curry

T = TypeVar("T")


[docs] @curry def any_pass(preds: Sequence[Callable[[T], bool]], var: T | Iterable[T]) -> bool: """ Checks if any predicate in the given sequence returns True when applied to the input variable. Args: preds (Sequence[Callable[[T], bool]]): A sequence of predicate functions that take an element of type `T` and return a boolean. var (T | Iterable[T]): The value or iterable of values to test against the predicates. Returns: bool: True if any predicate returns True for the given value or iterable; otherwise, False. Examples: With single values: >>> is_positive = lambda x: x > 0 >>> is_even = lambda x: x % 2 == 0 >>> any_pass([is_positive, is_even], 4) True >>> any_pass([is_positive, is_even], -2) True With iterables: >>> any_pass([lambda x: x > 5, lambda x: x < 0], [1, 2, 3]) False >>> any_pass([lambda x: x > 5, lambda x: x < 0], [1, -2, 3]) True """ try: return compose(any, map)(lambda pred: pred(var), preds) except Exception: return compose(any, map)(lambda pred: any_satisfy(pred, var), preds)