Posts

Showing posts with the label either

How to use Either in scala

How to use Either in scala I want to do similar like below def run(list : List(String)){ val map = Map[String,String] (immutable map) list.foreach{ val x = someOtherMethod() val result = processMap(x,map) // please see below method for details //Not sure how to use result here 1) if ProcessMap returns Map then I want to assign returned MAP to map val 2) otherwise don't do anything } writeOutput(x,map) } def processMap(x :boolean, map : Map[String,String]) : Either[Option(String) , Map[String,String]] { x match { case true => { do some processing and update map and return Right(map ++ newGeneratedMap) } case _ => Left(None) } } In above example I want to use Map as Java static variable. I want to pass Map to a method based on some condition it may get updated and may not. If its updated then only I want to return it and pass it to next method.If map is not updated then I want to pass existing map to method. Main Question : How can I access output of result (left ...