Skip to content

Instantly share code, notes, and snippets.

@crazygit
Created July 28, 2021 03:04
Show Gist options
  • Save crazygit/910e11ef9724c532f527ab7839c7df6e to your computer and use it in GitHub Desktop.
Save crazygit/910e11ef9724c532f527ab7839c7df6e to your computer and use it in GitHub Desktop.
firstOrNone Implementation in python
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