Scala: flatMap and for-comprehension

object MyClass {
def twice(x: Int) = Some(x * 2)

def thrice(x: Int) = Some(x * 3)

def quadruple(x: Int) = Some(x * 4)
def main(args: Array[String]) {
List(1,2,3)
.flatMap(twice)
.flatMap(thrice)
.flatMap(quadruple)
.foreach(println)
}
24
48
72
object MyClass {
def twice(x: Int) = Some(x * 2)

def thrice(x: Int) = Some(x * 3)

def quadruple(x: Int) = Some(x * 4)
def main(args: Array[String]) {
List(1,2,3)
.flatMap(twice)
.flatMap(thrice)
.flatMap(quadruple)
.foreach(println)

println("For comprehension")

(for {
n <- List(1,2,3)
t <- twice(n)
th <- thrice(t)
q <- quadruple(th)
} yield q).foreach(println)
}
}
24
48
72
For comprehension
24
48
72
object MyClass {

case class Apple(s:String) {
override def toString() = s
}

def twice(x: Apple) = Some(Apple(x.toString + "-twice"))

def thrice(x: Apple) = Some(Apple(x.toString + "-thrice"))

def quadruple(x: Apple) = Some(Apple(x.toString + "-quadrupled"))
def main(args: Array[String]) {
List(Apple("1"),Apple("2"),Apple("3"))
.flatMap(twice)
.flatMap(thrice)
.flatMap(quadruple)
.foreach(println)

println("For comprehension")

(for {
n <- List(Apple("1"),Apple("2"),Apple("3"))
t <- twice(n)
th <- thrice(t)
q <- quadruple(th)
} yield q).foreach(println)
}
}
1-twice-thrice-quadrupled
2-twice-thrice-quadrupled
3-twice-thrice-quadrupled
For comprehension
1-twice-thrice-quadrupled
2-twice-thrice-quadrupled
3-twice-thrice-quadrupled
object MyClass {

case class Apple(s:String) {
override def toString() = s
}

case class Orange(s : String) {
override def toString() = s
}

def twice(x: Apple) = Some(Apple(x.toString + "-twice"))

def thrice(x: Apple) = Some(Apple(x.toString + "-thrice"))

def quadruple(x: Orange) = Some(Orange(x.toString + "-quadrupled"))
def main(args: Array[String]) {
List(Apple("1"),Apple("2"),Apple("3"))
.flatMap(twice)
.flatMap(thrice)
.flatMap(quadruple)
.foreach(println)
}
}
jdoodle.scala:22: error: type mismatch;
found : Main.Orange => Some[Main.Orange]
required: Main.Apple => scala.collection.IterableOnce[?]
.flatMap(quadruple) ^
object MyClass {

case class Apple(s:String) {
override def toString() = s
}

case class Orange(s : String) {
override def toString() = s
}

def twice(x: Apple) = Some(Apple(x.toString + "-twice"))

def thrice(x: Apple) = Some(Apple(x.toString + "-thrice"))

def quadruple(x: Orange) = Some(Orange(x.toString + "-quadrupled"))
def main(args: Array[String]) {
println("For comprehension")

(for {
n <- List(Apple("1"),Apple("2"),Apple("3"))
t <- twice(n)
th <- thrice(t)
q <- quadruple(th)
} yield q).foreach(println)
}
}
jdoodle.scala:25: error: type mismatch;
found : Main.Apple
required: Main.Orange
q <- quadruple(th) ^

--

--

--

On a continuing learning journey..

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Day 76 | 100 Days Of GADS

day 76 of my 100 days of code challenge

Load a XIB-Based View Controller

Be Okay with Problem-Free Dependenc ies

Generic networking layer using Combine in Swift UI.

Migrating an Existing Application

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Learn

Learn

On a continuing learning journey..

More from Medium

Scala: for-comprehension contexts do not mix

[Scala] Implicit Parameters

Polymorphic functions in Scala

Type Classes in Scala 2