12

=次のように、scala for-comprehension ( SLS のセクション6.19で指定) でan を使用できます。

オプション

いくつかの機能があるとしますString => Option[Int]:

scala> def intOpt(s: String) = try { Some(s.toInt) } catch { case _ => None }
intOpt: (s: String)Option[Int]

それから私はそれを使うことができます

scala> for {
   |     str <- Option("1")
   |     i <- intOpt(str)
   |     val j = i + 10    //Note use of = in generator
   |   }
   |   yield j
res18: Option[Int] = Some(11)

これは本質的に次のものと同等であることが私の理解でした。

scala> Option("1") flatMap { str => intOpt(str) } map { i => i + 10 } map { j => j }
res19: Option[Int] = Some(11)

つまり、組み込みジェネレーターは、map一連のflatMap呼び出しに a を挿入する方法でした。ここまでは順調ですね。

どちらか.RightProjection

私が実際にやりたいこと:モナドを使用した前の例と同様の for-comprehension を使用しEitherます。

ただし、同様のチェーンで使用すると、今回はEither.RightProjectionモナド/ファンクターを使用すると機能しません。

scala> def intEither(s: String): Either[Throwable, Int] = 
  |      try { Right(s.toInt) } catch { case x => Left(x) }
intEither: (s: String)Either[Throwable,Int]

次に使用します。

scala> for {
 | str <- Option("1").toRight(new Throwable()).right
 | i <- intEither(str).right //note the "right" projection is used
 | val j = i + 10
 | }
 | yield j
<console>:17: error: value map is not a member of Product with Serializable with Either[java.lang.Throwable,(Int, Int)]
              i <- intEither(str).right
                ^

この問題は、右射影がそのflatMapメソッドへの引数として期待する関数 (つまり、 を期待するR => Either[L, R]) と関係があります。しかしright、2 番目のジェネレーターを呼び出さないように変更しても、コンパイルされません。

scala>  for {
 |        str <- Option("1").toRight(new Throwable()).right
 |        i <- intEither(str) // no "right" projection
 |          val j = i + 10
 |      }
 |      yield j
<console>:17: error: value map is not a member of Either[Throwable,Int]
              i <- intEither(str)
                            ^

メガコンフュージョン

しかし今、私は二重に混乱しています。以下は問題なく動作します。

scala> for {
 |       x <- Right[Throwable, String]("1").right
 |       y <- Right[Throwable, String](x).right //note the "right" here
 |     } yield y.toInt
res39: Either[Throwable,Int] = Right(1)

しかし、これはしません:

scala> Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right } map { y => y.toInt }
<console>:14: error: type mismatch;
 found   : Either.RightProjection[Throwable,String]
 required: Either[?,?]
              Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right } map { y => y.toInt }
                                                                                             ^

これらは同等だと思いました

  • 何が起こっている?
  • =を介して理解するために、ジェネレーターをに埋め込むにはどうすればよいEitherですか?
4

1 に答える 1

9

=を for-comprehension に埋め込むことができないという事実は、Jason Zaugg によって報告されたこの問題に関連しています。解決策は、右バイアスEither(またはそれに同型の新しいデータ型を作成する) です。

あなたの巨大な混乱のために、あなたは砂糖の を間違って展開しました。の脱糖

for {
  b <- x(a)
  c <- y(b)
} yield z(c)

x(a) flatMap { b =>
 y(b) map { c =>
  z(c) }} 

ではない

x(a) flatMap { b => y(b)} map { c => z(c) }

したがって、これを行う必要がありました:

scala> Right[Throwable, String]("1").right flatMap { x => Right[Throwable, String](x).right map { y => y.toInt } }
res49: Either[Throwable,Int] = Right(1)

脱糖の楽しみ方 (「j = i + 10」の問題)

for {
  b <- x(a)
  c <- y(b)
  x1 = f1(b)
  x2 = f2(b, x1)
  ...
  xn = fn(.....)
  d <- z(c, xn)
} yield w(d)

脱糖される

x(a) flatMap { b =>
  y(b) map { c =>
    x1 = ..
    ...
    xn = ..
    (c, x1, .., xn) 
  } flatMap { (_c1, _x1, .., _xn) =>
    z(_c1, _xn) map w }}

したがって、あなたの場合、定義されていないy(b)結果タイプがあります。Eithermap

于 2012-05-21T16:06:22.707 に答える