{-# LANGUAGE NoImplicitPrelude #-}
-- |
-- Module:      Data.Aeson.Functions
-- Copyright:   (c) 2011-2016 Bryan O'Sullivan
--              (c) 2011 MailRank, Inc.
-- License:     BSD3
-- Maintainer:  Bryan O'Sullivan <bos@serpentine.com>
-- Stability:   experimental
-- Portability: portable

module Data.Aeson.Internal.Functions
    ( mapKeyVal
    , mapKey
    , mapKeyValO
    , mapKeyO
    ) where

import Prelude.Compat

import Data.Hashable (Hashable)
import qualified Data.HashMap.Strict as H
import qualified Data.Map as M

-- | Transform the keys and values of a 'H.HashMap'.
mapKeyVal :: (Eq k2, Hashable k2) => (k1 -> k2) -> (v1 -> v2)
          -> H.HashMap k1 v1 -> H.HashMap k2 v2
mapKeyVal :: forall k2 k1 v1 v2.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> (v1 -> v2) -> HashMap k1 v1 -> HashMap k2 v2
mapKeyVal k1 -> k2
fk v1 -> v2
kv = (k1 -> v1 -> HashMap k2 v2 -> HashMap k2 v2)
-> HashMap k2 v2 -> HashMap k1 v1 -> HashMap k2 v2
forall k v a. (k -> v -> a -> a) -> a -> HashMap k v -> a
H.foldrWithKey (\k1
k v1
v -> k2 -> v2 -> HashMap k2 v2 -> HashMap k2 v2
forall k v.
(Eq k, Hashable k) =>
k -> v -> HashMap k v -> HashMap k v
H.insert (k1 -> k2
fk k1
k) (v1 -> v2
kv v1
v)) HashMap k2 v2
forall k v. HashMap k v
H.empty
{-# INLINE mapKeyVal #-}

-- | Transform the keys and values of a 'M.Map'.
mapKeyValO :: (Ord k2) => (k1 -> k2) -> (v1 -> v2)
          -> M.Map k1 v1 -> M.Map k2 v2
mapKeyValO :: forall k2 k1 v1 v2.
Ord k2 =>
(k1 -> k2) -> (v1 -> v2) -> Map k1 v1 -> Map k2 v2
mapKeyValO k1 -> k2
fk v1 -> v2
kv = (k1 -> v1 -> Map k2 v2 -> Map k2 v2)
-> Map k2 v2 -> Map k1 v1 -> Map k2 v2
forall k a b. (k -> a -> b -> b) -> b -> Map k a -> b
M.foldrWithKey (\k1
k v1
v -> k2 -> v2 -> Map k2 v2 -> Map k2 v2
forall k a. Ord k => k -> a -> Map k a -> Map k a
M.insert (k1 -> k2
fk k1
k) (v1 -> v2
kv v1
v)) Map k2 v2
forall k a. Map k a
M.empty
{-# INLINE mapKeyValO #-}

-- | Transform the keys of a 'H.HashMap'.
mapKey :: (Eq k2, Hashable k2) => (k1 -> k2) -> H.HashMap k1 v -> H.HashMap k2 v
mapKey :: forall k2 k1 v.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> HashMap k1 v -> HashMap k2 v
mapKey k1 -> k2
fk = (k1 -> k2) -> (v -> v) -> HashMap k1 v -> HashMap k2 v
forall k2 k1 v1 v2.
(Eq k2, Hashable k2) =>
(k1 -> k2) -> (v1 -> v2) -> HashMap k1 v1 -> HashMap k2 v2
mapKeyVal k1 -> k2
fk v -> v
forall a. a -> a
id
{-# INLINE mapKey #-}

-- | Transform the keys of a 'M.Map'.
mapKeyO :: (Ord k2) => (k1 -> k2) -> M.Map k1 v -> M.Map k2 v
mapKeyO :: forall k2 k1 v. Ord k2 => (k1 -> k2) -> Map k1 v -> Map k2 v
mapKeyO k1 -> k2
fk = (k1 -> k2) -> (v -> v) -> Map k1 v -> Map k2 v
forall k2 k1 v1 v2.
Ord k2 =>
(k1 -> k2) -> (v1 -> v2) -> Map k1 v1 -> Map k2 v2
mapKeyValO k1 -> k2
fk v -> v
forall a. a -> a
id
{-# INLINE mapKeyO #-}