# "For Comprehensions" - why "for"?

**URL:** <https://users.scala-lang.org/t/for-comprehensions-why-for/6797>\
**Category:** Question\
**Created:** [October 30, 2020, 9:22pm UTC](https://users.scala-lang.org/t/for-comprehensions-why-for/6797 "2020-10-30T21:22:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubaut](https://users.scala-lang.org/user_avatar/users.scala-lang.org/dubaut/32/1152_2.png) [@dubaut](https://users.scala-lang.org/u/dubaut)\
**Post date:** [October 30, 2020, 9:22pm UTC](https://users.scala-lang.org/t/for-comprehensions-why-for/6797/1 "2020-10-30T21:22:14Z")

</div>

As far as I understand it, _For Comprehensions_ are a way to write a chain of `flatMap` calls. So far so good, that I understand to a certain extend.

That leads me to my question: Why is it called _For Comprehension_, why much the decision on “for”? Where is the connection between `flatMap` and `for`, where for is as well used for iterating and looping?

Maybe, if I understand this I can better understand the anatomy of _For Comprehensions_.

---

<div class="post-metadata">

**Author:** ![BalmungSan](https://users.scala-lang.org/user_avatar/users.scala-lang.org/balmungsan/32/1700_2.png) [@BalmungSan](https://users.scala-lang.org/u/BalmungSan)\
**Post date:** [October 30, 2020, 9:33pm UTC](https://users.scala-lang.org/t/for-comprehensions-why-for/6797/2 "2020-10-30T21:33:50Z")

</div>

AFAIK just because for collections `map` and `flatMap` represent _(nested)_ iterations, so it is like a _for each loop_.

So if you have something like:

```auto
val userCountries = for {
  user <- users
  address <- user.addresses
} yield address.country

```

You can read it like:  
_“For each user in users and each address on the user addresses, then collect all the addresses countries into the variable userCountries”_.

Now, when you look at other effects like **Option** , **Either** , **Future** , **IO** , etc, while the idea of _“iteration”_ is weirder, the wording still makes sense.

```auto
def process: Either[Error, String] = ???

val result = for {
  rawData <- process
  cleanData <- rawData.toIntOption.toRight(left = "Bad number")
} yield cleanData

```

You can read it like:  
_“For some raw data coming from the process, and for a cleaned data extracted from the raw data, collect the cleaned data on the result variable”_.  
So the wording is weird but still somewhat clear.

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [October 30, 2020, 10:06pm UTC](https://users.scala-lang.org/t/for-comprehensions-why-for/6797/3 "2020-10-30T22:06:52Z")

</div>

Yeah, exactly. In no small part, it makes it _way_ easier for folks who are coming in from other languages.

I generally start out by teaching them that it’s more or less what they understand as a typical for-loop (in particular, many languages now have a concept of “loop over each element in this collection”), and then later ease them into The Joy of Monads.

---

<div class="post-metadata">

**Author:** ![karolwolonciej](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/k/49beb7/32.png) [@karolwolonciej](https://users.scala-lang.org/u/karolwolonciej)\
**Post date:** [October 30, 2020, 11:38pm UTC](https://users.scala-lang.org/t/for-comprehensions-why-for/6797/4 "2020-10-30T23:38:25Z")

</div>

You can read about translating for to map, flatmap and filter in depth in programming in scala 4 edition

---

<div class="post-metadata">

**Author:** ![crater2150](https://users.scala-lang.org/user_avatar/users.scala-lang.org/crater2150/32/1011_2.png) [@crater2150](https://users.scala-lang.org/u/crater2150)\
**Post date:** [November 2, 2020, 12:47pm UTC](https://users.scala-lang.org/t/for-comprehensions-why-for/6797/5 "2020-11-02T12:47:46Z")

</div>

Also remember, that Scala allows impure code and there are also people using it in a more imperative way. If you use `for` without `yield`, it isn’t translated to `flatMap` and `map` calls, but to `foreach` calls. For people coming from imperative languages, this means they can use `for` like a for-each-loop in other languages, and then just add a `yield` to get a “for loop which returns a new collection”.
