summaryrefslogtreecommitdiff
path: root/dhall/src/phase/normalize.rs
diff options
context:
space:
mode:
authorNadrieril Feneanar2019-08-03 20:59:50 +0200
committerGitHub2019-08-03 20:59:50 +0200
commit30bc224b524031fbf64057516961831890bb749c (patch)
treee5e85ade0666ad17011a44069bb6e8ae7528f801 /dhall/src/phase/normalize.rs
parent925bf5b5cb7d10301d0a92214bc13d8eb53bf035 (diff)
parent9447cb5d36dfc0b1b9a4bbaf09c82579fd59b7a2 (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.rs32
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