parsing - How can assign different return types to a function in Scala? -
i trying write function should return different pairs depending on input. have override "+ - / *" in scala specific use. each 1 ( +, -,* ,/) has 3 implementations based on input. have rdd , float inputs can + between rdd , rdd, or float , rdd, or float , float , on.
now having parser reads expression input : rdd+1 , parse , create postfix make calculations easier : rdd1+ , want calculation using implemented + . of this algorithm trying change in way make performing calculation based on input expression. instance contains:
var lastop: (float, float) => float = add
how can change this: (float, float) => float
accept (rdd, float)|(rdd, rdd) |(float, float) => rdd = add
// implementation of add ???
edition:
i added part of 2 answers below: ok wrote :
def lastop:(either[rdd[(int,array[float])], float], either[rdd[(int,array[float])], float]) => rdd[(int,array[float])] = sv.+
in sv instance other class have been override + in in 2 different ways ma getting error guess because compiler gets confused implementation use
error: type mismatch; [error] found : (that: org.apache.spark.rdd.rdd[(int, array[float])])org.apache.spark.rdd.rdd[(int, array[float])] <and> (that: float)org.apache.spark.rdd.rdd[(int, array[float])] [error] required: (either[org.apache.spark.rdd.rdd[(int, array[float])],float], either[org.apache.spark.rdd.rdd[(int, array[float])],float]) => org.apache.spark.rdd.rdd[(int, array[float])]
note: says found 2 different implementations "+"
well, i'm not sure best way it, 1 way , should result in usage described (or @ least close it):
import scala.language.implicitconversions // implicit conversions implicit def float2either(in: float): either[float, rdd[(int,array[float])]] = left(in) implicit def rdd2either(in: rdd[(int,array[float])]): either[float, rdd[(int,array[float])]] = right(in) def add(left: either[float, rdd[(int,array[float])]], right: either[float, rdd[(int,array[float])]]): float = { (left, right) match { case (left(somefloat), left(anotherfloat)) => ??? case (left(somefloat), right(somerdd)) => ??? case (right(somerdd), left(somefloat)) => ??? case (right(somerdd), right(anotherrdd)) => ??? } } val lastop: (either[float, rdd[(int,array[float])]], either[float, rdd[(int,array[float])]]) => float = add
another way, , better one, pimp library pattern.
however, not able decide (float + float) yield. in sane cases should not problem.
you write implicit wrapper classes float , rdd 'richfloat' 'richint' , like. implementing operators each accept other input.
implicit class richrdd(val underlying: rdd) extends anyval { def +(in: float): float = ??? def +(in: test): float = ??? } implicit class richerfloat(val underlying: float) extends anyval { def +(in: rdd): float = ??? }
Comments
Post a Comment