Last active
June 7, 2017 17:24
-
-
Save BenFradet/0a2ef44af34b22d9c79a3af95bbf3173 to your computer and use it in GitHub Desktop.
Q
This file contains 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
// o for original t for translated | |
// basically a translation table | |
val reference = Map("schema1" -> Map("oa1" -> "ta1", "ob1" -> "tb1"), | |
"schema2" -> Map("oa2" -> "ta2", "ob2" -> "tb2")) | |
// given data with the original names and values | |
// v for value | |
val given = Map("ob2" -> "vb2", "oa1" -> "va1") | |
val wanted = Map("schema1" -> Map("ta1" -> "va1"), "schema2" -> Map("tb2" -> "vb2")) | |
// 1. brute | |
// check for every key in `given` if it is a value in one of the schema in `reference` | |
// cost: N*M | |
// 2. moar maps | |
// create an intermediary table ("reverse" of `reference`) | |
val intermediary = Map("oa1" -> "schema1", "ob1" -> "schema1", "oa2" -> "schema2", "ob2" -> "schema2") | |
// build the list of needed schemas `needed` from `intermediary` and `given` | |
// index `reference` on schema and field to get to the translation | |
// cost: N ? | |
val indirect = reference.flatMap { case (k, v) => v.keys.map(_ -> k) } | |
given.foldLeft(Map.empty[String, Map[String, String]]) { (m, e) => | |
indirect.get(e._1).map { s => | |
val ref = reference(s)(e._1) | |
val tr = m.getOrElse(s, Map.empty) + (ref -> e._2) | |
m + (s -> tr) | |
} | |
.getOrElse(m) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment