Created
July 28, 2021 03:04
-
-
Save crazygit/910e11ef9724c532f527ab7839c7df6e to your computer and use it in GitHub Desktop.
firstOrNone Implementation in python
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
def first_or_none(predicate, seq): | |
return next(filter(predicate, seq), None) | |
if __name__ == '__main__': | |
print(first_or_none(lambda x: x % 2 == 0, [2, 4, 6, 8])) # 2 | |
print(first_or_none(lambda x: x % 2 == 0, [1, 3, 5, 7])) # None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment