2018-06-28 11:19:27 +03:00
|
|
|
module Data.HashMap.Strict.InsOrd.Extended
|
2021-09-24 01:56:37 +03:00
|
|
|
( module OMap,
|
2022-03-03 23:12:09 +03:00
|
|
|
catMaybes,
|
2021-09-24 01:56:37 +03:00
|
|
|
partition,
|
2022-02-08 19:53:30 +03:00
|
|
|
alterF,
|
2021-09-24 01:56:37 +03:00
|
|
|
)
|
|
|
|
where
|
2018-06-28 11:19:27 +03:00
|
|
|
|
2021-09-24 01:56:37 +03:00
|
|
|
import Data.HashMap.Strict.InsOrd as OMap
|
|
|
|
import Data.Hashable (Hashable)
|
|
|
|
import Prelude
|
2018-06-28 11:19:27 +03:00
|
|
|
|
2022-03-03 23:12:09 +03:00
|
|
|
catMaybes :: InsOrdHashMap k (Maybe v) -> InsOrdHashMap k v
|
|
|
|
catMaybes = OMap.mapMaybe id
|
|
|
|
|
2021-08-04 17:51:20 +03:00
|
|
|
partition :: (Eq k, Hashable k) => (v -> Bool) -> OMap.InsOrdHashMap k v -> (OMap.InsOrdHashMap k v, OMap.InsOrdHashMap k v)
|
|
|
|
partition predicate =
|
|
|
|
OMap.foldlWithKey'
|
2021-09-24 01:56:37 +03:00
|
|
|
( \(left, right) key val ->
|
|
|
|
if (predicate val)
|
|
|
|
then (OMap.insert key val left, right)
|
|
|
|
else (left, OMap.insert key val right)
|
|
|
|
)
|
2021-08-04 17:51:20 +03:00
|
|
|
(mempty, mempty)
|
2022-02-08 19:53:30 +03:00
|
|
|
|
|
|
|
-- | Alter a hashmap using a function that can fail, in which case the entire operation fails.
|
|
|
|
-- (Maybe a version with the key also being passed to the function could be useful.)
|
|
|
|
alterF ::
|
|
|
|
(Functor f, Eq k, Hashable k) =>
|
|
|
|
(Maybe v -> f (Maybe v)) ->
|
|
|
|
k ->
|
|
|
|
InsOrdHashMap k v ->
|
|
|
|
f (InsOrdHashMap k v)
|
|
|
|
alterF f k m = alter' <$> f (OMap.lookup k m)
|
|
|
|
where
|
|
|
|
alter' = \case
|
|
|
|
Nothing -> OMap.delete k m
|
|
|
|
Just v -> OMap.insert k v m
|