Map flatten and flatmap not equivalent

后端 未结 2 1811
野性不改
野性不改 2020-12-15 08:35

I thought that Scala construct map(f).flatten was equivalent to flatMap(f). But with this example, it is not the case. I wonder what is the role of the case class in it. If

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-15 08:56

    Take a look at implementation of flatMap:

    def flatMap[B, That](f: A => GenTraversableOnce[B])(implicit bf: CanBuildFrom[Repr, B, That]): That = {
      def builder = bf(repr) // extracted to keep method size under 35 bytes, so that it can be JIT-inlined
      val b = builder
      for (x <- this) b ++= f(x).seq
      b.result
    }
    

    Result of flatMap depends on original collection type and result type of function f. In first example you generate sequence of tuples from map, so that compiler picks implementation like CanBuildFrom[Map[A, B], (C, D), Map[C, D]], which provides builder for Map, that causes overwriting of same keys.

    You can directly convert map to plain iterable, that will yield result you want:

    case class CTest(v: Int)
    val s = Set(Map(CTest(0) -> List(0, 3), CTest(1) -> List(0, 2)))
    val possibilities = s flatMap { m =>
      val mapping = m.toIterable.flatMap {
        case (label, destNodes) => destNodes map {
          case nodes => (label, nodes) }
      }
      mapping
    }
    possibilities
    

提交回复
热议问题