{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE DefaultSignatures #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module Data.MonoTraversable.Keys
( MonoKey
, MonoKeyed(..)
, MonoFoldableWithKey(..)
, MonoTraversableWithKey(..)
, MonoAdjustable(..)
, MonoZip(..)
, MonoZipWithKey(..)
, MonoIndexable(..)
, MonoLookup(..)
, ofoldlWithKeyUnwrap
, ofoldWithKeyMUnwrap
) where
import Control.Applicative
import Control.Arrow (Arrow)
import Control.Monad (Monad (..))
import Control.Monad.Trans.Cont (ContT)
import Control.Monad.Trans.Identity (IdentityT)
import Control.Monad.Trans.List (ListT(..))
import Control.Monad.Trans.Maybe (MaybeT(..))
import Control.Monad.Trans.Reader (ReaderT)
import Control.Monad.Trans.RWS (RWST(..))
import qualified Control.Monad.Trans.RWS.Strict as S (RWST(..))
import Control.Monad.Trans.State (StateT(..))
import qualified Control.Monad.Trans.State.Strict as S (StateT(..), evalState, get, modify)
import Control.Monad.Trans.Writer (WriterT)
import qualified Control.Monad.Trans.Writer.Strict as S (WriterT)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as BSL
import Data.Foldable
import Data.Functor.Compose (Compose(..))
import Data.Functor.Identity (Identity)
import Data.Functor.Product (Product(..))
import Data.Hashable
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HM
import Data.HashSet (HashSet)
import qualified Data.HashSet as HS
import Data.Int (Int)
import Data.IntMap (IntMap)
import qualified Data.IntMap as IM
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import Data.Key
import Data.List.NonEmpty (NonEmpty(..))
import Data.Map (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Monoid (Monoid(..))
import Data.MonoTraversable (Element, MonoFoldable(..), MonoFunctor(..), MonoTraversable(..))
import Data.Semigroup (Semigroup(..), Arg(..), Dual(..), Endo(..), Option(..))
import Data.Sequence (Seq, ViewL(..), ViewR(..))
import qualified Data.Sequence as Seq
import Data.Set (Set)
import qualified Data.Set as Set
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL
import Data.Tree (Tree(..))
import Data.Vector (Vector)
import qualified Data.Vector as V
import Data.Vector.Instances ()
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Storable.Mutable as VSM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Prelude hiding (lookup, zipWith)
type family MonoKey key
type instance MonoKey (r -> a) = ()
type instance MonoKey [a] = Int
type instance MonoKey (a, b) = ()
type instance MonoKey (Arg a b) = ()
type instance MonoKey BS.ByteString = Int
type instance MonoKey BSL.ByteString = Int
type instance MonoKey (Compose f g a) = (MonoKey (f a), MonoKey (g a))
type instance MonoKey (Const m a) = ()
type instance MonoKey (ContT r m a) = ()
type instance MonoKey (Either a b) = ()
type instance MonoKey (HashMap k v) = k
type instance MonoKey (HashSet e) = Int
type instance MonoKey (Identity a) = ()
type instance MonoKey (IdentityT m a) = ()
type instance MonoKey (IntMap a) = Int
type instance MonoKey IntSet = Int
type instance MonoKey (IO a) = ()
type instance MonoKey (ListT m a) = Int
type instance MonoKey (Map k v) = k
type instance MonoKey (Maybe a) = ()
type instance MonoKey (MaybeT m a) = ()
type instance MonoKey (NonEmpty a) = Int
type instance MonoKey (Option a) = ()
type instance MonoKey (Product f g a) = Either (Key f) (Key g)
type instance MonoKey (ReaderT r m a) = (r, Key m)
type instance MonoKey (RWST r w s m a) = ()
type instance MonoKey (S.RWST r w s m a) = ()
type instance MonoKey (Seq a) = Int
type instance MonoKey (Set e) = Int
type instance MonoKey (StateT s m a) = ()
type instance MonoKey (S.StateT s m a) = ()
type instance MonoKey T.Text = Int
type instance MonoKey TL.Text = Int
type instance MonoKey (Tree a) = Seq Int
type instance MonoKey (Vector a) = Int
type instance MonoKey (VU.Vector a) = Int
type instance MonoKey (VS.Vector a) = Int
type instance MonoKey (ViewL a) = ()
type instance MonoKey (ViewR a) = ()
type instance MonoKey (WrappedArrow a b c) = ()
type instance MonoKey (WrappedMonad m a) = ()
type instance MonoKey (WriterT w m a) = ()
type instance MonoKey (S.WriterT w m a) = ()
type instance MonoKey (ZipList a) = Int
class MonoFunctor mono => MonoKeyed mono where
{-# INLINE omapWithKey #-}
omapWithKey :: (MonoKey mono -> Element mono -> Element mono) -> mono -> mono
default omapWithKey :: (Keyed f, Element (f a) ~ a, MonoKey (f a) ~ Key f, f a ~ mono)
=> (MonoKey mono -> Element mono -> Element mono) -> mono -> mono
omapWithKey = mapWithKey
class MonoFoldable mono => MonoFoldableWithKey mono where
{-# MINIMAL ofoldMapWithKey | ofoldlWithKey #-}
otoKeyedList :: mono -> [(MonoKey mono, Element mono)]
otoKeyedList = ofoldrWithKey (\k v t -> (k,v):t) []
ofoldMapWithKey :: Monoid m => (MonoKey mono -> Element mono -> m) -> mono -> m
ofoldMapWithKey f = ofoldlWithKey (\a k v -> mappend (f k v) a) mempty
ofoldrWithKey :: (MonoKey mono -> Element mono -> a -> a) -> a -> mono -> a
ofoldrWithKey f z t = appEndo (ofoldMapWithKey (\k v -> Endo (f k v)) t) z
ofoldlWithKey :: (a -> MonoKey mono -> Element mono -> a) -> a -> mono -> a
ofoldlWithKey f z t = appEndo (getDual (ofoldMapWithKey (\k a -> Dual (Endo (\b -> f b k a))) t)) z
class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTraversableWithKey mono where
{-# MINIMAL otraverseWithKey #-}
otraverseWithKey :: Applicative f => (MonoKey mono -> Element mono -> f (Element mono)) -> mono -> f mono
default otraverseWithKey :: (Applicative f, TraversableWithKey t, Element (t a) ~ a, MonoKey (t a) ~ Key t, t a ~ mono)
=> (MonoKey mono -> Element mono -> f (Element mono)) -> mono -> f mono
otraverseWithKey = traverseWithKey
{-# INLINE omapWithKeyM #-}
omapWithKeyM :: Monad m => (MonoKey mono -> Element mono -> m (Element mono)) -> mono-> m mono
omapWithKeyM f = unwrapMonad . otraverseWithKey (fmap WrapMonad . f)
class MonoLookup mono where
{-# MINIMAL olookup #-}
olookup :: MonoKey mono -> mono -> Maybe (Element mono)
default olookup :: (Lookup f, Element (f a) ~ a, MonoKey (f a) ~ Key f, f a ~ mono)
=> MonoKey mono -> mono -> Maybe (Element mono)
olookup = lookup
class MonoLookup mono => MonoIndexable mono where
{-# MINIMAL oindex #-}
oindex :: mono -> MonoKey mono -> Element mono
default oindex :: (Indexable f, Element (f a) ~ a, MonoKey (f a) ~ Key f, f a ~ mono)
=> mono -> MonoKey mono -> Element mono
oindex = index
class MonoFunctor mono => MonoAdjustable mono where
{-# MINIMAL oadjust #-}
oadjust :: (Element mono -> Element mono) -> MonoKey mono -> mono -> mono
default oadjust :: (Adjustable f, Element (f a) ~ a, MonoKey (f a) ~ Key f, f a ~ mono)
=> (Element mono -> Element mono) -> MonoKey mono -> mono -> mono
oadjust = adjust
oreplace :: MonoKey mono -> Element mono -> mono -> mono
oreplace k v = oadjust (const v) k
class MonoFunctor mono => MonoZip mono where
{-# MINIMAL ozipWith #-}
ozipWith :: (Element mono -> Element mono -> Element mono) -> mono -> mono -> mono
class (MonoKeyed mono, MonoZip mono) => MonoZipWithKey mono where
{-# MINIMAL ozipWithKey #-}
ozipWithKey :: (MonoKey mono -> Element mono -> Element mono -> Element mono) -> mono -> mono -> mono
instance MonoKeyed (r -> a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed [a]
instance MonoKeyed (a, b) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed (Arg a b) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed BS.ByteString where
{-# INLINE omapWithKey #-}
omapWithKey f = snd . BS.mapAccumL g 0
where
g k v = (succ k, f k v)
instance MonoKeyed BSL.ByteString where
{-# INLINE omapWithKey #-}
omapWithKey f = snd . BSL.mapAccumL g 0
where
g k v = (succ k, f k v)
instance ( Keyed f
, Keyed g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoKeyed (Compose f g a)
instance MonoKeyed (Const m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance Functor m => MonoKeyed (ContT r m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed (Either a b) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed (HashMap k v)
instance MonoKeyed (Identity a)
instance Functor m => MonoKeyed (IdentityT m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed (IntMap a)
instance MonoKeyed (IO a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance Functor m => MonoKeyed (ListT m a) where
{-# INLINE omapWithKey #-}
omapWithKey f = ListT . fmap (omapWithKey f) . runListT
instance MonoKeyed (Map k v)
instance MonoKeyed (Maybe a)
instance Functor m => MonoKeyed (MaybeT m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed (NonEmpty a)
instance MonoKeyed (Option a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance ( Keyed f
, Keyed g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoKeyed (Product f g a)
instance Keyed m => MonoKeyed (ReaderT r m a)
instance Functor m => MonoKeyed (RWST r w s m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance Functor m => MonoKeyed (S.RWST r w s m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed (Seq a)
instance Functor m => MonoKeyed (StateT s m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance Functor m => MonoKeyed (S.StateT s m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed T.Text where
{-# INLINE omapWithKey #-}
omapWithKey f = snd . T.mapAccumL g 0
where
g k v = (succ k, f k v)
instance MonoKeyed TL.Text where
{-# INLINE omapWithKey #-}
omapWithKey f = snd . TL.mapAccumL g 0
where
g k v = (succ k, f k v)
instance MonoKeyed (Tree a)
instance MonoKeyed (Vector a)
instance VU.Unbox a => MonoKeyed (VU.Vector a) where
{-# INLINE omapWithKey #-}
omapWithKey = VU.imap
instance VS.Storable a => MonoKeyed (VS.Vector a) where
{-# INLINE omapWithKey #-}
omapWithKey = VS.imap
instance MonoKeyed (ViewL a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed (ViewR a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance Arrow a => MonoKeyed (WrappedArrow a b c) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance Monad m => MonoKeyed (WrappedMonad m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance Functor m => MonoKeyed (WriterT w m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance Functor m => MonoKeyed (S.WriterT w m a) where
{-# INLINE omapWithKey #-}
omapWithKey = omapWithUnitKey
instance MonoKeyed (ZipList a)
instance MonoFoldableWithKey [a] where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance MonoFoldableWithKey (a, b) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance MonoFoldableWithKey BS.ByteString where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance MonoFoldableWithKey BSL.ByteString where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance ( FoldableWithKey f
, FoldableWithKey g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoFoldableWithKey (Compose f g a) where
{-# INLINE ofoldMapWithKey #-}
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldMapWithKey = foldMapWithKey
ofoldrWithKey = foldrWithKey
ofoldlWithKey = foldlWithKey
instance MonoFoldableWithKey (Const m a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance MonoFoldableWithKey (Either a b) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance MonoFoldableWithKey (HashMap k v) where
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldrWithKey = HM.foldrWithKey
ofoldlWithKey = HM.foldlWithKey'
instance MonoFoldableWithKey (HashSet v) where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance MonoFoldableWithKey (Identity a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance Foldable f => MonoFoldableWithKey (IdentityT f a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance MonoFoldableWithKey (IntMap a) where
{-# INLINE ofoldMapWithKey #-}
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldMapWithKey = IM.foldMapWithKey
ofoldrWithKey = IM.foldrWithKey
ofoldlWithKey = IM.foldlWithKey'
instance MonoFoldableWithKey IntSet where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance Foldable f => MonoFoldableWithKey (ListT f a) where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance MonoFoldableWithKey (Map k v) where
{-# INLINE ofoldMapWithKey #-}
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldMapWithKey = Map.foldMapWithKey
ofoldrWithKey = Map.foldrWithKey
ofoldlWithKey = Map.foldlWithKey'
instance MonoFoldableWithKey (Maybe a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance Foldable f => MonoFoldableWithKey (MaybeT f a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance MonoFoldableWithKey (NonEmpty a) where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance MonoFoldableWithKey (Option a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance ( FoldableWithKey f
, FoldableWithKey g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoFoldableWithKey (Product f g a) where
{-# INLINE ofoldMapWithKey #-}
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldMapWithKey = foldMapWithKey
ofoldrWithKey = foldrWithKey
ofoldlWithKey = foldlWithKey
instance MonoFoldableWithKey (Seq a) where
{-# INLINE ofoldMapWithKey #-}
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldMapWithKey = foldMapWithKey
ofoldrWithKey = Seq.foldrWithIndex
ofoldlWithKey = Seq.foldlWithIndex
instance Ord e => MonoFoldableWithKey (Set e) where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance MonoFoldableWithKey T.Text where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance MonoFoldableWithKey TL.Text where
{-# INLINE ofoldlWithKey #-}
ofoldlWithKey = monoFoldableWithIntegralKey
instance MonoFoldableWithKey (Tree a) where
{-# INLINE ofoldMapWithKey #-}
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldMapWithKey = foldMapWithKey
ofoldrWithKey = foldrWithKey
ofoldlWithKey = foldlWithKey
instance MonoFoldableWithKey (Vector a) where
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldrWithKey = V.ifoldr
ofoldlWithKey = V.ifoldl'
instance VU.Unbox a => MonoFoldableWithKey (VU.Vector a) where
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldrWithKey = VU.ifoldr
ofoldlWithKey = VU.ifoldl'
instance VS.Storable a => MonoFoldableWithKey (VS.Vector a) where
{-# INLINE ofoldrWithKey #-}
{-# INLINE ofoldlWithKey #-}
ofoldrWithKey = VS.ifoldr
ofoldlWithKey = VS.ifoldl'
instance MonoFoldableWithKey (ViewL a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance MonoFoldableWithKey (ViewR a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance Foldable f => MonoFoldableWithKey (WriterT w f a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance Foldable f => MonoFoldableWithKey (S.WriterT w f a) where
{-# INLINE ofoldMapWithKey #-}
ofoldMapWithKey = monoFoldableWithUnitKey
instance MonoTraversableWithKey [a] where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance MonoTraversableWithKey (a, b) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance MonoTraversableWithKey BS.ByteString where
{-# INLINE otraverseWithKey #-}
{-# INLINE omapWithKeyM #-}
otraverseWithKey f = fmap BS.pack . traverseWithKey f . BS.unpack
omapWithKeyM f = fmap BS.pack . mapWithKeyM f . BS.unpack
instance MonoTraversableWithKey BSL.ByteString where
{-# INLINE otraverseWithKey #-}
{-# INLINE omapWithKeyM #-}
otraverseWithKey f = fmap BSL.pack . traverseWithKey f . BSL.unpack
omapWithKeyM f = fmap BSL.pack . mapWithKeyM f . BSL.unpack
instance ( MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
, TraversableWithKey f
, TraversableWithKey g
) => MonoTraversableWithKey (Compose f g a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance MonoTraversableWithKey (Const m a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance MonoTraversableWithKey (Either a b) where
{-# INLINE otraverseWithKey #-}
{-# INLINE omapWithKeyM #-}
otraverseWithKey _ (Left a) = pure $ Left a
otraverseWithKey f (Right b) = Right <$> f () b
omapWithKeyM = otraverseWithKey
instance MonoTraversableWithKey (HashMap k v) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance MonoTraversableWithKey (Identity a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance Traversable f => MonoTraversableWithKey (IdentityT f a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance MonoTraversableWithKey (IntMap a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance Traversable f => MonoTraversableWithKey (ListT f a) where
otraverseWithKey f = fmap ListT . traverse (traverseWithKey f) . runListT
instance MonoTraversableWithKey (Map k v) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance MonoTraversableWithKey (Maybe a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance Traversable f => MonoTraversableWithKey (MaybeT f a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance MonoTraversableWithKey (NonEmpty a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance MonoTraversableWithKey (Option a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance ( MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
, TraversableWithKey f
, TraversableWithKey g
) => MonoTraversableWithKey (Product f g a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance MonoTraversableWithKey (Seq a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance MonoTraversableWithKey T.Text where
{-# INLINE otraverseWithKey #-}
{-# INLINE omapWithKeyM #-}
otraverseWithKey f = fmap T.pack . traverseWithKey f . T.unpack
omapWithKeyM f = fmap T.pack . mapWithKeyM f . T.unpack
instance MonoTraversableWithKey TL.Text where
{-# INLINE otraverseWithKey #-}
{-# INLINE omapWithKeyM #-}
otraverseWithKey f = fmap TL.pack . traverseWithKey f . TL.unpack
omapWithKeyM f = fmap TL.pack . mapWithKeyM f . TL.unpack
instance MonoTraversableWithKey (Tree a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance MonoTraversableWithKey (Vector a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = traverseWithKey
instance VU.Unbox a => MonoTraversableWithKey (VU.Vector a) where
{-# INLINE otraverseWithKey #-}
{-# INLINE omapWithKeyM #-}
otraverseWithKey f v = fmap (VU.fromListN (VU.length v)) . traverseWithKey f $ VU.toList v
omapWithKeyM = otraverseWithKey
instance VS.Storable a => MonoTraversableWithKey (VS.Vector a) where
{-# INLINE otraverseWithKey #-}
{-# INLINE omapWithKeyM #-}
otraverseWithKey f v = fmap (VS.fromListN (VS.length v)) . traverseWithKey f $ VS.toList v
omapWithKeyM = otraverseWithKey
instance MonoTraversableWithKey (ViewL a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance MonoTraversableWithKey (ViewR a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance Traversable f => MonoTraversableWithKey (WriterT w f a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance Traversable f => MonoTraversableWithKey (S.WriterT w f a) where
{-# INLINE otraverseWithKey #-}
otraverseWithKey = monoTraversableWithUnitKey
instance MonoLookup [a] where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (a, b) where
{-# INLINE olookup #-}
olookup _ (_, v) = Just v
instance MonoLookup (Arg a b) where
{-# INLINE olookup #-}
olookup _ (Arg _ v) = Just v
instance MonoLookup BS.ByteString where
{-# INLINE olookup #-}
olookup i bs
| i < 0
|| i >= BS.length bs = Nothing
| otherwise = Just $ BS.index bs i
instance MonoLookup BSL.ByteString where
{-# INLINE olookup #-}
olookup i bs
| i < 0
|| i >= fromEnum (BSL.length bs) = Nothing
| otherwise = Just . BSL.index bs $ toEnum i
instance ( Lookup f
, Lookup g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoLookup (Compose f g a) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (Either a b) where
{-# INLINE olookup #-}
olookup _ (Left _) = Nothing
olookup _ (Right v) = Just v
instance (Eq k, Hashable k) => MonoLookup (HashMap k v) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (HashSet v) where
{-# INLINE olookup #-}
olookup = monoLookupFoldable
instance MonoLookup (Identity a) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (IntMap a) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup IntSet where
{-# INLINE olookup #-}
olookup = monoLookupFoldable
instance Ord k => MonoLookup (Map k v) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (Maybe a) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (NonEmpty a) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (Option a) where
{-# INLINE olookup #-}
olookup = const getOption
instance ( Lookup f
, Lookup g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoLookup (Product f g a) where
{-# INLINE olookup #-}
olookup = lookup
instance Lookup m => MonoLookup (ReaderT r m a) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (Seq a) where
{-# INLINE olookup #-}
olookup = lookup
instance Ord a => MonoLookup (Set a) where
{-# INLINE olookup #-}
olookup = monoLookupFoldable
instance MonoLookup T.Text where
{-# INLINE olookup #-}
olookup i ts
| i < 0
|| i >= T.length ts = Nothing
| otherwise = Just $ T.index ts i
instance MonoLookup TL.Text where
{-# INLINE olookup #-}
olookup i ts
| i < 0
|| i >= fromEnum (TL.length ts) = Nothing
| otherwise = Just . TL.index ts $ toEnum i
instance MonoLookup (Tree a) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoLookup (Vector a) where
{-# INLINE olookup #-}
olookup = lookup
instance VU.Unbox a => MonoLookup (VU.Vector a) where
{-# INLINE olookup #-}
olookup = flip (VU.!?)
instance VS.Storable a => MonoLookup (VS.Vector a) where
{-# INLINE olookup #-}
olookup = flip (VS.!?)
instance MonoLookup (ViewL a) where
{-# INLINE olookup #-}
olookup _ EmptyL = Nothing
olookup _ (v:<_) = Just v
instance MonoLookup (ViewR a) where
{-# INLINE olookup #-}
olookup _ EmptyR = Nothing
olookup _ (_:>v) = Just v
instance MonoLookup (ZipList a) where
{-# INLINE olookup #-}
olookup = lookup
instance MonoIndexable [a] where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (a, b) where
{-# INLINE oindex #-}
oindex (_, v) = const v
instance MonoIndexable (Arg a b) where
{-# INLINE oindex #-}
oindex (Arg _ v) = const v
instance MonoIndexable BS.ByteString where
{-# INLINE oindex #-}
oindex bs i
| i < 0
|| i >= BS.length bs = error $ mconcat [ "oindex on ByteString at point ", show i, " is outside the range: [0, ", show (BS.length bs - 1), "]."]
| otherwise = BS.index bs i
instance MonoIndexable BSL.ByteString where
{-# INLINE oindex #-}
oindex bs i
| i < 0
|| i >= fromEnum (BSL.length bs) = error $ mconcat [ "oindex on Lazy ByteString at point ", show i, " is outside the range: [0, ", show (BSL.length bs - 1), "]."]
| otherwise = BSL.index bs $ toEnum i
instance ( Indexable f
, Indexable g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoIndexable (Compose f g a) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (Either a b) where
{-# INLINE oindex #-}
oindex (Right v) = const v
oindex (Left _) = error
"oindex on Either is Left, cannot retreive a value. Consider using olookup instead."
instance (Eq k, Hashable k) => MonoIndexable (HashMap k v) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (HashSet v) where
{-# INLINE oindex #-}
oindex hs i = fromMaybe errorMessage $ olookup i hs
where
errorMessage = error $ mconcat
[ "oindex on HashSet at point "
, show i
, " is outside the range: [0, "
, show (HS.size hs - 1)
, "]."
]
instance MonoIndexable (Identity a) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (IntMap a) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable IntSet where
{-# INLINE oindex #-}
oindex is i = fromMaybe errorMessage $ olookup i is
where
errorMessage = error $ mconcat
[ "oindex on IntSet at point "
, show i
, " is outside the range: [0, "
, show (IS.size is - 1)
, "]."
]
instance Ord k => MonoIndexable (Map k v) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (Maybe a) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (NonEmpty a) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (Option a) where
{-# INLINE oindex #-}
oindex = flip . const $ fromMaybe errorMessage . getOption
where
errorMessage = error
"oindex on empty Option, cannot retreive a value. Consider using olookup instead."
instance ( Indexable f
, Indexable g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoIndexable (Product f g a) where
{-# INLINE oindex #-}
oindex = index
instance Indexable m => MonoIndexable (ReaderT r m a) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (Seq a) where
{-# INLINE oindex #-}
oindex = index
instance Ord a => MonoIndexable (Set a) where
{-# INLINE oindex #-}
oindex s i = fromMaybe errorMessage $ olookup i s
where
errorMessage = error $ mconcat
[ "oindex on Set at point "
, show i
, " is outside the range: [0, "
, show (Set.size s - 1)
, "]."
]
instance MonoIndexable T.Text where
{-# INLINE oindex #-}
oindex ts i
| i < 0
|| i >= T.length ts = error $ mconcat [ "oindex on Text at point ", show i, " is outside the range: [0, ", show (T.length ts - 1), "]."]
| otherwise = T.index ts i
instance MonoIndexable TL.Text where
{-# INLINE oindex #-}
oindex ts i
| i < 0
|| i >= fromEnum (TL.length ts) = error $ mconcat [ "oindex on Lazy Text at point ", show i, " is outside the range: [0, ", show (TL.length ts - 1), "]."]
| otherwise = TL.index ts $ toEnum i
instance MonoIndexable (Tree a) where
{-# INLINE oindex #-}
oindex = index
instance MonoIndexable (Vector a) where
{-# INLINE oindex #-}
oindex = index
instance VU.Unbox a => MonoIndexable (VU.Vector a) where
{-# INLINE oindex #-}
oindex = (VU.!)
instance VS.Storable a => MonoIndexable (VS.Vector a) where
{-# INLINE oindex #-}
oindex = (VS.!)
instance MonoIndexable (ViewL a) where
{-# INLINE oindex #-}
oindex (v:<_) = const v
oindex EmptyL = error
"oindex on ViewL is EmptyL, cannot retreive a value. Consider using olookup instead."
instance MonoIndexable (ViewR a) where
{-# INLINE oindex #-}
oindex (_:>v) = const v
oindex EmptyR = error
"oindex on ViewR is EmptyR, cannot retreive a value. Consider using olookup instead."
instance MonoIndexable (ZipList a) where
{-# INLINE oindex #-}
oindex = index
instance MonoAdjustable (r -> a) where
{-# INLINE oadjust #-}
oadjust f _ g = f . g
instance MonoAdjustable [a] where
{-# INLINE oadjust #-}
oadjust = adjust
instance MonoAdjustable (a, b) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable (Arg a b) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable BS.ByteString where
{-# INLINE oadjust #-}
oadjust f i bs
| i < 0
|| i >= BS.length bs = bs
| otherwise = snd $ BS.mapAccumL g 0 bs
where
g k v = (succ k, if k == i then f v else v)
instance MonoAdjustable BSL.ByteString where
{-# INLINE oadjust #-}
oadjust f i bs
| i < 0
|| i >= fromEnum (BSL.length bs) = bs
| otherwise = snd $ BSL.mapAccumL g 0 bs
where
g k v = (succ k, if k == i then f v else v)
instance MonoAdjustable (Const m a) where
{-# INLINE oadjust #-}
oadjust = const $ const id
instance Functor m => MonoAdjustable (ContT r m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable (Either a b) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance (Eq k, Hashable k) => MonoAdjustable (HashMap k v) where
{-# INLINE oadjust #-}
oadjust = HM.adjust
instance MonoAdjustable (Identity a) where
{-# INLINE oadjust #-}
oadjust = adjust
instance Functor m => MonoAdjustable (IdentityT m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable (IntMap a) where
{-# INLINE oadjust #-}
oadjust = IM.adjust
instance MonoAdjustable (IO a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Functor m => MonoAdjustable (ListT m a) where
{-# INLINE oadjust #-}
oadjust f i = ListT . fmap (adjust f i) . runListT
instance Ord k => MonoAdjustable (Map k v) where
{-# INLINE oadjust #-}
oadjust = Map.adjust
instance MonoAdjustable (Maybe a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Functor m => MonoAdjustable (MaybeT m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable (NonEmpty a) where
{-# INLINE oadjust #-}
oadjust = adjust
instance MonoAdjustable (Option a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance ( Adjustable f
, Adjustable g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoAdjustable (Product f g a) where
{-# INLINE oadjust #-}
oadjust = adjust
instance Functor m => MonoAdjustable (ReaderT r m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Functor m => MonoAdjustable (RWST r w s m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Functor m => MonoAdjustable (S.RWST r w s m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable (Seq a) where
{-# INLINE oadjust #-}
oadjust = adjust
instance Functor m => MonoAdjustable (StateT s m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Functor m => MonoAdjustable (S.StateT s m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable T.Text where
{-# INLINE oadjust #-}
oadjust f i ts
| i < 0
|| i >= T.length ts = ts
| otherwise = snd $ T.mapAccumL g 0 ts
where
g k v = (succ k, if k == i then f v else v)
instance MonoAdjustable TL.Text where
{-# INLINE oadjust #-}
oadjust f i ts
| i < 0
|| i >= fromEnum (TL.length ts) = ts
| otherwise = snd $ TL.mapAccumL g 0 ts
where
g k v = (succ k, if k == i then f v else v)
instance MonoAdjustable (Tree a) where
{-# INLINE oadjust #-}
oadjust = adjust
instance MonoAdjustable (Vector a) where
{-# INLINE oadjust #-}
oadjust = adjust
instance VU.Unbox a => MonoAdjustable (VU.Vector a) where
{-# INLINE oadjust #-}
oadjust f i = VU.modify $ \v -> VUM.modify v f i
instance VS.Storable a => MonoAdjustable (VS.Vector a) where
{-# INLINE oadjust #-}
oadjust f i = VS.modify $ \v -> VSM.modify v f i
instance MonoAdjustable (ViewL a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable (ViewR a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Arrow a => MonoAdjustable (WrappedArrow a b c) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Monad m => MonoAdjustable (WrappedMonad m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Functor m => MonoAdjustable (WriterT w m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance Functor m => MonoAdjustable (S.WriterT w m a) where
{-# INLINE oadjust #-}
oadjust f = const $ fmap f
instance MonoAdjustable (ZipList a) where
{-# INLINE oadjust #-}
oadjust = adjust
instance MonoZip (r -> a) where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance MonoZip [a] where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance MonoZip (a, b) where
{-# INLINE ozipWith #-}
ozipWith f (_, b1) (a, b2) = (a, f b1 b2)
instance MonoZip (Arg a b) where
{-# INLINE ozipWith #-}
ozipWith f (Arg _ b1) (Arg a b2) = Arg a $ f b1 b2
instance MonoZip BS.ByteString where
{-# INLINE ozipWith #-}
ozipWith f bs = BS.pack . BS.zipWith f bs
instance MonoZip BSL.ByteString where
{-# INLINE ozipWith #-}
ozipWith f bs = BSL.pack . BSL.zipWith f bs
instance ( Zip f
, Zip g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoZip (Compose f g a) where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance MonoZip (Const m a) where
{-# INLINE ozipWith #-}
ozipWith = const $ const id
instance Functor m => MonoZip (ContT r m a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance MonoZip (Either a b) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance (Eq k, Hashable k) => MonoZip (HashMap k v) where
{-# INLINE ozipWith #-}
ozipWith f x y = HM.intersectionWith f x y <> HM.difference x y <> HM.difference y x
instance MonoZip (Identity a) where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance Applicative m => MonoZip (IdentityT m a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance MonoZip (IntMap a) where
{-# INLINE ozipWith #-}
ozipWith f x y = IM.intersectionWith f x y <> IM.difference x y <> IM.difference y x
instance MonoZip (IO a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance Applicative m => MonoZip (ListT m a) where
{-# INLINE ozipWith #-}
ozipWith f x y = ListT $ zipWith f <$> runListT x <*> runListT y
instance Ord k => MonoZip (Map k v) where
{-# INLINE ozipWith #-}
ozipWith f x y = Map.intersectionWith f x y <> Map.difference x y <> Map.difference y x
instance MonoZip (Maybe a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance Applicative m => MonoZip (MaybeT m a) where
{-# INLINE ozipWith #-}
ozipWith f x y = MaybeT $ liftA2 f <$> runMaybeT x <*> runMaybeT y
instance MonoZip (NonEmpty a) where
{-# INLINE ozipWith #-}
ozipWith f (x:|xs) (y :|ys) = f x y :| zipWith f xs ys
instance MonoZip (Option a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance ( Zip f
, Zip g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoZip (Product f g a) where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance Applicative m => MonoZip (ReaderT r m a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance (Applicative m, Semigroup w) => MonoZip (RWST r w s m a) where
{-# INLINE ozipWith #-}
ozipWith f (RWST x) (RWST y) = RWST $ \r s ->
let g (a1, _, w1) (a2, _, w2) = (f a1 a2, s, w1 <> w2)
in g <$> x r s <*> y r s
instance (Applicative m, Semigroup w) => MonoZip (S.RWST r w s m a) where
{-# INLINE ozipWith #-}
ozipWith f (S.RWST x) (S.RWST y) = S.RWST $ \r s ->
let g (a1, _, w1) (a2, _, w2) = (f a1 a2, s, w1 <> w2)
in g <$> x r s <*> y r s
instance MonoZip (Seq a) where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance Applicative m => MonoZip (StateT s m a) where
{-# INLINE ozipWith #-}
ozipWith f (StateT x) (StateT y) = StateT $ \ s ->
let g (a1, _) (a2, _) = (f a1 a2, s)
in g <$> x s <*> y s
instance Applicative m => MonoZip (S.StateT s m a) where
{-# INLINE ozipWith #-}
ozipWith f (S.StateT x) (S.StateT y) = S.StateT $ \ s ->
let g (a1, _) (a2, _) = (f a1 a2, s)
in g <$> x s <*> y s
instance MonoZip T.Text where
{-# INLINE ozipWith #-}
ozipWith = T.zipWith
instance MonoZip TL.Text where
{-# INLINE ozipWith #-}
ozipWith = TL.zipWith
instance MonoZip (Tree a) where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance MonoZip (Vector a) where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance VU.Unbox a => MonoZip (VU.Vector a) where
{-# INLINE ozipWith #-}
ozipWith = VU.zipWith
instance VS.Storable a => MonoZip (VS.Vector a) where
{-# INLINE ozipWith #-}
ozipWith = VS.zipWith
instance MonoZip (ViewL a) where
{-# INLINE ozipWith #-}
ozipWith _ EmptyL _ = EmptyL
ozipWith _ _ EmptyL = EmptyL
ozipWith f (x:<xs) (y:<ys) = f x y :< Seq.zipWith f xs ys
instance MonoZip (ViewR a) where
{-# INLINE ozipWith #-}
ozipWith _ EmptyR _ = EmptyR
ozipWith _ _ EmptyR = EmptyR
ozipWith f (xs:>x) (ys:>y) = Seq.zipWith f xs ys :> f x y
instance Arrow a => MonoZip (WrappedArrow a b c) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance Monad m => MonoZip (WrappedMonad m a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance (Applicative m, Monoid w) => MonoZip (WriterT w m a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance (Applicative m, Monoid w) => MonoZip (S.WriterT w m a) where
{-# INLINE ozipWith #-}
ozipWith = liftA2
instance MonoZip (ZipList a) where
{-# INLINE ozipWith #-}
ozipWith = zipWith
instance MonoZipWithKey (r -> a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = zipWith (f ())
instance MonoZipWithKey [a] where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
instance MonoZipWithKey (a, b) where
{-# INLINE ozipWithKey #-}
ozipWithKey f (_, b1) (a, b2) = (a, f () b1 b2)
instance MonoZipWithKey (Arg a b) where
{-# INLINE ozipWithKey #-}
ozipWithKey f (Arg _ b1) (Arg a b2) = Arg a $ f () b1 b2
instance MonoZipWithKey BS.ByteString where
{-# INLINE ozipWithKey #-}
ozipWithKey f bs = BS.pack . zipWithKey f (BS.unpack bs) . BS.unpack
instance MonoZipWithKey BSL.ByteString where
{-# INLINE ozipWithKey #-}
ozipWithKey f bs = BSL.pack . zipWithKey f (BSL.unpack bs) . BSL.unpack
instance ( ZipWithKey f
, ZipWithKey g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoZipWithKey (Compose f g a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
instance MonoZipWithKey (Const m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = const $ const id
instance Functor m => MonoZipWithKey (ContT r m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 (f ())
instance MonoZipWithKey (Either a b) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 (f ())
instance (Eq k, Hashable k) => MonoZipWithKey (HashMap k v) where
{-# INLINE ozipWithKey #-}
ozipWithKey f x y = HM.intersectionWithKey f x y <> HM.difference x y <> HM.difference y x
instance MonoZipWithKey (Identity a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
instance Applicative m => MonoZipWithKey (IdentityT m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 (f ())
instance MonoZipWithKey (IntMap a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f x y = IM.intersectionWithKey f x y <> IM.difference x y <> IM.difference y x
instance MonoZipWithKey (IO a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 (f ())
instance Applicative m => MonoZipWithKey (ListT m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f x y = ListT $ zipWithKey f <$> runListT x <*> runListT y
instance Ord k => MonoZipWithKey (Map k v) where
{-# INLINE ozipWithKey #-}
ozipWithKey f x y = Map.intersectionWithKey f x y <> Map.difference x y <> Map.difference y x
instance MonoZipWithKey (Maybe a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 (f ())
instance Monad m => MonoZipWithKey (MaybeT m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 (f ())
instance MonoZipWithKey (NonEmpty a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
instance MonoZipWithKey (Option a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 (f ())
instance ( ZipWithKey f
, ZipWithKey g
, MonoKey (f a) ~ Key f
, MonoKey (g a) ~ Key g
) => MonoZipWithKey (Product f g a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
instance (Applicative m, ZipWithKey m) => MonoZipWithKey (ReaderT r m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
instance (Applicative m, Semigroup w) => MonoZipWithKey (RWST r w s m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f (RWST x) (RWST y) = RWST $ \r s ->
let g (a1, _, w1) (a2, _, w2) = (f () a1 a2, s, w1 <> w2)
in g <$> x r s <*> y r s
instance (Applicative m, Semigroup w) => MonoZipWithKey (S.RWST r w s m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f (S.RWST x) (S.RWST y) = S.RWST $ \r s ->
let g (a1, _, w1) (a2, _, w2) = (f () a1 a2, s, w1 <> w2)
in g <$> x r s <*> y r s
instance MonoZipWithKey (Seq a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
instance Applicative m => MonoZipWithKey (StateT s m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f (StateT x) (StateT y) = StateT $ \ s ->
let g (a1, _) (a2, _) = (f () a1 a2, s)
in g <$> x s <*> y s
instance Applicative m => MonoZipWithKey (S.StateT s m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f (S.StateT x) (S.StateT y) = S.StateT $ \ s ->
let g (a1, _) (a2, _) = (f () a1 a2, s)
in g <$> x s <*> y s
instance MonoZipWithKey T.Text where
{-# INLINE ozipWithKey #-}
ozipWithKey f ts = T.pack . zipWithKey f (T.unpack ts) . T.unpack
instance MonoZipWithKey TL.Text where
{-# INLINE ozipWithKey #-}
ozipWithKey f ts = TL.pack . zipWithKey f (TL.unpack ts) . TL.unpack
instance MonoZipWithKey (Tree a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
instance MonoZipWithKey (Vector a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = V.izipWith
instance VU.Unbox a => MonoZipWithKey (VU.Vector a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = VU.izipWith
instance VS.Storable a => MonoZipWithKey (VS.Vector a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = VS.izipWith
instance MonoZipWithKey (ViewL a) where
{-# INLINE ozipWithKey #-}
ozipWithKey _ EmptyL _ = EmptyL
ozipWithKey _ _ EmptyL = EmptyL
ozipWithKey f (x:<xs) (y:<ys) = f () x y :< Seq.fromList (zipWith (f ()) (toList xs) (toList ys))
instance MonoZipWithKey (ViewR a) where
{-# INLINE ozipWithKey #-}
ozipWithKey _ EmptyR _ = EmptyR
ozipWithKey _ _ EmptyR = EmptyR
ozipWithKey f (xs:>x) (ys:>y) = Seq.fromList (zipWith (f ()) (toList xs) (toList ys)) :> f () x y
instance Arrow a => MonoZipWithKey (WrappedArrow a b c) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 $ f ()
instance Monad m => MonoZipWithKey (WrappedMonad m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 $ f ()
instance (Applicative m, Monoid w) => MonoZipWithKey (WriterT w m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 $ f ()
instance (Applicative m, Monoid w) => MonoZipWithKey (S.WriterT w m a) where
{-# INLINE ozipWithKey #-}
ozipWithKey f = liftA2 $ f ()
instance MonoZipWithKey (ZipList a) where
{-# INLINE ozipWithKey #-}
ozipWithKey = zipWithKey
ofoldlWithKeyUnwrap :: MonoFoldableWithKey mono
=> (x -> Element mono -> x) -> x -> (x -> b) -> mono -> b
ofoldlWithKeyUnwrap f x unwrap mono = unwrap (ofoldl' f x mono)
ofoldWithKeyMUnwrap :: (Monad m, MonoFoldableWithKey mono)
=> (x -> Element mono -> m x) -> m x -> (x -> m b) -> mono -> m b
ofoldWithKeyMUnwrap f mx unwrap mono = do
x <- mx
x' <- ofoldlM f x mono
unwrap x'
omapWithUnitKey :: MonoFunctor mono => (() -> Element mono -> Element mono) -> mono -> mono
omapWithUnitKey f = omap (f ())
monoFoldableWithUnitKey :: (Monoid m, MonoFoldable mono) => (() -> Element mono -> m) -> mono -> m
monoFoldableWithUnitKey f = ofoldMap (f ())
monoFoldableWithIntegralKey
:: ( Integral i, MonoFoldable mono)
=> (a -> i -> Element mono -> a) -> a -> mono -> a
monoFoldableWithIntegralKey f z = (`S.evalState` 0) . ofoldlM g z
where
g a e = do
!k <- S.get
S.modify succ
pure $ f a k e
monoTraversableWithUnitKey
:: (Applicative f, MonoTraversable mono)
=> (() -> Element mono -> f (Element mono)) -> mono -> f mono
monoTraversableWithUnitKey f = otraverse (f ())
monoLookupFoldable :: (Integral i, MonoFoldable mono) => i -> mono -> Maybe (Element mono)
monoLookupFoldable i t
| i < 0 = Nothing
| otherwise = go i $ otoList t
where
go _ [] = Nothing
go 0 [x] = Just x
go !n (_:xs) = go (n-1) xs