hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

MenTaL on Monads in Ruby #

by why in inspect

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?

said on 07 Nov 2005 at 01:54

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

said on 07 Nov 2005 at 03:22

ah! now I understand all this raskelltalk jigging, haskellers are trying to subvert the ruby folks from within!

said on 07 Nov 2005 at 06:45

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 :)

said on 07 Nov 2005 at 07:51
said on 07 Nov 2005 at 08:27

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.

said on 07 Nov 2005 at 10:14

Aaa! So THIS is where all those visitors have been coming from!

I guess I’d better hurry up and write the remaining chapters!

said on 07 Nov 2005 at 10:35

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!):


require 'singleton'

class Maybe
  def Maybe.wrap( value )
    Just::new( value )
  end
end

class Just < Maybe
  attr_reader :value
  def initialize( value )
    @value = value
  end
  def pass ; yield @value ; end
  def empty? ; false ; end
  def maybe( default_value ) ; @value ; end
end

class Nothing < Maybe
  include Singleton
  def pass ; self ; end
  def empty? ; true ; end
  def maybe( default_value ) ; default_value ; end
end

(No MonadPlus operations though—exercise for the reader…)

said on 07 Nov 2005 at 12:27

I used monads to search the 7 seas for new cancer cures!

said on 07 Nov 2005 at 12:59

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)

said on 07 Nov 2005 at 14:48

Inspired by MentalGuy I finally learned a (very) little about Monads and wrote up a long article on my own implementation

said on 07 Nov 2005 at 15:46

Chris: Very good!

Two caveats: you have to be careful about replacing the existing (unrelated) Array#join, and Array#flatten being too agressive. You really only want one level of flattening…

said on 07 Nov 2005 at 19:09

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?

said on 07 Nov 2005 at 20:51

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.

said on 07 Nov 2005 at 21:30

MenTaLguY: “Array#flatten being too agressive”

I often use foo.sum([]). Inefficient but fun.

said on 07 Nov 2005 at 23:22

The page you link to appears to be blank.

said on 08 Nov 2005 at 02:24

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?

said on 08 Nov 2005 at 06:21

I still haven’t got it yet. Is a command pattern a monad with a pre-declared block? Do they fit other design patterns?

said on 08 Nov 2005 at 08:15

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?

said on 08 Nov 2005 at 17:02

MenTal, have you changed something? Now it works in Opera. Don’t know about IE, I’m at home.

said on 09 Nov 2005 at 03:32

Now that I’m at work, I can tell you it works on IE too. You must really have changed something…

said on 10 Nov 2005 at 15:37

Yeah, apparently Opera and IE don’t like minimized closure in XHTML tags.

Will make the Javascript more IE-friendly later.

said on 12 Nov 2005 at 23:19

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.

said on 15 Nov 2005 at 07:15

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

said on 17 Nov 2005 at 08:31

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.

said on 17 Nov 2005 at 11:13

hgs: Yes, the idea’s to find all the exits.

said on 17 Jan 2006 at 21:17

http://redhanded.hobix.com/inspect/mentalOnMonadsInRuby.html

Comments are closed for this entry.