Skip to content

Instantly share code, notes, and snippets.

@bak1an
Created August 30, 2012 21:44
Show Gist options
  • Save bak1an/3541956 to your computer and use it in GitHub Desktop.
Save bak1an/3541956 to your computer and use it in GitHub Desktop.
lol, kind of Maybe in python
# found here http://stackoverflow.com/a/8507440
class Maybe():
def andThen(self, action): # equivalent to Haskell's >>=
if self.__class__ == _Maybe__Nothing:
return Nothing
elif self.__class__ == Just:
return action(self.value)
def followedBy(self, action): # equivalent to Haskell's >>
return self.andThen(action)
class _Maybe__Nothing(Maybe):
def __repr__(self):
return "Nothing"
Nothing = _Maybe__Nothing()
class Just(Maybe):
def __init__(self, v):
self.value = v
def __repr__(self):
return "Just(%r)" % self.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment