Related
I often find myself needing to chain collects where I want to do multiple collects in a single traversal. I also would like to return a "remainder" for things that don't match any of the collects.
For example:
sealed trait Animal
case class Cat(name: String) extends Animal
case class Dog(name: String, age: Int) extends Animal
val animals: List[Animal] =
List(Cat("Bob"), Dog("Spot", 3), Cat("Sally"), Dog("Jim", 11))
// Normal way
val cats: List[Cat] = animals.collect { case c: Cat => c }
val dogAges: List[Int] = animals.collect { case Dog(_, age) => age }
val rem: List[Animal] = Nil // No easy way to create this without repeated code
This really isn't great, it requires multiple iterations and there is no reasonable way to calculate the remainder. I could write a very complicated fold to pull this off, but it would be really nasty.
Instead, I usually opt for mutation which is fairly similar to the logic you would have in a fold:
import scala.collection.mutable.ListBuffer
// Ugly, hide the mutation away
val (cats2, dogsAges2, rem2) = {
// Lose some benefits of type inference
val cs = ListBuffer[Cat]()
val da = ListBuffer[Int]()
val rem = ListBuffer[Animal]()
// Bad separation of concerns, I have to merge all of my functions
animals.foreach {
case c: Cat => cs += c
case Dog(_, age) => da += age
case other => rem += other
}
(cs.toList, da.toList, rem.toList)
}
I don't like this one bit, it has worse type inference and separation of concerns since I have to merge all of the various partial functions. It also requires lots of lines of code.
What I want, are some useful patterns, like a collect that returns the remainder (I grant that partitionMap new in 2.13 does this, but uglier). I also could use some form of pipe or map for operating on parts of tuples. Here are some made up utilities:
implicit class ListSyntax[A](xs: List[A]) {
import scala.collection.mutable.ListBuffer
// Collect and return remainder
// A specialized form of new 2.13 partitionMap
def collectR[B](pf: PartialFunction[A, B]): (List[B], List[A]) = {
val rem = new ListBuffer[A]()
val res = new ListBuffer[B]()
val f = pf.lift
for (elt <- xs) {
f(elt) match {
case Some(r) => res += r
case None => rem += elt
}
}
(res.toList, rem.toList)
}
}
implicit class Tuple2Syntax[A, B](x: Tuple2[A, B]){
def chainR[C](f: B => C): Tuple2[A, C] = x.copy(_2 = f(x._2))
}
Now, I can write this in a way that could be done in a single traversal (with a lazy datastructure) and yet follows functional, immutable practice:
// Relatively pretty, can imagine lazy forms using a single iteration
val (cats3, (dogAges3, rem3)) =
animals.collectR { case c: Cat => c }
.chainR(_.collectR { case Dog(_, age) => age })
My question is, are there patterns like this? It smells like the type of thing that would be in a library like Cats, FS2, or ZIO, but I am not sure what it might be called.
Scastie link of code examples: https://scastie.scala-lang.org/Egz78fnGR6KyqlUTNTv9DQ
I wanted to see just how "nasty" a fold() would be.
val (cats
,dogAges
,rem) = animals.foldRight((List.empty[Cat]
,List.empty[Int]
,List.empty[Animal])) {
case (c:Cat, (cs,ds,rs)) => (c::cs, ds, rs)
case (Dog(_,d),(cs,ds,rs)) => (cs, d::ds, rs)
case (r, (cs,ds,rs)) => (cs, ds, r::rs)
}
Eye of the beholder I suppose.
How about defining a couple utility classes to help you with this?
case class ListCollect[A](list: List[A]) {
def partialCollect[B](f: PartialFunction[A, B]): ChainCollect[List[B], A] = {
val (cs, rem) = list.partition(f.isDefinedAt)
new ChainCollect((cs.map(f), rem))
}
}
case class ChainCollect[A, B](tuple: (A, List[B])) {
def partialCollect[C](f: PartialFunction[B, C]): ChainCollect[(A, List[C]), B] = {
val (cs, rem) = tuple._2.partition(f.isDefinedAt)
ChainCollect(((tuple._1, cs.map(f)), rem))
}
}
ListCollect is just meant to start the chain, and ChainCollect takes the previous remainder (the second element of the tuple) and tries to apply a PartialFunction to it, creating a new ChainCollect object. I'm not particularly fond of the nested tuples this produces, but you may be able to make it look a bit better if you use Shapeless's HLists.
val ((cats, dogs), rem) = ListCollect(animals)
.partialCollect { case c: Cat => c }
.partialCollect { case Dog(_, age) => age }
.tuple
Scastie
Dotty's *: type makes this a bit easier:
opaque type ChainResult[Prev <: Tuple, Rem] = (Prev, List[Rem])
extension [P <: Tuple, R, N](chainRes: ChainResult[P, R]) {
def partialCollect(f: PartialFunction[R, N]): ChainResult[List[N] *: P, R] = {
val (cs, rem) = chainRes._2.partition(f.isDefinedAt)
(cs.map(f) *: chainRes._1, rem)
}
}
This does end up in the output being reversed, but it doesn't have that ugly nesting from my previous approach:
val ((owls, dogs, cats), rem) = (EmptyTuple, animals)
.partialCollect { case c: Cat => c }
.partialCollect { case Dog(_, age) => age }
.partialCollect { case Owl(wisdom) => wisdom }
/* more animals */
case class Owl(wisdom: Double) extends Animal
case class Fly(isAnimal: Boolean) extends Animal
val animals: List[Animal] =
List(Cat("Bob"), Dog("Spot", 3), Cat("Sally"), Dog("Jim", 11), Owl(200), Fly(false))
Scastie
And if you still don't like that, you can always define a few more helper methods to reverse the tuple, add the extension on a List without requiring an EmptyTuple to begin with, etc.
//Add this to the ChainResult extension
def end: Reverse[List[R] *: P] = {
def revHelp[A <: Tuple, R <: Tuple](acc: A, rest: R): RevHelp[A, R] =
rest match {
case EmptyTuple => acc.asInstanceOf[RevHelp[A, R]]
case h *: t => revHelp(h *: acc, t).asInstanceOf[RevHelp[A, R]]
}
revHelp(EmptyTuple, chainRes._2 *: chainRes._1)
}
//Helpful types for safety
type Reverse[T <: Tuple] = RevHelp[EmptyTuple, T]
type RevHelp[A <: Tuple, R <: Tuple] <: Tuple = R match {
case EmptyTuple => A
case h *: t => RevHelp[h *: A, t]
}
And now you can do this:
val (cats, dogs, owls, rem) = (EmptyTuple, animals)
.partialCollect { case c: Cat => c }
.partialCollect { case Dog(_, age) => age }
.partialCollect { case Owl(wisdom) => wisdom }
.end
Scastie
Since you mentioned cats, I would also add solution using foldMap:
sealed trait Animal
case class Cat(name: String) extends Animal
case class Dog(name: String) extends Animal
case class Snake(name: String) extends Animal
val animals: List[Animal] = List(Cat("Bob"), Dog("Spot"), Cat("Sally"), Dog("Jim"), Snake("Billy"))
val map = animals.foldMap{ //Map(other -> List(Snake(Billy)), cats -> List(Cat(Bob), Cat(Sally)), dogs -> List(Dog(Spot), Dog(Jim)))
case d: Dog => Map("dogs" -> List(d))
case c: Cat => Map("cats" -> List(c))
case o => Map("other" -> List(o))
}
val tuples = animals.foldMap{ //(List(Dog(Spot), Dog(Jim)),List(Cat(Bob), Cat(Sally)),List(Snake(Billy)))
case d: Dog => (List(d), Nil, Nil)
case c: Cat => (Nil, List(c), Nil)
case o => (Nil, Nil, List(o))
}
Arguably it's more succinct than fold version, but it has to combine partial results using monoids, so it won't be as performant.
This code is dividing a list into three sets, so the natural way to do this is to use partition twice:
val (cats, notCat) = animals.partitionMap{
case c: Cat => Left(c)
case x => Right(x)
}
val (dogAges, rem) = notCat.partitionMap {
case Dog(_, age) => Left(age)
case x => Right(x)
}
A helper method can simplify this
def partitionCollect[T, U](list: List[T])(pf: PartialFunction[T, U]): (List[U], List[T]) =
list.partitionMap {
case t if pf.isDefinedAt(t) => Left(pf(t))
case x => Right(x)
}
val (cats, notCat) = partitionCollect(animals) { case c: Cat => c }
val (dogAges, rem) = partitionCollect(notCat) { case Dog(_, age) => age }
This is clearly extensible to more categories, with the slight irritation of having to invent temporary variable names (which could be overcome by explicit n-way partition methods)
I had an imperative program which deserializes to a Binary Tree from an array. Its a BFS algorithm. I was wondering how to do this in Scala with functional programming concepts.
class TreeNode(_value: Int = 0, _left: TreeNode = null, _right: TreeNode = null) {
val value: Int = _value
val left: TreeNode = _left
val right: TreeNode = _right
}
def createTree(list: Array[Int]): TreeNode = ???
For reference this is the imperative version in Javascript. The algorithm is described here. https://support.leetcode.com/hc/en-us/articles/360011883654-What-does-1-null-2-3-mean-in-binary-tree-representation-
class TreeNode {
constructor(val){
this.val = val;
this.left = this.right = null;
}
}
function makeTree(arr){
let root = new TreeNode(arr[0]);
let q = [root];
let index = 1;
while(q.length){
let node = q.splice(0,1)[0];
if(arr[index] != null){
node.left = new TreeNode(arr[index]);
q.push(node.left);
}
index++;
if(arr[index] != null){
node.right = new TreeNode(arr[index]);
q.push(node.right);
}
index++;
}
return root;
}
First of all, you can use a case class to simplify your tree class, and you should use Option instead of null:
case class Tree(value: Int, left: Option[Tree], right: Option[Tree])
Next, the main trouble here is because your tree is immutable, it needs to be built with a depth-first post-order traversal, and your serialization format requires a breadth-first level-order traversal. So you first have to deserialize to a data structure that can then be traversed in depth-first order. The following uses a Map from (row, column) to the node value:
#scala.annotation.tailrec
private def bfsTraverse(
serialized: List[Option[Int]],
queue: Queue[(Int, Int)],
map: Map[(Int, Int), Int]): Map[(Int, Int), Int] = {
val ((row, col), queueTail) = queue.dequeue
if (serialized.isEmpty) {
map
} else if (serialized.head.isEmpty) {
bfsTraverse(serialized.tail, queueTail, map)
} else {
val newQueue = queueTail.enqueueAll(List((row + 1, col * 2), (row + 1, col * 2 + 1)))
val newMap = map + ((row, col) -> serialized.head.get)
bfsTraverse(serialized.tail, newQueue, newMap)
}
}
Now you can use the output of that function to build your Tree:
private def buildTree(row: Int, col: Int, map: Map[(Int, Int), Int]): Option[Tree] = {
map.get((row, col)).map{value =>
Tree(value, buildTree(row + 1, col * 2, map), buildTree(row + 1, col * 2 + 1, map))
}
}
This solution is a bit verbose but uses some functional concepts and defines the data structures thoroughly.
The algorithm you provided works better with mutable nodes. It's possible to have a shorter solution with just one mutable class, but here, there are two versions (one node class with mutable left/right and the other completely immutable).
Case classes automatically provide a lot of useful features such as comparison and friendly print-out
The processValues function tail-recursively performs the tasks equivalent to the makeTree function you provided.
The #tailrec annotation tells the compiler to check that the function is tail recursive.
Pattern matching using the match and case keywords replace checking for null-ness or different subtypes, and the compiler can check for a non-exhaustive match clause.
The App trait allows you to easily make an object (static class) into an entrypoint to run some examples.
import scala.annotation.tailrec
sealed trait TreeNode[T]
sealed trait MutableTreeNode[T]
object MutableTreeNode {
final case class Empty[T]() extends MutableTreeNode[T]
case class Branch[T](val value: T) extends MutableTreeNode[T] {
protected var left: MutableTreeNode[T] = Empty()
protected var right: MutableTreeNode[T] = Empty()
def setLeft(newLeft: T): Branch[T] = {
left = Branch(newLeft)
left.asInstanceOf[Branch[T]] // shouldn't be necessary but compiler requires it
}
def setRight(newRight: T): Branch[T] = {
right = Branch(newRight)
right.asInstanceOf[Branch[T]]
}
override def toString: String = this.toImmutable().toString
/* converts given node to immutable version */
private def toImmutable(node: MutableTreeNode[T]): TreeNode[T] = {
node match {
case Empty() => TreeNode.Empty()
case b#Branch(value) => TreeNode.Branch(value, toImmutable(b.left), toImmutable(b.right))
}
}
def toImmutable():TreeNode[T] = toImmutable(this)
}
/**
* Modifies nodes inside of queue
*/
#tailrec def processValues[T](values: Seq[Option[T]], queue: Seq[MutableTreeNode.Branch[T]]): Unit = {
(queue, values) match {
case (Nil, _) => ()
case (_, Nil) => ()
case (qHead :: qTail, Some(vLeft) :: Some(vRight) :: vTail) =>
processValues(vTail, qTail :+ qHead.setLeft(vLeft) :+ qHead.setRight(vRight))
case (qHead :: qTail, Some(vLeft) :: None :: vTail) =>
processValues(vTail, qTail :+ qHead.setLeft(vLeft))
case (qHead :: qTail, None :: Some(vRight) :: vTail) =>
processValues(vTail, qTail :+ qHead.setRight(vRight))
case (qHead :: qTail, None :: None :: vTail) =>
processValues(vTail, qTail)
}
}
}
object TreeNode {
final case class Empty[T]() extends TreeNode[T]
final case class Branch[T](value: T, left: TreeNode[T], right: TreeNode[T]) extends TreeNode[T]
def deserialize[T](values: Seq[Option[T]]): TreeNode[T] = {
values match {
case Some(headVal) :: tail =>
val root: MutableTreeNode.Branch[T] = MutableTreeNode.Branch(headVal)
MutableTreeNode.processValues(tail, Seq(root))
root.toImmutable()
case Nil => Empty()
case _ => throw new RuntimeException("Invalid argument values")
}
}
}
object TreeNodeTest extends App {
val input = Seq(Some(5), Some(4), Some(7), None, None, Some(2), None)
val treeNode:TreeNode[Int] = TreeNode.deserialize(input)
println(treeNode)
}
As has been noted, Scala avoids null whenever possible, preferring Option to indicate the absence of a value.
Mutable variables are also shunned, which makes it much easier to construct a B-tree in a depth-first manner rather than breadth-first.
So all you really need is an easy-to-use breadth-first-serialization --to--> depth-first-serialization translator.
I did it in two steps.
//from Breadth-First-Serialization to full tree representation
def BFS2full[A](bfs:IndexedSeq[Option[A]]) :List[List[Option[A]]] = {
val bfsLen = bfs.length
if (bfs.isEmpty) Nil
else
List(bfs.head) :: List.unfold((List(bfs.head),1)){case (pr,idx) =>
Option.when(bfsLen > idx){
val ns = pr.foldLeft((List.empty[Option[A]],idx)){
case ((acc,x), None) => (acc ++ List(None,None), x)
case ((acc,x), _) => (acc ++ List(bfs.lift(x).flatten
,bfs.lift(x+1).flatten), x+2)
}
(ns._1, ns)
}
}
}
//from full tree representation to Depth-First-Serialization
def toDFS[A](lloa :List[List[Option[A]]]
,lvl :Int = 0) :List[Option[A]] = lloa match {
case Nil => List(None, None)
case List(None) :: Nil => List(None)
case List( oa ) :: tl => oa :: toDFS(tl, lvl)
case row :: tl => row.drop(lvl*2) match {
case List(None,None,_*) => List(None, None)
case List(None, ob ,_*) => None :: (ob::toDFS(tl,2*lvl + 1))
case List( oa ,None,_*) => (oa::toDFS(tl,2*lvl)) ++ List(None)
case List( oa , ob ,_*) => (oa :: toDFS(tl, 2*lvl)) ++
(ob :: toDFS(tl,2*lvl + 1))
}
}
Now let's parameterize the tree so that we can build Int trees, Float trees, String trees, etc.
We're also going to make the constructor private so that node creation is only done via factory methods.
case class Tree[A] private (value : A
,left : Option[Tree[A]]
,right : Option[Tree[A]])
All that's left is to supply the factory methods.
object Tree {
private def BFS2full[A]( . . . //as above
private def toDFS[A]( . . . //as above
def fromDFS[A](dfs :IterableOnce[Option[A]]) :Option[Tree[A]] = {
val itr = dfs.iterator
def loop(): Option[Tree[A]] =
Option.when(itr.hasNext)(itr.next())
.flatten
.map(new Tree(_,loop(),loop()))
loop()
}
def fromBFS[A](bfs:IndexedSeq[Option[A]]) :Option[Tree[A]] =
fromDFS(toDFS(BFS2full(bfs)))
}
testing:
Tree.fromBFS(Vector(Some('A'),None,Some('B'),Some('C'))).get
//res0: Tree[Char] = Tree(A,None,Some(Tree(B,Some(Tree(C,None,None)),None)))
Here's a basic solution. It doesn't use lazy vals or any data structures other than List, Option, and Either. You might find it easier to understand because of that or harder because of the verbosity.
I've defined Tree like this, just to make things easier.
sealed trait Tree[+T]
case class Node[+T](data: T, left: Tree[T], right: Tree[T]) extends Tree[T]
case object Empty extends Tree[Nothing]
Also, instead of an Array[Int], I'm using a List[Option[T]] (where T is a type parameter of the makeTree method). Some means there's a node, None is like -1 in your code. This is more idiomatic and also works for types other than Int.
The thing about doing this breadth-first is that you need to kinda keep passing the input list around to the children. First you try to make the left child, then when that's done, you try make the right child. Then you try to make the left child's children, then the right child's children, then their children, and so on.
One way to deal this without using var would be to take in the input list containing the serialized binary tree and see what the head of the list is. If it's a None, we can just return an Empty, because we know it's the end of the tree. If it's a Some however, we can't yet return a tree, but we can return another function that takes in the next round of input and returns a tree.
Since there are 2 different types that can be returned, these functions will be of type List[Option[T]] => Either[List[Option[T]] => ThisFunctionsType, Tree[T]]. Since the function may return another function of the same type, we'll have to define a new type that can return itself:
trait RecFun[T] extends ((List[Option[T]]) => (List[Option[T]], Either[RecFun[T], Tree[T]]))
The reason that it's List[Option[T]] => (List[Option[T]], Either[RecFun[T], Tree[T]]) and not just List[Option[T]] => Either[RecFun[T], Tree[T]] is that in case one child turns out to be a leaf somewhere in the middle of the list, we still need to continue, so the first element of the returned tuple contains the rest of the list after processing.
Now we can define this. The helper function is so that as long as the RecFun returns a Left[RecFun], it keeps passing the remaining input into that function.
def makeTree[T](list: List[Option[T]]): Tree[T] = {
def helper(f: RecFun[T], l: List[Option[T]]): Tree[T] =
f(l) match {
case (_, Right(tree)) => tree
case (next, Left(f)) => helper(f, next)
}
list match {
case Some(x) :: tail => helper(createRec(x), tail)
case _ => Empty
}
}
def createRec[T](data: T): RecFun[T] = {
case None :: Nil | Nil => (Nil, Right(Node(data, Empty, Empty)))
case Some(l) :: Nil => (Nil, Right(Node(data, Node(l, Empty, Empty), Empty)))
case lo :: ro :: rest =>
(rest, (lo, ro) match {
case (Some(l), Some(r)) =>
Left(waitForChildren(data, createRec(l), createRec(r)))
case (Some(l), None) =>
Left(waitForChild(Node(data, _, Empty), createRec(l)))
case (None, Some(r)) =>
Left(waitForChild(Node(data, Empty, _), createRec(r)))
case (None, None) => Right(Node(data, Empty, Empty))
})
}
def waitForChildren[T](data: T, leftF: RecFun[T], rightF: RecFun[T]): RecFun[T] =
input => {
val (next, res) = leftF(input)
res match {
case Right(tree) =>
(next, Left(waitForChild(Node(data, tree, _), rightF)))
case Left(leftF2) => {
val (next2, res2) = rightF(next)
(next2, Left(res2 match {
case Right(tree) => waitForChild(Node(data, _, tree), leftF2)
case Left(rightF2) => waitForChildren(data, leftF2, rightF2)
}))
}
}
}
def waitForChild[T](ctor: Tree[T] => Node[T], f: RecFun[T]): RecFun[T] =
input => {
val (next, res) = f(input)
(next, res match {
case Right(tree) => Right(ctor(tree))
case Left(recFun) => Left(waitForChild(ctor, recFun))
})
}
Scastie
I have an array of objects of type Either[A, B]. If I know for a particular element whether it is an A or a B, how do I call a method on it that only exists on one of the 2 types. For example:
import scala.util.Random
object EitherTest extends App {
def newObj(x: Int): Either[A,B] = {
if (x == 0)
Left(new A())
else
Right(new B())
}
val random = new Random()
val randomArray = (0 until 10).map(_ => random.nextInt(2))
val eitherArray = randomArray.map(newObj)
(0 until 10).foreach(x => randomArray(x) match {
case 0 => eitherArray(x).aMethod()
case 1 => eitherArray(x).bMethod()
case _ => println("Error!")
})
}
class A {
def aMethod() = println("A")
}
class B {
def bMethod() = println("B")
}
When I compile this code, the lines
case 0 => eitherArray(x).aMethod()
case 1 => eitherArray(x).bMethod()
both have the error "value aMethod is not a member of Either[A,B]". How can I solve this?
I don't know why fold doesn't get the respect it deserves. It can be so useful.
eitherArray.foreach(_.fold(_.aMethod(), _.bMethod()))
Well, you can do it if you exctract the logic to another method, and do some pattern matching over the value Either, then check if it is Right or Left, and that's it!
object HelloWorld {
import scala.util.Random
def main(args: Array[String]) {
val random = new Random()
val randomArray = (0 until 10).map(_ => random.nextInt(2))
val eitherArray = randomArray.map(EitherTest.newObj)
(0 until 10).foreach(x => randomArray(x) match {
case 0 => EitherTest.callmethod(eitherArray(x))
case 1 => EitherTest.callmethod(eitherArray(x))
case _ => println("Error!")
})
println("Hello, world!")
}
}
class EitherTest
object EitherTest {
def callmethod(ei : Either[A,B]) = {
ei match {
case Left(a) => a.aMethod()
case Right(b) => b.bMethod()
}
}
def newObj(x: Int): Either[A,B] = {
if (x == 0)
Left(new A())
else
Right(new B())
}
}
class A {
def aMethod() = println("A")
}
class B {
def bMethod() = println("B")
}
Will print for you, for one random example:
A
B
A
B
A
A
A
B
B
B
Hello, world!
Basically, the way you do with Either is projections: Either.left gives you the projection of the left type, and Either.right gives you that of the right.
The projections are somewhat similar to options, in that they can be empty (if your Either is a Right, then the left projection is empty and vice versa), and you can use the usual monadic transformations with them, like map, flatMap, foreach, getOrElse etc.
Your example, could look like this:
randomArray.foreach { either =>
either.left.foreach(_.aMethod)
either.right.foreach(_.bMethod)
}
You could also use pattern-matching instead, that's less general, but, perhaps looks a bit clearer in this case:
randomArray.foreach {
case Left(a) => a.aMethod
case Right(b) => b.bMethod
}
Is there a way to bind a value of Either[L1, R1] to a function of type R1 => Either[L2, R2] and get a value of Either[L1 | L2, R2] so individual functions can declare and potentially return their errors and consumers of a monadic pipeline of these functions can cleanly handle all possible errors in an exhaustive, type-safe way?
Edit
Here's an example...
sealed trait IncrementError
case object MaximumValueReached extends IncrementError
def increment(n: Int): Either[IncrementError, Int] = n match {
case Integer.MAX_VALUE => Left(MaximumValueReached)
case n => Right(n + 1)
}
sealed trait DecrementError
case object MinimumValueReached extends DecrementError
def decrement(n: Int): Either[DecrementError, Int] = n match {
case Integer.MIN_VALUE => Left(MinimumValueReached)
case n => Right(n - 1)
}
for {
n <- increment(0).right
n <- decrement(n).right
} yield n // scala.util.Either[Object, Int] = Right(0)
With that return type I'm not able to do exhaustive error handling. I'm curious if there exists a way to do this using standard Scala Either or if there exists something in a library like scalaz which supports this behavior. I'd like to be able to handle errors like this...
val n = for {
n <- increment(0).right
n <- decrement(n).right
} yield n // scala.util.Either[IncrementError | DecrementError, Int]
match n {
case Left(MaximumValueReached) => println("Maximum value reached!")
case Left(MinimumValueReached) => println("Minimum value reached!")
case Right(_) => println("Success!")
}
I would do following:
/**
* Created by alex on 10/3/16.
*/
object Temp{
sealed trait IncrementError
case object MaximumValueReached extends IncrementError
sealed trait DecrementError
case object MinimumValueReached extends DecrementError
type MyResult = Either[Either[IncrementError, DecrementError], Int]
def increment(n: Int): MyResult = n match {
case Integer.MAX_VALUE => Left(Left(MaximumValueReached))
case n => Right(n + 1)
}
def decrement(n: Int): MyResult = n match {
case Integer.MIN_VALUE => Left(Right(MinimumValueReached))
case n => Right(n - 1)
}
def main(args:Array[String]) = {
val result = for {
k <- increment(0).right
n <- decrement(k).right
} yield n
result match {
case Left(Left(MaximumValueReached)) => println("Maximum value reached!")
case Left(Right(MinimumValueReached)) => println("Minimum value reached!")
case Right(_) => println("Success!")
}
}
}
Honestly I don't like it but it works for the case if you don't want to have IncrementError and DecrementError to inherit from some ancestor trait for some reasons.
I saw anwser here but i dont have Object Empty and i have leaf not just everthing in Node like this :
case class Node ( val e : Int ,left : BinaryTree, right : BinaryTree).
I have problem with adding Leaf as param in this def sums.
import scala.annotation.tailrec
/**
* #author emil
*/
class tree {
}
sealed abstract class BinaryTree
case class Node (left : BinaryTree, right : BinaryTree) extends BinaryTree
case class Leaf (value : Int) extends BinaryTree
object Main {
def main(args : Array[String]) {
val tree = Node(Node(Leaf(1),Leaf(3)), Node(Leaf(5),Leaf(7)));
println(tree)
sum(tree)
}
def sum(bin: BinaryTree) = {
def sums(trees: List[BinaryTree], acc: Int): Int = trees match {
case Nil => acc
case Node(l, r) :: rs => sums(l :: r :: rs, acc)
}
sums(List(bin),0)
}
}
If I understand what you want to do is something like
case Leaf(v) :: rs => sums(xs, acc+v)
May be:
def sum(bin: BinaryTree) = {
def sums(t: BinaryTree): Int = t match {
case Leaf(v) => v
case Node(l, r) => sums(l) + sums(r)
}
sums(bin)
}
val res = sum(tree)
println(res) // 16