diff options
author | Nadrieril Feneanar | 2019-08-03 20:59:50 +0200 |
---|---|---|
committer | GitHub | 2019-08-03 20:59:50 +0200 |
commit | 30bc224b524031fbf64057516961831890bb749c (patch) | |
tree | e5e85ade0666ad17011a44069bb6e8ae7528f801 /dhall/src/phase/normalize.rs | |
parent | 925bf5b5cb7d10301d0a92214bc13d8eb53bf035 (diff) | |
parent | 9447cb5d36dfc0b1b9a4bbaf09c82579fd59b7a2 (diff) |
Merge pull request #92 from FintanH/fintan/typecheck-combine-types
Add typechecking for RecursiveRecordTypeMerge
Diffstat (limited to 'dhall/src/phase/normalize.rs')
-rw-r--r-- | dhall/src/phase/normalize.rs | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/dhall/src/phase/normalize.rs b/dhall/src/phase/normalize.rs index 7d86833..a493b66 100644 --- a/dhall/src/phase/normalize.rs +++ b/dhall/src/phase/normalize.rs @@ -372,6 +372,38 @@ enum Ret<'a> { Expr(ExprF<Thunk, X>), } +/// Performs an intersection of two HashMaps. +/// +/// # Arguments +/// +/// * `f` - Will compute the final value from the intersecting +/// key and the values from both maps. +/// +/// # Description +/// +/// If the key is present in both maps then the final value for +/// that key is computed via the `f` function. +/// +/// The final map will contain the shared keys from the +/// two input maps with the final computed value from `f`. +pub(crate) fn intersection_with_key<K, T, U, V>( + mut f: 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 (k, t) in map1 { + // Only insert in the final map if the key exists in both + if let Some(u) = map2.get(k) { kvs.insert(k.clone(), f(k, t, u)); } + } + + kvs +} + /// Performs an outer join of two HashMaps. /// /// # Arguments |