flatMap 忽略结果
flatMap ignoring the result
我想知道是否存在一个函数(在 scala 或 cats 中)省略了 flatMap
中的结果。例如
Some("ignore this").ignoreArgumentFlatMap(Some("result"))
与
相同
Some("ignore this").flatMap(_ => Some("result"))
它在猫中被称为>>
。
scala> import cats.implicits._
import cats.implicits._
scala> Option("ignore this") >> Some("result")
res14: Option[String] = Some(result)
文档明确说明
Alias for fa.flatMap(_ => fb).
Unlike *>, fb is defined as a by-name parameter, allowing this method to be used in cases where computing fb is not stack safe unless suspended in a flatMap.
还有 productR
或 *>
。
scala> Option("ignore this").productR(Some("result"))
res15: Option[String] = Some(result)
scala> Option("ignore this") *> Some("result")
res16: Option[String] = Some(result)
就像文档所说的那样,它的论点不是名字。所以它或多或少等同于
val x0 = Some("result")
Some("ignore this").flatMap(_ => x0)
如果你想要一个替代的评估策略,productREval
。
我想知道是否存在一个函数(在 scala 或 cats 中)省略了 flatMap
中的结果。例如
Some("ignore this").ignoreArgumentFlatMap(Some("result"))
与
相同Some("ignore this").flatMap(_ => Some("result"))
它在猫中被称为>>
。
scala> import cats.implicits._
import cats.implicits._
scala> Option("ignore this") >> Some("result")
res14: Option[String] = Some(result)
文档明确说明
Alias for fa.flatMap(_ => fb).
Unlike *>, fb is defined as a by-name parameter, allowing this method to be used in cases where computing fb is not stack safe unless suspended in a flatMap.
还有 productR
或 *>
。
scala> Option("ignore this").productR(Some("result"))
res15: Option[String] = Some(result)
scala> Option("ignore this") *> Some("result")
res16: Option[String] = Some(result)
就像文档所说的那样,它的论点不是名字。所以它或多或少等同于
val x0 = Some("result")
Some("ignore this").flatMap(_ => x0)
如果你想要一个替代的评估策略,productREval
。