Skip to content

Instantly share code, notes, and snippets.

@sepulchered
Created November 7, 2017 21:12
Show Gist options
  • Save sepulchered/06b439c780910d7e06cd3fbd09a8f644 to your computer and use it in GitHub Desktop.
Save sepulchered/06b439c780910d7e06cd3fbd09a8f644 to your computer and use it in GitHub Desktop.
import functools
def maybe(fn):
@functools.wraps(fn)
def wrapper(*args, **kwargs):
try:
return fn(*args, **kwargs)
except Exception:
return None
return wrapper
@maybe
def divide(num, den):
return num / den
def is_just(maybe_val):
return maybe_val is not None
def is_nothing(maybe_val):
return maybe_val is None
print(is_just(divide(1, 2)))
print(is_nothing(divide(1, 0)))
print(is_just(divide(1, 0)))
print(is_nothing(divide(1, 2)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment