Created
August 12, 2018 13:53
-
-
Save mastern2k3/350ae599586325c79c0b25aec5b2c39d to your computer and use it in GitHub Desktop.
FP option category 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
class Option(object): | |
pass | |
class Some(Option): | |
def __init__(self, value): | |
if value is None: | |
raise "Lol cant have some None" | |
self.value = value | |
def map(self, mapper): | |
return Some(mapper(self.value)) | |
def __repr__(self): | |
return "Some(%s)" % self.value | |
class Nothing(Option): | |
def __init__(self): | |
pass | |
def map(self, mapper): | |
return self | |
def __repr__(self): | |
return "Nothing" | |
im_some = Some([1,2,3,4]) | |
im_none = Nothing() | |
print(im_some.map(lambda l: l * 2).map(lambda l: l + [2, 3])) | |
print(im_none.map(lambda l: l * 2).map(lambda l: l + [2, 3])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment