diff options
author | FintanH | 2019-08-03 19:46:03 +0100 |
---|---|---|
committer | FintanH | 2019-08-03 19:46:03 +0100 |
commit | 9447cb5d36dfc0b1b9a4bbaf09c82579fd59b7a2 (patch) | |
tree | e5e85ade0666ad17011a44069bb6e8ae7528f801 /dhall/src/phase/normalize.rs | |
parent | 8bc33c16e90ff6e1d2b4ddc7317a91190c9d8540 (diff) | |
parent | 925bf5b5cb7d10301d0a92214bc13d8eb53bf035 (diff) |
Merge remote-tracking branch 'origin/master' into fintan/typecheck-combine-types
Diffstat (limited to '')
-rw-r--r-- | dhall/src/phase/normalize.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/dhall/src/phase/normalize.rs b/dhall/src/phase/normalize.rs index 4a25ed9..a493b66 100644 --- a/dhall/src/phase/normalize.rs +++ b/dhall/src/phase/normalize.rs @@ -404,6 +404,60 @@ where kvs } +/// Performs an outer join of two HashMaps. +/// +/// # Arguments +/// +/// * `ft` - Will convert the values of the first map +/// into the target value. +/// +/// * `fu` - Will convert the values of the second map +/// into the target value. +/// +/// * `fktu` - Will convert the key and values from both maps +/// into the target type. +/// +/// # Description +/// +/// If the key is present in both maps then the final value for +/// that key is computed via the `fktu` function. Otherwise, the +/// final value will be calculated by either the `ft` or `fu` value +/// depending on which map the key is present in. +/// +/// The final map will contain all keys from the two input maps with +/// also values computed as per above. +pub(crate) fn outer_join<K, T, U, V>( + mut ft: impl FnMut(&T) -> V, + mut fu: impl FnMut(&U) -> V, + mut fktu: impl FnMut(&K, &T, &U) -> V, + map1: &HashMap<K, T>, + map2: &HashMap<K, U>, +) -> HashMap<K, V> +where + K: std::hash::Hash + Eq + Clone, +{ + let mut kvs = HashMap::new(); + + for (k1, t) in map1 { + let v = if let Some(u) = map2.get(k1) { + // The key exists in both maps + // so use all values for computation + fktu(k1, t, u) + } else { + // Key only exists in map1 + ft(t) + }; + kvs.insert(k1.clone(), v); + } + + for (k1, u) in map2 { + // Insert if key was missing in map1 + kvs.entry(k1.clone()).or_insert(fu(u)); + } + + kvs +} + pub(crate) fn merge_maps<K, V>( map1: &HashMap<K, V>, map2: &HashMap<K, V>, |