Created
August 30, 2012 21:44
-
-
Save bak1an/3541956 to your computer and use it in GitHub Desktop.
lol, kind of Maybe in python
This file contains 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
# 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