scala - Pattern Matching on Disjunction -
given following:
scala> val err: \/[string, boolean \/ int] = -\/("bad") err: scalaz.\/[string,scalaz.\/[boolean,int]] = -\/(bad)   i wrote function takes \/[string, boolean \/ int] , returns boolean \/ int:
scala> def f(x: \/[string, boolean \/ int]): \/[boolean, int] = x match {      |       case -\/(_) => -\/(false)      |       case \/-(y) => y      | } f: (x: scalaz.\/[string,scalaz.\/[boolean,int]])scalaz.\/[boolean,int]   it appears work expected:
scala> f(err) res6: scalaz.\/[boolean,int] = -\/(false)  scala> f(\/-(\/-(5))      | ) res7: scalaz.\/[boolean,int] = \/-(5)   is there more concise, idiomatic scalaz way this?
def f(x: \/[string, boolean \/ int]) = x.getorelse(-\/(false))      
Comments
Post a Comment