Skip to content

Instantly share code, notes, and snippets.

@jedws
Created August 21, 2012 06:05
Show Gist options
  • Select an option

  • Save jedws/3412529 to your computer and use it in GitHub Desktop.

Select an option

Save jedws/3412529 to your computer and use it in GitHub Desktop.
simple scala unionWith
object Maps {
import scalaz._, syntax.semigroup._
def unionWithKey[A, K](f: (K, A, A) => A): (Map[K, A], Map[K, A]) => Map[K, A] =
(m1, m2) =>
(m1 -- m2.keySet) ++ m2.map {
case (k, v) => k -> (m1 get k map { f(k, v, _) } getOrElse v)
}
def unionWith[K, A: Semigroup]: (Map[K, A], Map[K, A]) => Map[K, A] =
unionWithKey { (_, a, b) => a |+| b }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment