Last active
November 2, 2018 12:31
-
-
Save vlad-bezden/7ca08e3dd0bb19a3eac099eca797cd53 to your computer and use it in GitHub Desktop.
Python doesn't have 'first' function. Here is an example of implementation of it.
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 Callable, Iterable, Any | |
def first(predicate: Callable, collection: Iterable) -> Any: | |
for x in collection: | |
if predicate(x): | |
return x | |
def first(predicate: Callable, collection: Iterable) -> Any: | |
return next((x for x in collection if predicate(x)), None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment