papa-0.7.0: src/Papa/Containers/Data/Map.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Strict maps, with total versions of the partial functions of
-- "Data.Map.Strict". Import qualified:
--
-- > import qualified Papa.Containers.Data.Map as Map
--
-- The functions of "Data.Map.Strict" that throw (@(!)@, @findMin@,
-- @findMax@, @deleteFindMin@, @deleteFindMax@, @elemAt@, @findIndex@,
-- @deleteAt@, @updateAt@) are replaced by functions of the same names that
-- return 'Data.Maybe.Nothing' there instead.
--
-- The functions that build a map from an ordered list, or map its keys
-- with a monotonic function, check the order in the same linear time, and
-- return 'Data.Maybe.Nothing' where it does not hold, where "Data.Map.Strict"'s build
-- a map that breaks its invariant.
module Papa.Containers.Data.Map (
module X,
(!),
findMin,
findMax,
deleteFindMin,
deleteFindMax,
elemAt,
findIndex,
deleteAt,
updateAt,
fromAscList,
fromAscListWith,
fromAscListWithKey,
fromDistinctAscList,
fromDescList,
fromDescListWith,
fromDescListWithKey,
fromDistinctDescList,
mapKeysMonotonic,
) where
import Control.Monad (guard)
import Data.Bool (Bool, (&&))
import Data.Functor (fmap, (<$))
import Data.Int (Int)
import Data.Map.Strict as X hiding (
deleteAt,
deleteFindMax,
deleteFindMin,
elemAt,
findIndex,
findMax,
findMin,
fromAscList,
fromAscListWith,
fromAscListWithKey,
fromDescList,
fromDescListWith,
fromDescListWithKey,
fromDistinctAscList,
fromDistinctDescList,
mapKeysMonotonic,
updateAt,
(!),
)
import qualified Data.Map.Strict as M
import Data.Maybe (Maybe)
import Data.Ord (Ord ((<), (<=), (>), (>=)))
import Data.Tuple (fst)
import Papa.Internal.Order (pairwise)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa as P
-- >>> import qualified Papa.Containers.Data.Map as Map
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genKVs0 = Gen.list (Range.linear 0 10) ((,) <$> Gen.int (Range.linear 0 9) <*> Gen.alpha)
-- >>> let genKVs = Gen.choice [P.sortOn P.fst <$> genKVs0, P.sortOn (Down . P.fst) <$> genKVs0, P.nubBy (\a b -> P.fst a == P.fst b) . P.sortOn P.fst <$> genKVs0, genKVs0]
-- >>> let genMap = Map.fromList <$> Gen.list (Range.linear 0 20) ((,) <$> Gen.int (Range.linear 0 30) <*> Gen.alpha)
-- >>> let genIndex = Gen.int (Range.linear (-5) 25)
-- | Whether an index is a position in a map.
inRange :: Int -> Map k a -> Bool
inRange i m =
0 <= i && i < M.size m
{-# INLINE inRange #-}
-- | Total 'M.!': the value at a key. 'Data.Maybe.Nothing' where the key is not in the
-- map. This is 'M.!?'.
--
-- ==== __Examples__
--
-- >>> Map.fromList [(1, 'a')] Map.! (1 :: Int)
-- Just 'a'
--
-- >>> Map.fromList [(1, 'a')] Map.! (2 :: Int)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; k <- forAll (Gen.int (Range.linear 0 30)); (m Map.! k) === Map.lookup k m
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
(!) :: (Ord k) => Map k a -> k -> Maybe a
(!) =
(M.!?)
{-# INLINE (!) #-}
infixl 9 !
-- | Total 'M.findMin': the smallest key and its value. 'Data.Maybe.Nothing' for the
-- empty map. This is 'M.lookupMin'.
--
-- ==== __Examples__
--
-- >>> Map.findMin (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Just (1,'a')
--
-- >>> Map.findMin (Map.empty :: Map Int Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; Map.findMin m === preview _head (Map.toList m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findMin :: Map k a -> Maybe (k, a)
findMin =
M.lookupMin
{-# INLINE findMin #-}
-- | Total 'M.findMax': the largest key and its value. 'Data.Maybe.Nothing' for the
-- empty map. This is 'M.lookupMax'.
--
-- ==== __Examples__
--
-- >>> Map.findMax (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Just (2,'b')
--
-- >>> Map.findMax (Map.empty :: Map Int Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; Map.findMax m === preview _last (Map.toList m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findMax :: Map k a -> Maybe (k, a)
findMax =
M.lookupMax
{-# INLINE findMax #-}
-- | Total 'M.deleteFindMin': the smallest key and its value, and the map
-- without them. 'Data.Maybe.Nothing' for the empty map. This is 'M.minViewWithKey'.
--
-- ==== __Examples__
--
-- >>> Map.deleteFindMin (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Just ((1,'a'),fromList [(2,'b')])
--
-- >>> Map.deleteFindMin (Map.empty :: Map Int Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; fmap (\(kv, m') -> Map.insert (P.fst kv) (P.snd kv) m') (Map.deleteFindMin m) === (m <$ Map.findMin m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteFindMin :: Map k a -> Maybe ((k, a), Map k a)
deleteFindMin =
M.minViewWithKey
{-# INLINE deleteFindMin #-}
-- | Total 'M.deleteFindMax': the largest key and its value, and the map
-- without them. 'Data.Maybe.Nothing' for the empty map. This is 'M.maxViewWithKey'.
--
-- ==== __Examples__
--
-- >>> Map.deleteFindMax (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Just ((2,'b'),fromList [(1,'a')])
--
-- >>> Map.deleteFindMax (Map.empty :: Map Int Char)
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; fmap (\(kv, m') -> Map.insert (P.fst kv) (P.snd kv) m') (Map.deleteFindMax m) === (m <$ Map.findMax m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteFindMax :: Map k a -> Maybe ((k, a), Map k a)
deleteFindMax =
M.maxViewWithKey
{-# INLINE deleteFindMax #-}
-- | Total 'M.elemAt': the key and value at an index of the map in key
-- order. 'Data.Maybe.Nothing' for an index out of range.
--
-- ==== __Examples__
--
-- >>> Map.elemAt 1 (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Just (2,'b')
--
-- >>> Map.elemAt 2 (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; i <- forAll genIndex; Map.elemAt i m === (Map.toList m P.!? i)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
elemAt :: Int -> Map k a -> Maybe (k, a)
elemAt i m =
M.elemAt i m <$ guard (inRange i m)
{-# INLINE elemAt #-}
-- | Total 'M.findIndex': the index of a key in key order. 'Data.Maybe.Nothing' where
-- the key is not in the map. This is 'M.lookupIndex'.
--
-- ==== __Examples__
--
-- >>> Map.findIndex 2 (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Just 1
--
-- >>> Map.findIndex 3 (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; k <- forAll (Gen.int (Range.linear 0 30)); (Map.findIndex k m >>= \i -> Map.elemAt i m) === fmap (\v -> (k, v)) (Map.lookup k m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
findIndex :: (Ord k) => k -> Map k a -> Maybe Int
findIndex =
M.lookupIndex
{-# INLINE findIndex #-}
-- | Total 'M.deleteAt': the map without the key at an index in key order.
-- 'Data.Maybe.Nothing' for an index out of range.
--
-- ==== __Examples__
--
-- >>> Map.deleteAt 0 (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Just (fromList [(2,'b')])
--
-- >>> Map.deleteAt 2 (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; i <- forAll genIndex; Map.deleteAt i m === fmap (\(k, _) -> Map.delete k m) (Map.elemAt i m)
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
deleteAt :: Int -> Map k a -> Maybe (Map k a)
deleteAt i m =
M.deleteAt i m <$ guard (inRange i m)
{-# INLINE deleteAt #-}
-- | Total 'M.updateAt': update or delete the value at an index in key
-- order. 'Data.Maybe.Nothing' for an index out of range.
--
-- ==== __Examples__
--
-- >>> Map.updateAt (\_ _ -> Just 'z') 0 (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Just (fromList [(1,'z'),(2,'b')])
--
-- >>> Map.updateAt (\_ _ -> Nothing) 5 (Map.fromList [(2, 'b'), (1 :: Int, 'a')])
-- Nothing
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; i <- forAll genIndex; Map.updateAt (\_ _ -> Nothing) i m === Map.deleteAt i m
-- ...passed 100 tests.
-- True
--
-- @since 0.6.0
updateAt :: (k -> a -> Maybe a) -> Int -> Map k a -> Maybe (Map k a)
updateAt f i m =
M.updateAt f i m <$ guard (inRange i m)
{-# INLINE updateAt #-}
-- | Total @fromAscList@: build a map from a list in ascending order of keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.fromAscList [(1, 'a'), (2, 'b')]
-- Just (fromList [(1,'a'),(2,'b')])
--
-- >>> Map.fromAscList [(2, 'b'), (1, 'a')]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; Map.fromAscList kvs === bool Nothing (Just (Map.fromList kvs)) (P.sortOn P.fst kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromAscList :: (Ord k) => [(k, a)] -> Maybe (Map k a)
fromAscList kvs =
M.fromAscList kvs <$ guard (pairwise (<=) (fmap fst kvs))
{-# INLINE fromAscList #-}
-- | Total @fromAscListWith@: build a map from a list in ascending order of keys, combining the values of equal keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.fromAscListWith (<>) [(1, "a"), (1, "b"), (2, "c")]
-- Just (fromList [(1,"ba"),(2,"c")])
--
-- >>> Map.fromAscListWith (<>) [(2, "a"), (1, "b")]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs }; Map.fromAscListWith (<>) kss === bool Nothing (Just (Map.fromListWith (<>) kss)) (P.sortOn P.fst kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromAscListWith :: (Ord k) => (a -> a -> a) -> [(k, a)] -> Maybe (Map k a)
fromAscListWith f kvs =
M.fromAscListWith f kvs <$ guard (pairwise (<=) (fmap fst kvs))
{-# INLINE fromAscListWith #-}
-- | Total @fromAscListWithKey@: build a map from a list in ascending order of keys, combining the values of equal keys with the key, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.fromAscListWithKey (\k a b -> show k <> a <> b) [(1, "a"), (1, "b")]
-- Just (fromList [(1,"1ba")])
--
-- >>> Map.fromAscListWithKey (\_ a _ -> a) [(2, "a"), (1, "b")]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs; f k a b = show k <> a <> b }; Map.fromAscListWithKey f kss === bool Nothing (Just (Map.fromListWithKey f kss)) (P.sortOn P.fst kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromAscListWithKey :: (Ord k) => (k -> a -> a -> a) -> [(k, a)] -> Maybe (Map k a)
fromAscListWithKey f kvs =
M.fromAscListWithKey f kvs <$ guard (pairwise (<=) (fmap fst kvs))
{-# INLINE fromAscListWithKey #-}
-- | Total @fromDistinctAscList@: build a map from a list in strictly ascending order of keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.fromDistinctAscList [(1, 'a'), (2, 'b')]
-- Just (fromList [(1,'a'),(2,'b')])
--
-- >>> Map.fromDistinctAscList [(1, 'a'), (1, 'b')]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; Map.fromDistinctAscList kvs === bool Nothing (Just (Map.fromList kvs)) (P.sortOn P.fst kvs == kvs && P.nub (fmap P.fst kvs) == fmap P.fst kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDistinctAscList :: (Ord k) => [(k, a)] -> Maybe (Map k a)
fromDistinctAscList kvs =
M.fromDistinctAscList kvs <$ guard (pairwise (<) (fmap fst kvs))
{-# INLINE fromDistinctAscList #-}
-- | Total @fromDescList@: build a map from a list in descending order of keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.fromDescList [(2, 'b'), (1, 'a')]
-- Just (fromList [(1,'a'),(2,'b')])
--
-- >>> Map.fromDescList [(1, 'a'), (2, 'b')]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; Map.fromDescList kvs === bool Nothing (Just (Map.fromList kvs)) (P.sortOn (Down . P.fst) kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDescList :: (Ord k) => [(k, a)] -> Maybe (Map k a)
fromDescList kvs =
M.fromDescList kvs <$ guard (pairwise (>=) (fmap fst kvs))
{-# INLINE fromDescList #-}
-- | Total @fromDescListWith@: build a map from a list in descending order of keys, combining the values of equal keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.fromDescListWith (<>) [(2, "c"), (1, "a"), (1, "b")]
-- Just (fromList [(1,"ba"),(2,"c")])
--
-- >>> Map.fromDescListWith (<>) [(1, "a"), (2, "b")]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs }; Map.fromDescListWith (<>) kss === bool Nothing (Just (Map.fromListWith (<>) kss)) (P.sortOn (Down . P.fst) kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDescListWith :: (Ord k) => (a -> a -> a) -> [(k, a)] -> Maybe (Map k a)
fromDescListWith f kvs =
M.fromDescListWith f kvs <$ guard (pairwise (>=) (fmap fst kvs))
{-# INLINE fromDescListWith #-}
-- | Total @fromDescListWithKey@: build a map from a list in descending order of keys, combining the values of equal keys with the key, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.fromDescListWithKey (\k a b -> show k <> a <> b) [(1, "a"), (1, "b")]
-- Just (fromList [(1,"1ba")])
--
-- >>> Map.fromDescListWithKey (\_ a _ -> a) [(1, "a"), (2, "b")]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs; f k a b = show k <> a <> b }; Map.fromDescListWithKey f kss === bool Nothing (Just (Map.fromListWithKey f kss)) (P.sortOn (Down . P.fst) kvs == kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDescListWithKey :: (Ord k) => (k -> a -> a -> a) -> [(k, a)] -> Maybe (Map k a)
fromDescListWithKey f kvs =
M.fromDescListWithKey f kvs <$ guard (pairwise (>=) (fmap fst kvs))
{-# INLINE fromDescListWithKey #-}
-- | Total @fromDistinctDescList@: build a map from a list in strictly descending order of keys, in linear time. 'Data.Maybe.Nothing' where it is not, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.fromDistinctDescList [(2, 'b'), (1, 'a')]
-- Just (fromList [(1,'a'),(2,'b')])
--
-- >>> Map.fromDistinctDescList [(1, 'a'), (1, 'b')]
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do kvs <- forAll genKVs; Map.fromDistinctDescList kvs === bool Nothing (Just (Map.fromList kvs)) (P.sortOn (Down . P.fst) kvs == kvs && P.nub (fmap P.fst kvs) == fmap P.fst kvs)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
fromDistinctDescList :: (Ord k) => [(k, a)] -> Maybe (Map k a)
fromDistinctDescList kvs =
M.fromDistinctDescList kvs <$ guard (pairwise (>) (fmap fst kvs))
{-# INLINE fromDistinctDescList #-}
-- | Total @mapKeysMonotonic@: map the keys with a strictly increasing function, in linear time. 'Data.Maybe.Nothing' where the function is not strictly increasing on the keys of the map, where "Data.Map.Strict"'s builds a map that breaks its invariant.
--
-- ==== __Examples__
--
-- >>> Map.mapKeysMonotonic (* 2) (Map.fromList [(1, 'a'), (2, 'b')])
-- Just (fromList [(2,'a'),(4,'b')])
--
-- >>> Map.mapKeysMonotonic negate (Map.fromList [(1, 'a'), (2, 'b')])
-- Nothing
--
-- >>> check $ withTests 200 $ property $ do m <- forAll (fmap Map.fromList genKVs); c <- forAll (Gen.int (Range.linear (-2) 2)); Map.mapKeysMonotonic (* c) m === bool Nothing (Just (Map.mapKeys (* c) m)) (c > 0 || Map.size m <= 1)
-- ...passed 200 tests.
-- True
--
-- @since 0.6.0
mapKeysMonotonic :: (Ord k2) => (k1 -> k2) -> Map k1 a -> Maybe (Map k2 a)
mapKeysMonotonic f m =
let r = M.mapKeysMonotonic f m
in r <$ guard (pairwise (<) (M.keys r))
{-# INLINE mapKeysMonotonic #-}