Created
April 21, 2019 11:01
-
-
Save Tadaboody/09ec7cad81751e5425484f9530773c09 to your computer and use it in GitHub Desktop.
Returns the element that satisfies the predicate, if exists
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import TypeVar, Sequence, Optional, Callable | |
T = TypeVar("T") | |
def find_in_sequence( | |
sequence: Sequence[T], predicate: Callable[[T], bool] | |
) -> Optional[T]: | |
"""Returns the element that satisfies the predicate, if exists""" | |
return next((element for element in sequence if predicate(element)), None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment