Skip to content

Instantly share code, notes, and snippets.

@dmyersturnbull
Created July 11, 2016 18:05
Show Gist options
  • Save dmyersturnbull/ebb686dc9f86cbb76013f1174026fb34 to your computer and use it in GitHub Desktop.
Save dmyersturnbull/ebb686dc9f86cbb76013f1174026fb34 to your computer and use it in GitHub Desktop.
Efficient existential quantifier for a filter() predicate.
from typing import Callable, Generic, TypeVar, Iterable
T = TypeVar('T')
def exists(keep_predicate: Callable[[T], bool], seq: Iterable[T]) -> bool:
"""Efficient existential quantifier for a filter() predicate.
Returns true iff keep_predicate is true for one or more elements."""
for e in seq:
if keep_predicate(e): return True # short-circuit
return False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment