Skip to content

Instantly share code, notes, and snippets.

@loicdescotte
Last active November 12, 2025 15:54
Show Gist options
  • Select an option

  • Save loicdescotte/4044169 to your computer and use it in GitHub Desktop.

Select an option

Save loicdescotte/4044169 to your computer and use it in GitHub Desktop.
Scala for comprehension translation helper

Scala for comprehension translation helper

"For comprehension" is a another syntaxe to use map, flatMap and withFilter (or filter) methods.

yield keyword is used to aggregate values in the resulting structure.

This composition can be used on any type implementing this methods, like List, Option, Try, Future...

Case 1

case class Book(author: String, title: String)
for {
  book <- books
  if book.author startsWith "bob" 
} yield book.title

is the same as

books.withFilter(book => book.author startsWith "bob").map(book => book.title)

Case 2

case class Book(authors: List[String], title: String)
for {
  book <- books
  author <- book.authors 
  if author startsWith "bob" 
} yield book.title

is the same as

books.flatMap(book => book.authors.withFilter(author => author startsWith "bob").map(author => book.title))

Case 3

val optA : Option[String] = Some("a value")
val optB : Option[String] = Some("b value")

I want an option tuple : Option[(String, String) composing this two options, i.e. Some(("a value","b value")) :

for {
     a <- optA
     b <- optB
} yield (a,b)

is the same as

optA.flatMap(a => optB.map(b => (a, b)))

You can also filter options with if/withFilter :

for {
  a <- optA
  if(a startsWith "a")
  b <- optB
} yield (a,b)

Or

optA.withFilter(a=> a startsWith "a").flatMap(a => optB.map(b => (a, b)))

If you change "a" to "c" in the condition, the resulting value will be None instead of Some(("a value","b value")).

Case 4

You can mix options and list types in map/flatMap (Option can be seen as a simple collection) :

val optNumbers = List(Some(1), Some(2), None, Some(3))

We want to remove empty values and increment other values : List(2, 3, 4) :

for {
  optNumber <- optNumbers
  value <- optNumber
} yield value +1

is the same as

optNumbers.flatMap(optNumber => optNumber.map(value => value+1))
@samklr

samklr commented Nov 9, 2012

Copy link
Copy Markdown

Beautiful Monadic pattern.

@Supryhan

Copy link
Copy Markdown

Awesome!

@svitkovsergey

Copy link
Copy Markdown

So good

@LeonisX

LeonisX commented Nov 29, 2018

Copy link
Copy Markdown

Nice

@cgntnr

cgntnr commented Nov 7, 2019

Copy link
Copy Markdown

This was very helpful, many thanks

@tberkane

tberkane commented Nov 8, 2019

Copy link
Copy Markdown

Very helpful before scala exam, thank you!

@dboreham

dboreham commented Apr 8, 2021

Copy link
Copy Markdown

Thank the Maker

@Moverr

Moverr commented Oct 1, 2021

Copy link
Copy Markdown

Great, howeer. take forexample

val result  = for {
  optNumber <- optNumbers
  value <- optNumber
} yield value +1

how are we able to get the result value while using

optNumbers.flatMap(optNumber => optNumber.map(value => value+1))
I will be glad if u can help my doubhts

@TDLiShiXiao

Copy link
Copy Markdown

Chinese : 牛逼

@AveryZhang66

Copy link
Copy Markdown

666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment