Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flatten Vs flatMap with def method and val function

flatten Vs flatMap with def method and val function:

I defined a def method called toInt:

  def toInt(s: String): Option[Int] = {
    try {
      Some(Integer.parseInt(s.trim))
    } catch {
      case e: Exception => None
    }
  }

And this method works fine with both flatten and flatMap as follows:

//using toInt method
val x = 1.to(5).toList
val y = List("a")
val z = x ++ y
val q = z.map(_.toString)

//using map and flatten
println(q.map(toInt).flatten)
//using flatMap
println(q.flatMap(toInt))

Now I defined the same toInt functionality(as in def method) using val in a function "tooInt":

val tooInt: String => Option[Int] = s => {
  try {
    Some(Integer.parseInt(s.trim))
  } catch {
    case c: Exception => None
  }
}

This works fine with flatten but NOT with flatMap as shown below:

//using map and flatten
 println(q.map(tooInt).flatten)
 //using flatMap // this has error
 **println(q.flatMap(tooInt))**

Could you please help me in understanding this?

Best Regards, Kiran

like image 233
Kiran Avatar asked Apr 23 '17 07:04

Kiran


1 Answers

You need to help the compiler a little with expansion to make this work:

q.flatMap(s => tooInt(s))

It all boils down to the fact that we have an implicit option2Iterable defined in Option.scala. When you first map and then flatten, the Option[Int] is already in scope and the implicit can be applied. But when you flatMap, the compiler has to first expand tooInt to s => tooInt(s) and then apply the implicit resolution, but that doesn't work. Why doesn't it work? because the compiler looks for an implicit of type:

pt=(=> String => Option[Int]) => (String => scala.collection.GenTraversableOnce[?])

Which doesn't exist. On the contrary, the toInt method is first expanded into a function type, and then the implicit is searched for Option[Int]:

-- toInt : pt=String => scala.collection.GenTraversableOnce[?] BYVALmode-EXPRmode-POLYmode (site: value r in X)
|    |    |    |    |    |    |-- { ((s: String) => toInt(s)) } : pt=String => scala.collection.GenTraversableOnce[?] BYVALmode-EXPRmode-POLYmode (site: value r in X)
|    |    |    |    |    |    |    |-- ((s: String) => toInt(s)) : pt=String => scala.collection.GenTraversableOnce[?] BYVALmode-EXPRmode-POLYmode (site: value r in X)
|    |    |    |    |    |    |    |    |-- (s: String)Option[Int] : pt=scala.collection.GenTraversableOnce[?] EXPRmode (site: value $anonfun in X)
|    |    |    |    |    |    |    |    |    |-- s : pt=String BYVALmode-EXPRmode (site: value $anonfun in X)
|    |    |    |    |    |    |    |    |    |    \-> String
|    |    |    |    |    |    |    |    |    [search #3] start `(s: String)Option[Int]`, searching for adaptation to pt=Option[Int] => scala.collection.GenTraversableOnce[?] (silent: value $anonfun in X) implicits disabled
|    |    |    |    |    |    |    |    |    [search #3] considering scala.this.Option.option2Iterable

We can also see it in the decompiled code:

val r: scala.collection.immutable.IndexedSeq[Int] = q.flatMap[Int, scala.collection.immutable.IndexedSeq[Int]]({
    {
      final <artifact> def $anonfun$main(s: String): Iterable[Int] = scala.this.Option.option2Iterable[Int](toInt(s));
      ((s: String) => $anonfun$main(s))
    }
  }, immutable.this.IndexedSeq.canBuildFrom[Int]());
like image 92
Yuval Itzchakov Avatar answered Nov 20 '22 07:11

Yuval Itzchakov