Skip to content

Instantly share code, notes, and snippets.

@mastern2k3
Created August 12, 2018 13:53
Show Gist options
  • Save mastern2k3/350ae599586325c79c0b25aec5b2c39d to your computer and use it in GitHub Desktop.
Save mastern2k3/350ae599586325c79c0b25aec5b2c39d to your computer and use it in GitHub Desktop.
FP option category in python
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