module Control.Distributed.Process.Internal.StrictContainerAccessors
  ( mapMaybe
  , mapDefault
  ) where

import Prelude hiding (map)
import Data.Accessor
import Data.Map (Map)
import qualified Data.Map as Map (lookup, insert, delete, findWithDefault)

mapMaybe :: Ord key => key -> Accessor (Map key elem) (Maybe elem)
mapMaybe :: forall key elem.
Ord key =>
key -> Accessor (Map key elem) (Maybe elem)
mapMaybe key
key = (Map key elem -> Maybe elem)
-> (Maybe elem -> Map key elem -> Map key elem)
-> Accessor (Map key elem) (Maybe elem)
forall r a. (r -> a) -> (a -> r -> r) -> Accessor r a
accessor
  (key -> Map key elem -> Maybe elem
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup key
key)
  (\Maybe elem
mVal Map key elem
map -> case Maybe elem
mVal of
      Maybe elem
Nothing  -> key -> Map key elem -> Map key elem
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete key
key Map key elem
map
      Just elem
val -> elem
val elem -> Map key elem -> Map key elem
forall a b. a -> b -> b
`seq` key -> elem -> Map key elem -> Map key elem
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert key
key elem
val Map key elem
map)

mapDefault :: Ord key => elem -> key -> Accessor (Map key elem) elem
mapDefault :: forall key elem.
Ord key =>
elem -> key -> Accessor (Map key elem) elem
mapDefault elem
def key
key = (Map key elem -> elem)
-> (elem -> Map key elem -> Map key elem)
-> Accessor (Map key elem) elem
forall r a. (r -> a) -> (a -> r -> r) -> Accessor r a
accessor
  (elem -> key -> Map key elem -> elem
forall k a. Ord k => a -> k -> Map k a -> a
Map.findWithDefault elem
def key
key)
  (\elem
val Map key elem
map -> elem
val elem -> Map key elem -> Map key elem
forall a b. a -> b -> b
`seq` key -> elem -> Map key elem -> Map key elem
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert key
key elem
val Map key elem
map)