background preloader

Monads

Facebook Twitter

More Monads on the Cheap: InLined from maybe. This article is about how to deal with null values.

More Monads on the Cheap: InLined from maybe

It follows up on this one. It’s intended for code stylists: people who care a lot about the difference between one line of code and two, or keeping control statements and temporary variables to a minimum. (A code stylist is kind of like the dual of a software architect, although one person can be both.) It’s not about code golf – although you might learn some strokes to use on that – but about keeping the structure of your code, even at the expression level, close to the way you think about the problem, if you think like me. If you’re not a code stylist – and I’m not saying that being a code stylist, any more than being a prose stylist, is either a good or a bad thing – you might find it baffling that someone would put so much time into such simple topic.

A nullable or optional, type is one that might have a value of a certain basis type, but might be null. The First Problem Set. The Maybe Monad in Ruby. | [tests] I’m a little behind on my goal to learn a new programming language each year – I’m still working on really understanding last year’s language, Haskell.

The Maybe Monad in Ruby

Judging from the number of tutorials online, understanding monads is one of the hardest parts of learning Haskell. I won’t even try to explain monads to you, because (as this post will reveal), I’m still a bit shaky on them myself. However, I will say my attempts to build a Maybe monad in Ruby have helped me learn more about Monads – and have yielded a pretty useful piece of code. Why should you care about the Maybe monad? Have you ever written code like this? If(customer && customer.order && customer.order.id==newest_customer_id) # ... do something with customer end Don’t you wish there was just a way to call customer.order.id without all those intermediate checks for nil objects?

If(Maybe.new(customer).order.id.value==newest_customer_id) # ... do something with customer end How does this work? Like this: Like Loading... Monads in Ruby (with nice syntax!) My last article was about how you can do monads in python with nice syntax.

Monads in Ruby (with nice syntax!)

Now I'd like to present nice monad syntax in Ruby. I'm not explaining what monads are or how you can use them. For now, I'm expecting you to be familiar with monads. If you aren't, go read a nice article on them. Imagine you have a Monad base class like this: class Monad def bind(func) raise "not implemented" end def self.unit(val) raise "not implemented" end # bind to block def bindb(&func) bind(func) end end As an aside, Please excuse there being a "bind" and a "bindb". Let's implement the Either monad, which I call it Failable: class Failable < Monad def initialize(value, success) @value = value @success = success end def bind(bindee) if @success bindee.call(@value) else self end end def self.unit(val) self.new(val, true) end def to_s if @success "Success(#{@value})" else "Failure(#{@value})" end end end def success(val) Failable.new(val, true) end def failure(val) Failable.new(val, false) end Which prints:

The Theory of Monads and the Monad Laws. As promised, I’m finally going to get to the theory behind monads.

The Theory of Monads and the Monad Laws

As a quick review, the basic idea of the monad in Haskell is a hidden transition function – a monad is, basically, a state transition function. The theory of monads comes from category theory. I’m going to assume you know a little bit about category theory – if you have trouble with it, go take a look at my introductory posts here. Fundamentally, in category theory a monad is a category with a particular kind of structure. It’s a category with one object. The first trick to the monad, in terms of theory, is that it’sfundamentally about the functor: since the functor maps from a category tothe same category, so you can almost ignore the category; it’s implicit in the definition of the functor.

The other big trick is closely related to that. Let’s take a moment and get formal. Given a category, C, 1C is the identity functor from C to C. Now, with that out of the way, we can give the complete formal definition of a monad.