From 1e7dc0ff98f2f6f25ad773c658eb3317360f5573 Mon Sep 17 00:00:00 2001 From: Rob Rix Date: Mon, 14 Dec 2015 17:00:30 -0500 Subject: [PATCH] Define `intersectionWith`. --- src/OrderedMap.hs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/OrderedMap.hs b/src/OrderedMap.hs index 1300f34a1..bf1b51132 100644 --- a/src/OrderedMap.hs +++ b/src/OrderedMap.hs @@ -9,6 +9,7 @@ module OrderedMap ( , empty , union , unions + , intersectionWith ) where data OrderedMap key value = OrderedMap { toList :: [(key, value)] } @@ -42,3 +43,6 @@ union (OrderedMap a) (OrderedMap b) = OrderedMap $ a ++ filter (not . (`elem` ex unions :: Eq key => [OrderedMap key value] -> OrderedMap key value unions = foldl union empty + +intersectionWith :: Eq key => (a -> b -> c) -> OrderedMap key a -> OrderedMap key b -> OrderedMap key c +intersectionWith combine (OrderedMap a) (OrderedMap b) = OrderedMap $ a >>= (\ (key, value) -> maybe [] (pure . ((,) key) . combine value) $ Prelude.lookup key b)