A programming blog from 2008 with Python tutorials on monads, parsing, reactive programming and memory usage.
HN commenters recommend its monads-in-Python post as a favorite monad tutorial because it avoids Haskell, and point to the Pysec parsing follow-up.
C and Java can't represent monads at all, since they don't support closures.
C# and Python can, sort of, but their syntax and object-oriented semantics make it somewhat difficult. I don't know C# well, and Python needs a lot of magic[1] to get reasonable-looking code.
Here's a Python example. It's not a direct translation, but is close enough that you should be able to figure out the original. I've included the Haskell code in comments.
import abc
# class Monad m where
# build :: a -> m a
# bind :: m a -> (a -> m b) -> m b
class Monad (metaclass = abc.ABCMeta):
@classmethod
def build (cls, x):
raise NotImplementedError
def bind (self, f):
raise NotImplementedError
# data Maybe a = Just a | Nothing
# deriving (Show)
class Maybe (Monad):
def __init__ (self, x):
self.__x = x
def __repr__ (self):
if self is Nothing:
return "Nothing"
return ("Just %r" % self.__x)
# instance Monad Maybe where
# build x = Just x
#
# bind Nothing _ = Nothing
# bind (Just x) f = f x
@classmethod
def build (cls, x):
return Just (x)
def bind (self, f):
if self is Nothing:
return Nothing
return f (self.__x)
def Just (x):
return Maybe (x)
Nothing = Maybe (None)
# clamp limit x = if x > limit then Nothing else Just x
def clamp (limit, x):
if x > limit:
return Nothing
return Just (x)
# multiclamp2 lim1 lim2 x = bind
# (clamp lim1 x)
# (\x'…
> Do I need to know Haskell? Yes.
This is enough for me to reject this blogpost.
No you don't need a specific language to implement any functionality that can be implemented by a turing-complete language.
I watched Crockford's monad talk [1]. He used javascript. The only problem with that talk is that he spent about 10 seconds or less explaining what a monad is (and the rest of the talk on implementation issues and his own perspective, etc, etc).
I'm still waiting for a good monad discussion without CT or Haskell. (this [2] and this [3] look promising, both in python)
[1] https://www.youtube.com/watch?v=b0EF0VTs9Dc
[2] http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html#6799472260664409462
[3] http://www.dustingetz.com/2012/04/07/dustins-awesome-monad-tutorial-for-humans-in-python.html
"It's a pattern."
"All useful languages have monads ... nearly impossible to avoid."
So here's a post on Python (hopefully useful) and monads, it appears to be implementing a pattern, although I don't think I would have ever stumbled across it naturally.
http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html
And here's a library implementing monads and other stuff:
https://pypi.python.org/pypi/PyMonad/
It still looks like I'm going to have to try using them before I can figure out if it's worth trying to use them.
Monads are useful and unknowingly created in other languages all the time [0].
If you aren't quite comfortable enough with monads to follow the first link, I found this implementing monads in python tutorial[1] to be a huge help.
For those that want to play with Monads in Python, check out PyMonad[2].
0: http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html
1: http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html
2: https://pypi.python.org/pypi/PyMonad/
A parser combinator may be more powerful? Not sure.
The python parsec package is really terribly documented, but there's a good example here:
http://www.valuedlessons.com/2008/02/pysec-monadic-combinatoric-parsing-in.html
EDIT:
The name of the package may have changed; it looks like this is the pypi package being documented (if not; i'm confused because pypi's pysec package is something totally different):
https://pypi.python.org/pypi/parsec
Here is my favorite monad tutorial: http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html . The reason I like it is because it uses Python rather than Haskell while pointing out that you can build monads in any language that supports objects or closures.
Actually, now that I think about it, it seems to me that pythonic monads should really be context managers, so they can be used like:
with SomeMonad:
do stuff