Last active
August 29, 2015 14:16
-
-
Save rbuckland/3817146c05658729fc97 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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