mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-18 13:02:11 +03:00
15b3ac0aee
PR-URL: https://github.com/hasura/graphql-engine-mono/pull/6777 Co-authored-by: Samir Talwar <47582+SamirTalwar@users.noreply.github.com> GitOrigin-RevId: 916abab76446cf7c4e1e63dc112ba4994ab4d23d
43 lines
1.1 KiB
Haskell
43 lines
1.1 KiB
Haskell
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
module Data.HashMap.Strict.InsOrd.Extended
|
|
( module OMap,
|
|
catMaybes,
|
|
partition,
|
|
alterF,
|
|
)
|
|
where
|
|
|
|
import Data.HashMap.Strict.InsOrd as OMap
|
|
import Data.Hashable (Hashable)
|
|
import Witherable (Filterable (..))
|
|
import Prelude
|
|
|
|
instance Filterable (OMap.InsOrdHashMap k) where
|
|
mapMaybe = OMap.mapMaybe
|
|
filter = OMap.filter
|
|
|
|
partition :: Hashable k => (v -> Bool) -> OMap.InsOrdHashMap k v -> (OMap.InsOrdHashMap k v, OMap.InsOrdHashMap k v)
|
|
partition predicate =
|
|
OMap.foldlWithKey'
|
|
( \(left, right) key val ->
|
|
if (predicate val)
|
|
then (OMap.insert key val left, right)
|
|
else (left, OMap.insert key val right)
|
|
)
|
|
(mempty, mempty)
|
|
|
|
-- | 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, 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
|