1

関数型プログラミングの Scala演習に引き続き取り組みfoldRightIndexedSeq型の実装に取り​​組んでいます。

foldRight は で評価されるright associativityため、パターン マッチング用に次の演算子を作成しました。

object :++ {
  def unapply[T](s: Seq[T]) = s.lastOption.map(last => 
                                                  (last, s.take(s.length - 1)))
}

そして、次のように実装します。

object IndexedSeqFoldable extends Foldable[IndexedSeq] {
  override def foldRight[A, B](as: IndexedSeq[A])(z: B)(f: (A, B) => B): B = {
    def go(bs: Seq[A], acc: B): B = bs match {
        case x :++ xs => go(xs, f(x, acc)) 
        case _ => acc
    }
    go(as, z)
  }

foldRightで書くことができるという事実を無視してfoldLeft、私のアプローチはどのように持ちこたえますか?

4

0 に答える 0