Skip to content

Instantly share code, notes, and snippets.

@rbuckland
Last active August 29, 2015 14:16
Show Gist options
  • Save rbuckland/3817146c05658729fc97 to your computer and use it in GitHub Desktop.
Save rbuckland/3817146c05658729fc97 to your computer and use it in GitHub Desktop.
def doWork[A: Numeric, B: Numeric](values: Seq[Option[A]], op: String): Option[B] = {
import spire.implicits._
val v = values.flatten
if (v.isEmpty) {
None
} else {
op match {
case "sum" => Some(v.qsum) // returns A
case "avg" | "avg" => Some(v.qsum / v.length) // returns Double
case "max" => Some(v.qmin) // A
case "min" => Some(v.qmin) // A
case _ => None
}
}
}
val resultA = doWork(Seq(Some(44L),Some(34L),Some(45L),None,Some(34L)),"sum")
val reulltB = doWork(Seq(Some(44L),Some(34L),Some(45L),None,Some(34L)),"avg")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment