Skip to content

Instantly share code, notes, and snippets.

@macalinao
Created October 21, 2016 06:58
Show Gist options
  • Save macalinao/0b4e4b9a237eade35741a10bdcb1e8d9 to your computer and use it in GitHub Desktop.
Save macalinao/0b4e4b9a237eade35741a10bdcb1e8d9 to your computer and use it in GitHub Desktop.
def divideScalars[S, T](
divisors: Map[Int, S], scalars: Map[Int, T]
)(implicit s: scala.math.Numeric[S], t: scala.math.Numeric[T]): Map[Int, Double] = {
val df = divisors.mapValues(x => s.toDouble(x))
scalars.mapValues(x => t.toDouble(x)).transform((k, v) =>
df.get(k) match {
case Some(divisor) if divisor != 0 => v / divisor
case None => 0
}
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment