Skip to content

Instantly share code, notes, and snippets.

@adilakhter
Last active August 29, 2015 14:23
Embed
What would you like to do?
`map` using fold
import Tree._
def map[A,B](tree: Tree[A])(f: A => B): Tree[B] =
fold(tree, Tree.empty[B]){(l, x, r) => Node(f(x), l,r)}
scala> map (t1) ( x => x * 10)
res11: Tree[Int] =
Node(40,
Node(20,
Node(10,EmptyTree,EmptyTree),
Node(30,EmptyTree,EmptyTree)),
Node(70,
Node(60,EmptyTree,EmptyTree),
Node(90,EmptyTree,EmptyTree)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment