为什么flatMap在映射器函数结果不是Vector [Option [Int]]有效的Vector [Option [Int]]上? [英] Why is flatMap on a Vector[Option[Int]] whose mapper function result is not a Vector[Option[Int]] valid?

查看:103
本文介绍了为什么flatMap在映射器函数结果不是Vector [Option [Int]]有效的Vector [Option [Int]]上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,

 向量(一些(1),一些(2),一些(3),无)。 flatMap {
n => n
}

会生成一个 Vector(1,2,3 )而不是给出错误。正如我在其他语言中看到的,当你有一个产生嵌套的mapper函数时,会使用 flatMap ,所以我认为这是一个有效的 flatMap

 向量(1,2,3).flatMap {
eachNum => Vector(eachNum)
}

我的mapper函数产生一个 Vector 这会导致嵌套(即 Vector(Vector(1),Vector(2),Vector(3),Vector(4)))if由于容器包装,我使用了 map 。但是, flatMap 会删除这个嵌套并将其压扁。这是有意义的,当有两个相同的monads嵌套。然而,我不明白如何在映射函数中使用 flatMap 来返回选项变成一个 Vector [Int] Vector [选项[Int]] 。是否有某种转变正在进行(我从未见过这种情况),有人可以解释,也可能指向我一些资源吗?



非常感谢

解决方案

我们可以使用 reify 来查看发生了什么:

  scala> import reflect.runtime.universe._ 
import reflect.runtime.universe._

scala> val v = Vector(一些(1),一些(2),一些(3),无)
v:scala.collection.immutable.Vector [Option [Int]] = Vector(一些(1),一些2),一些(3),无)

scala> reify {v.flatMap(x => x)}
res0:reflect.runtime.universe.Expr [scala.collection.immutable.Vector [Int]] =
Expr [scala.collection.immutable .Vector [Int]]($ read.v.flatMap(((x)=>
Option.option2Iterable(x)))(Vector.canBuildFrom))

这表明它使用option2Iterable转换将 Option 转换为 Iterable Iterable GenTraversableOnce 类型的子类型,flatMap是期待。


For example,

Vector(Some(1), Some(2), Some(3), None).flatMap{
  n => n
}

produces a Vector(1, 2, 3) instead of giving an error. As I have seen in other languages, flatMap is used when you have a mapper function that produces nesting so I would expect this to be a valid flatMap:

Vector(1, 2, 3).flatMap{
  eachNum => Vector(eachNum)
}

My mapper function produces a Vector which would cause nesting (i.e. Vector(Vector(1), Vector(2), Vector(3), Vector(4))) if I used a map due to the container wrapping. However, flatMap will remove this nesting and flatten it. This makes sense when there is nesting of two identical monads.

However, I do not understand how using a flatMap with a mapper function that returns an Option makes a Vector[Option[Int]] become a Vector[Int]. Is there some sort of transformation going on (I have never seen this before), could someone explain and perhaps point me to some resources?

Thank you very much

解决方案

we can use reify to see what is going on:

scala> import reflect.runtime.universe._
import reflect.runtime.universe._

scala> val v = Vector(Some(1), Some(2), Some(3), None)
v: scala.collection.immutable.Vector[Option[Int]] = Vector(Some(1), Some(2), Some(3), None)

scala> reify { v.flatMap(x => x) }
res0: reflect.runtime.universe.Expr[scala.collection.immutable.Vector[Int]] =
    Expr[scala.collection.immutable.Vector[Int]]($read.v.flatMap(((x) =>
     Option.option2Iterable(x)))(Vector.canBuildFrom))

This is showing us that it is using the option2Iterable conversion to convert the Option to Iterable, and Iterable is a subtype of the GenTraversableOnce type that flatMap is expecting.

这篇关于为什么flatMap在映射器函数结果不是Vector [Option [Int]]有效的Vector [Option [Int]]上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆