scala - Simple example of Either, Left, Right with scalaz -


how can implemented using scalaz? example taken here

object myobject {    def dividexbyy(x: int, y: int): either[string, int] = {     if (y == 0) left("cannot divide 0")     else right(x / y)    }    def main(args: array[string]) {      println(dividexbyy(12, 3))     println(dividexbyy(12, 0))      dividexbyy(12, 3) match {       case right(a) => println(s"we got: ${a}")       case left(b) => println(s"we got: ${b}")     }   } } 

import scalaz.{-\/, \/, \/-}  object myobject {    def dividexbyy(x: int, y: int): either[string, int] = {     if (y == 0) left("cannot divide 0")     else right(x / y)    }    def dividexbyyscalaz(x: int, y: int): \/[string, int] = {     if (y == 0) -\/("cannot divide 0")     else \/-(x / y)   }    def main(args: array[string]) {      println(dividexbyy(12, 3))     println(dividexbyy(12, 0))      dividexbyy(12, 3) match {       case right(a) => println(s"we got: ${a}")       case left(b) => println(s"we got: ${b}")     }      println("working scalaz")      println(dividexbyyscalaz(12, 3))     println(dividexbyyscalaz(12, 0))      dividexbyyscalaz(12, 3) match {       case -\/(a) => println(s"we got: ${a}")       case \/-(b) => println(s"we got: ${b}")     }   } } 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -