MenTaL on Monads in Ruby #
Hey, there’s more Ruby to talk about. Can you believe it? Our close friend MenTaLguY has started up a serialized article on Monads in Ruby. This is such a great way to approach the topic. You get to see an unfamiliar construct in a comfortable language. And MenTaL may unearth some new hacks along the way. Lots going on at his blog presently. Anyone have practical examples of this?
Sam
if someone wants to make a maybe monad, I’m sure it could be wrapped up really nicely with a ruby-MonadPlus and you guys would have a neat little “chain together computations that could fail” idiom in ruby.
Of course you could just write it in Haskell :-P
riffraff
ah! now I understand all this raskelltalk jigging, haskellers are trying to subvert the ruby folks from within!
weeksie
Funny, I’ve been making a somewhat serious attempt to get into Haskell over the last couple of weeks and this is a really nice explanation of monadic programming for me (coming from a ruby background).
Thanks MenTaL :)
chris2
I used monads to play with parser combinators…
crzwdjk
UNIX pipes are monads too! Bet you didn’t know you were using monads all this time. Bind is | and return is cat. Or something like that. My cat is a monad too, but don’t tell him, he might get scared.
MenTaLguY
Aaa! So THIS is where all those visitors have been coming from!
I guess I’d better hurry up and write the remaining chapters!
MenTaLguY
Sam: A Ruby Maybe Monad will be part 4 (“Maybe a Monad”), set in the mode of Gilbert and Sullivan.
(Part 3, “Monad, Array’d in thy Finest” will consist of making a monad out of plain Array, illustrated with selected works of the forgotten 19th century poet Henry William Bacon Woolworth)
But, just for you, here is a Ruby Maybe Monad, momentarily… (here’s a Ruby Maybe Monad, momentarily!):
(No MonadPlus operations though—exercise for the reader…)
<|:{
I used monads to search the 7 seas for new cancer cures!
Danno
You guys are totally focusing on the wrong thing here!
LAZYNESS ! That’s the top story on Mental’s blog!
(fine, monads are cool too, but lazyness people, let’s focus on what we all really are)
Chris
Inspired by MentalGuy I finally learned a (very) little about Monads and wrote up a long article on my own implementation
MenTaLguY
Chris: Very good!
Two caveats: you have to be careful about replacing the existing (unrelated)
Array#join
, andArray#flatten
being too agressive. You really only want one level of flattening…n00b
At least in these teaching examples, monads seem a little like just chaining method calls together:
someObj.foo.bar.baz
What are the advantages of monads over plain chaining of methods? Is it that you can chain in brand new functions defined on the spot?
MenTaLguY
noob: well, when we get to more interesting monads, the advantage is that the particular monad determines what chaining the method calls together means.
With monads, you can represent all kinds of very interesting and complex computations simply by chaining simple combinators (functions) together.
timsuth
MenTaLguY: “Array#flatten being too agressive”
I often use
foo.sum([])
. Inefficient but fun.Daniel Berger
The page you link to appears to be blank.
Tsela
Daniel: it is blank in IE and Opera, but shows up correctly in FireFox (at least for me). I wonder why too. MenTaLguY, what kind of non-standard things do you do? I can understand that crippled IE doesn’t work, but standards-compliant Opera?
hgs
I still haven’t got it yet. Is a command pattern a monad with a pre-declared block? Do they fit other design patterns?
MenTaLguY
Argh, WTF ? I tested in IE a while back; I wonder what changed?
The page passes HTML and CSS validation (with the exception of -moz-border-radius applied to the comment buttons). That should not prevent the page from rendering, however.
Some Javascript thing with the collapsable comment fields? Does it show up OK if you turn off Javascript?
Tsela
MenTal, have you changed something? Now it works in Opera. Don’t know about IE, I’m at home.
Tsela
Now that I’m at work, I can tell you it works on IE too. You must really have changed something…
MenTaLguY
Yeah, apparently Opera and IE don’t like minimized closure in XHTML tags.
Will make the Javascript more IE-friendly later.
MenTaLguY
I’ve uploaded an early draft of Part 3.
It’s missing a lot of flavor and explanation (and maybe I need to pick a different example), but it’s a beginning. Comments welcome, such as it is.
hgs
This is clearer now. I can now see that the monad can modify itself in the case where pass is not doing simply wrap(value).
The maze solver: nice idea. Should it not check start before checking the others and short-circuit if that it is true? Or do you mean it to find all exits, rather than just one.
Looking forward to the next installment
Merc
What would really help is a good, pseudocode (or metaphor) example of why Monads are useful. So far, they just seem like extra functions that don’t add much.
MenTaLguY
hgs: Yes, the idea’s to find all the exits.
Ralph
http://redhanded.hobix.com/inspect/mentalOnMonadsInRuby.html
Comments are closed for this entry.