File tree Expand file tree Collapse file tree 3 files changed +21
-0
lines changed
Expand file tree Collapse file tree 3 files changed +21
-0
lines changed Original file line number Diff line number Diff line change 55* Add ` Data.Map.NonEmpty `
66* Add ` Data.Set.NonEmpty `
77* Add ` lookupInternMap `
8+ * Add ` Data.MapExtras.boundedEnumMap `
9+ * Add ` Data.MapExtras.lookupMapFail `
810* Replace ` okeyL ` with ` toOKey `
911
1012## 1.2.4.1
Original file line number Diff line number Diff line change @@ -24,6 +24,8 @@ module Data.MapExtras (
2424 intersectDomPLeft ,
2525 intersectMapSetFold ,
2626 disjointMapSetFold ,
27+ boundedEnumMap ,
28+ lookupMapFail ,
2729 extractKeys ,
2830 extractKeysSmallSet ,
2931 fromKeys ,
@@ -284,3 +286,13 @@ lookupInternMap k = go
284286 LT -> go l
285287 GT -> go r
286288 EQ -> Just (kx, v)
289+
290+ boundedEnumMap :: (Ord k , Bounded a , Enum a ) => (a -> k ) -> Map k a
291+ boundedEnumMap toTxt = Map. fromList [(toTxt t, t) | t <- [minBound .. maxBound ]]
292+
293+ lookupMapFail ::
294+ (Ord k , Show k , MonadFail m ) => String -> Map k v -> k -> m v
295+ lookupMapFail name m key =
296+ case Map. lookup key m of
297+ Just t -> pure t
298+ Nothing -> fail $ " Unrecognized " <> name <> " : " ++ show key
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ import Control.Monad
66import Data.Map (Map )
77import qualified Data.Map.Strict as Map
88import Data.MapExtras
9+ import Data.Word (Word8 )
910import Test.Cardano.Data
1011import Test.Hspec
1112import Test.Hspec.QuickCheck
@@ -39,3 +40,9 @@ mapExtrasSpec =
3940 ma = intersectDomPLeft f m1 m2
4041 expectValidMap ma
4142 ma `shouldBe` Map. mapMaybeWithKey (\ k v -> v <$ (guard . f k =<< Map. lookup k m2)) m1
43+ prop " boundedEnumMap" $ do
44+ let convert = fromIntegral :: Word8 -> Int
45+ m = boundedEnumMap convert :: Map Int Word8
46+ expectValidMap m
47+ forM_ [minBound .. maxBound ] $ \ t -> do
48+ Map. lookup (convert t) m `shouldBe` Just t
You can’t perform that action at this time.
0 commit comments