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

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

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

said on

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
said on

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

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

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

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

said on

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

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

said on

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

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

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

MenTaLguY: “Array#flatten being too agressive”

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

said on

The page you link to appears to be blank.

said on

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

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

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

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

said on

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

said on

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

Will make the Javascript more IE-friendly later.

said on

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

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

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

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

said on

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

Comments are closed for this entry.