Created
July 14, 2015 14:41
-
-
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
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
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