Bohtvaroh

Blogger - Scalaz Motivation - 14

Jun 1st, 2012
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.35 KB | None | 0 0
  1. implicit object TreeFoldLeft extends FoldLeft[Tree] {
  2.   def foldLeft[A, B](ma: Tree[A], b: B, op: (B, A) => B) = {
  3.     def toList(ma: Tree[A]): List[A] = ma match {
  4.       // not the fastest implementation ever
  5.       case Node(el, left, right) => toList(left) ::: (el :: toList(right))
  6.       case Empty() => Nil
  7.     }
  8.     toList(ma).foldLeft(b)(op)
  9.   }
  10. }
Advertisement
Add Comment
Please, Sign In to add comment