background preloader

Monads

Facebook Twitter

Martians vs Monads: Null Considered Harmful. I hate null.

Martians vs Monads: Null Considered Harmful

More specifically I hate NullPointerExceptions(NPEs). Scala has an alternative, but let me explain why I hate NPEs. They're like evil, sneaky muppets. Remember the Martians on Sesame Street? They'd ask each other if a phone was a cow and then say "nope, nope, nope, nope. " Me: Is my unit test working? Does all this code/test/fix/Martians/yelling have to be part of the game? The answer in Java and C# is "No!

Some Craptacular Code First, what's the big deal? Public interface Spaceship { public Martian getMartian(); } Spaceship spaceship = getSpaceshipSomehow(); Martian m = spaceship.getMartian() if (m ! Public interface Spaceship { public Martian getMartian(); } // the main method... The idea here is that some spaceships have Martians while others don't. Okay, let's try creating a holder that I'll call "Option" for reasons we'll see later. I've got an abstract base class, Option, that's really just a marker that a value might be "null" or not. Making it Optional in Scala. Monads are Elephants Part 1. Introductions to monads are bit of cottage industry on the Internet.

Monads are Elephants Part 1

So I figured, "why buck tradition? " But this article will present Scala's way of dealing with monads. An ancient parable goes that several blind men were experiencing their first elephant. "It's a tree," one said while wrapping his arms around its legs. "A large snake," another said while holding its trunk. Monads are Elephants Part 2. In part 1, I introduced Scala's monads through the parable of the blind men and the elephant.

Monads are Elephants Part 2

Normally we're supposed to take the view that each blind man doesn't come close to understanding what an elephant is, but I presented the alternative view that if you heard all the blind men describe their experience then you'd quickly build a surprisingly good understanding of elephants. In part 2 I'm going to poke at the beast some more by exploring Scala's monad related syntactic sugar: "for comprehensions.

" A Little "For" A very simple "for" looks like this val ns = List(1, 2) val qs = for (n <- ns) yield n * 2 assert (qs == List(2, 4)) val ns = List(1, 2) val qs = for (n <- ns) yield n * 2 assert (qs == List(2, 4)) The "for" can be read as "for [each] n [in] ns yield n * 2. " val qs = ns map {n => n * 2} val qs = ns map {n => n * 2} The rule here is simple for (x <- expr) yield resultExpr for (x <- expr) yield resultExpr Expands to1 expr map {x => resultExpr} expr map {x => resultExpr} Monads are Elephants Part 3. In this series I've presented an alternative view on the old parable about the blind men and the elephant.

Monads are Elephants Part 3

In this view, listening to the limited explanations from each of the blind men eventually leads to a pretty good understanding of elephants. So far we've been looking at the outside of monads in Scala. That's taken us a great distance, but it's time to look inside. What really makes an elephant an elephant is its DNA. Monads have a common DNA of their own in the form of the monadic laws. This article is a lot to digest all at once. Equality for All Before I continue, I have to semi-formally explain what I mean when I use triple equals in these laws as in "f(x) ≡ g(x). " So I'm saying the expression on the left is "the same" as the expression on the right. The Scala Option class and how lift uses it - David Pollak's Blog. Monadologie. League's scala-type-anxiety at master - GitHub.