papa-0.7.0: src/Papa/UnorderedContainers/Data/HashMap.hs
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
-- | Hash maps, strict in their values, without the partial function of
-- "Data.HashMap.Strict". Import qualified:
--
-- > import qualified Papa.UnorderedContainers.Data.HashMap as HashMap
--
-- Every function of "Data.HashMap.Strict" is total but @(!)@, which throws
-- for a key that is not in the map, and is not exported. Read, insert, change
-- and delete the value at a key with lens's 'Control.Lens.At' and
-- 'Control.Lens.Ixed' instances:
--
-- * @'Papa.Lens.Optic.the' (at k) m@ (a 'Data.Maybe.Maybe'), @m ^? ix k@,
-- or 'HashMap.!?' and 'HashMap.lookup', to read;
-- * @m & at k ?~ v@ to insert, @'Control.Lens.sans' k m@
-- (@m & at k .~ Nothing@) to delete;
-- * @m & ix k %~ f@ to change the value at a key that is in the map.
--
-- lens's instances for 'HashMap': 'Control.Lens.At', 'Control.Lens.Ixed',
-- 'Control.Lens.Each', 'Control.Lens.AsEmpty',
-- 'Control.Lens.FunctorWithIndex', 'Control.Lens.FoldableWithIndex',
-- 'Control.Lens.TraversableWithIndex'.
--
-- The deprecated @lookupDefault@ is not exported: use 'HashMap.findWithDefault'.
module Papa.UnorderedContainers.Data.HashMap (
module X,
) where
import Data.HashMap.Strict as X hiding (
lookupDefault,
(!),
)
-- $setup
-- >>> :set -XNoImplicitPrelude
-- >>> import Papa
-- >>> import qualified Papa.UnorderedContainers.Data.HashMap as HashMap
-- >>> import Hedgehog
-- >>> import qualified Hedgehog.Gen as Gen
-- >>> import qualified Hedgehog.Range as Range
-- >>> let genKey = Gen.int (Range.linear 0 20)
-- >>> let genMap = HashMap.fromList <$> Gen.list (Range.linear 0 10) ((,) <$> genKey <*> Gen.alpha)
-- $examples
--
-- ==== __Examples__
--
-- >>> the (at (1 :: Int)) (HashMap.fromList [(1, 'a')])
-- Just 'a'
--
-- >>> HashMap.fromList [(1, 'a')] ^? ix (2 :: Int)
-- Nothing
--
-- >>> HashMap.toList (sans 1 (HashMap.fromList [(1 :: Int, 'a')] & at 2 ?~ 'b'))
-- [(2,'b')]
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; k <- forAll genKey; the (at k) m === HashMap.lookup k m
-- ...passed 100 tests.
-- True
--
-- >>> check $ withTests 100 $ property $ do m <- forAll genMap; k <- forAll genKey; v <- forAll Gen.alpha; (m & at k ?~ v) === HashMap.insert k v m
-- ...passed 100 tests.
-- True