Skip to content

Instantly share code, notes, and snippets.

@rbtcollins
Last active August 29, 2015 14:23
Show Gist options
  • Save rbtcollins/f570a2dd4aa4c0ea0264 to your computer and use it in GitHub Desktop.
Save rbtcollins/f570a2dd4aa4c0ea0264 to your computer and use it in GitHub Desktop.
Python Monad notes
PyMonad
https://pypi.python.org/pypi/PyMonad/
Doesn't duck type Writer - subclass
class StringWriter(Writer):
logType = str
PyMonads
https://github.com/dustingetz/pymonads
Doesn't duck type Writer:
https://github.com/dustingetz/pymonads/blob/master/writer.py#L6
OSlash
https://pypi.python.org/pypi/OSlash/0.5.1
Same issue with writer https://github.com/dbrattli/OSlash/blob/master/oslash/writer.py#L89
fn
https://pypi.python.org/pypi/fn/0.4.3
No writer at all.
Defines some nice common helper fns rather than needing lots-o-lambdas.
monad
https://bitbucket.org/pyx/monad/
https://pypi.python.org/pypi/monad/0.1
Looks nice and pithy
No writer at all.
Example duck-typing Writer:
from collections import namedtuple
class Writer(namedtuple("Writer", "value log")):
@classmethod
def pure(cls, value):
return Writer(value, None)
def bind(self, fn):
newvalue, newlog = fn(self.value)
if self.log is None:
resultlog = newlog
else:
resultlog = self.log + newlog
return Writer(newvalue, resultlog)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment