V
Valued Lessons
valuedlessons.com · Personal blog · on HN since 2008
A programming blog from 2008 with Python tutorials on monads, parsing, reactive programming and memory usage.
What HN saysHN commenters recommend its monads-in-Python post as a favorite monad tutorial because it avoids Haskell, and point to the Pysec parsing follow-up.
19HN people shared it
9linked it in comments
10stories submitted
182story points
7 years agolast shared
2008people per year2026
Most-shared posts
- 1Monads in Python (with nice syntax)11 people · 141 points · first shared 2008 · 48 comments on HN
- 2Python memory usage of common types2 people · 32 points · first shared 2009 · 3 comments on HN
- 3Pysec monadic combinatoric parsing in2 people · first shared 2010
- 4Garlic Programmers for Silver Code? How your code base affects productivity1 people · 6 points · first shared 2008 · 1 comments on HN
- 5Simple Reactive Programming with Python1 people · 2 points · first shared 2012 · discussion on HN
- 6List Monad in Ruby and Python1 people · 1 points · first shared 2010 · discussion on HN
- 7Sql is now turing complete1 people · first shared 2015
- 8Introducing pyrec cure to bane of init1 people · first shared 2011
What HN people wrote when they linked it
a3n · 2015-09-10
"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…
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.
codygman · 2014-07-18
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…
2: https://pypi.python.org/pypi/PyMonad/
gxti · 2010-07-20
The post is from 2008, and this seems to be the follow-up: http://www.valuedlessons.com/2008/02/pysec-monadic-combinatoric-parsin…
pmiller2 · 2009-11-24
Here is my favorite monad tutorial: http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax… . 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
yablak · 2017-07-09
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-parsin…
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
fizixer · 2016-01-20
> 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…
[3] http://www.dustingetz.com/2012/04/07/dustins-awesome-monad-tutorial-fo…
hobs · 2015-06-17
Also SQL variants are turing complete http://www.valuedlessons.com/2009/08/sql-is-now-turing-complete.html
pthatcherg · 2011-11-26
That's funny. I implemented almost the exact same thing a few years ago, although mine is arguably more complete :)
https://github.com/pthatcher/pyrec
Here's my original post about it:
http://www.valuedlessons.com/2009/10/introducing-pyrec-cure-to-bane-of…
And here's my analysis of how much less memory it uses:
http://www.valuedlessons.com/2008/10/blog-post.html
jmillikin · 2010-02-23
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'…