Skip to content

Instantly share code, notes, and snippets.

@bradkarels
Created July 14, 2015 14:41
Show Gist options
  • Save bradkarels/f34eaa59acf96b81ab71 to your computer and use it in GitHub Desktop.
Save bradkarels/f34eaa59acf96b81ab71 to your computer and use it in GitHub Desktop.
Creating a simple union of two Maps where values in 'core' will be overwritten by the 'overlay' and values in overlay that do not exist in core will be added to the resulitng Map
val key0 = ("0","0")
val key1 = ("1","0")
val key2 = ("2","0")
val key3 = ("3","0")
val core:Map[(String,String),Option[String]] = Map(key0 -> Some("a"), key1 -> Some("b"), key2 -> Some("c"))
val overlay:Map[(String,String),Option[String]] = Map(key2 -> Some("y"), key3 -> Some("z"))
//val expected = Map(key0 -> Some("a"), key1 -> Some("b"), key2 -> Some("y"), key3 -> Some("z"))
def bundle(core: Map[(String,String),Option[String]], overlay: Map[(String,String),Option[String]]) = {
//def bundle(core: Map[(String,String),Option[String]], overlay: Map[(String,String),Option[String]]): Map[(String,String),Option[String]] = {
val coreKeySet = core.keySet
val overlayKeySet = overlay.keySet
val keys = coreKeySet.union(overlayKeySet)
val mmap = keys.map { k =>
if (overlayKeySet.contains(k))
(k, overlay.get(k).get)
else
(k, core.get(k).get)
}
mmap.toMap
}
val foo = bundle(core,overlay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment