Skip to content
0:00 / 0:00

#scala#option#monad

LambdaConf 2015 - Idiomatic Scala Your Options Do Not Match Marconi Lanna

Confreaks

42K subscribers • 85 videos on Hallway • Conference channel

YouTube
  • Recommended by 2 people: 2 in engineering communities.
  • Presented by Marconi Lanna
  • An opinion. It argues one side of a question engineers disagree on.

Intermediate. Assumes working knowledge.

2.7K views10 years ago2 recommendations

Programming languagesSoftware designScalaFunctional programmingDesign patterns

Marconi Lanna explains how to use the Option monad in Scala using higher-order functions instead of pattern matching.

The Scaladoc page for Option says: "The most idiomatic way to use a scala.Option instance is to treat it as a collection or monad and use map, flatMap, filter, or foreach [...] A less-idiomatic way to use scala.Option values is via pattern matching." In this talk we show how to use the Option monad in an idiomatic, functional style, leveraging its comprehensive set of higher-order functions to solve any coding situation in a concise and declarative manner. After this talk, you will never have to pattern match against Options again! Help us caption & translate this video! amara.org/v/HbvW/

Why it's here

2 people recommend this

What people say. Recommended as a great guide on when to use map, getOrElse, or fold instead of pattern matching on Scala Options.

u/msimavin r/scala • Recommends

On “Fold or pattern matching with an Option?”

You should watch Marconi Lanna's talk on this topic. Here is link

u/nmarshall23in r/scala • Recommends

On “Fold or pattern matching with an Option?”

Your match is too simplistic, I wouldn't use it unless you have more conditions. However as far as when to use `map/getOrElse` or `fold` it depends on who will be reading that code in 6 months. For someone who is new to functional programming I'd go with `map/getOrElse`. The intentional of that code is clearer to those you are unfamiliar with monads. After watching Marconi Lanna's talk, that msimav posted. I have to revise my opinion. In light of that talk, before I use pattern matching I would ask myself if what I am doing is better expressed with one of the higher-order functions.

u/relativerin r/scala • Mentions

On “Fold or pattern matching with an Option?”

The operation in this case is rather small, but I'd agree it's not a great strategy to do big stuff inside the fold itself as you pointed out. But to be fair, something larger would probably be done: val defaultVal = { .......... } val funcToApply = (x) => { ...... } val cow = foo.bar.baz.fold(defaultVal)(funcToApply) Instead of defining everything in place like that example, at least that's how I'd do it and how I hope everyone coding something I'd have to read would too. Still I agree that there's nothing inherently wrong with pattern matching besides being more verbose and making you have to refactor more code in very specific circumstances(as pointed out by this talk ms…