Skip to content

Commit bc1bec3

Browse files
Adding many more MonoTraversableWithKey i nstances.
1 parent 121fdfd commit bc1bec3

File tree

1 file changed

+37
-52
lines changed

1 file changed

+37
-52
lines changed

src/Data/MonoTraversable/Keys.hs

Lines changed: 37 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,7 @@ module Data.MonoTraversable.Keys where
2828
import Control.Applicative
2929
import Control.Category
3030
import Control.Comonad.Cofree
31-
#if MIN_VERSION_base(4,8,0)
32-
import Control.Monad (Monad (..))
33-
#else
3431
import Control.Monad (Monad (..), liftM)
35-
#endif
3632
import Control.Monad.Free
3733
import qualified Data.ByteString as BS
3834
import qualified Data.ByteString.Lazy as BSL
@@ -102,6 +98,8 @@ import Control.Monad.Trans.Identity (IdentityT)
10298
import GHC.Generics
10399
import Data.MonoTraversable (Element, MonoFunctor(..), MonoFoldable(..), MonoTraversable(..))
104100
import Data.Vector.Instances
101+
import Data.Semigroup (Dual(..), Endo(..))
102+
105103

106104
-- |
107105
-- Type family for getting the type of the key of a monomorphic container.
@@ -197,7 +195,7 @@ class MonoFunctor mono => MonoZip mono where
197195
{-# MINIMAL ozipWith #-}
198196

199197
ozipWith :: (Element mono -> Element mono -> Element mono) -> mono -> mono -> mono
200-
ozipWith f a b = uncurry f <$> ozip a b
198+
-- ozipWith f a b = uncurry f <$> ozip a b
201199

202200

203201
-- |
@@ -207,7 +205,7 @@ class (MonoKeyed mono, MonoZip mono) => MonoZipWithKey mono where
207205
{-# MINIMAL ozipWithKey #-}
208206

209207
ozipWithKey :: (MonoKey mono -> Element mono -> Element mono -> Element mono) -> mono -> mono -> mono
210-
ozipWithKey f = ozap . omapWithKey f
208+
-- ozipWithKey f = ozap . omapWithKey f
211209

212210

213211
-- |
@@ -267,7 +265,7 @@ class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTr
267265
-- |
268266
-- Map each key-element pair of a monomorphic container to an action,
269267
-- evaluate these actions from left to right, and collect the results.
270-
{-# INLINE otraverseWithKey #-}
268+
-- {-# INLINE otraverseWithKey #-}
271269
otraverseWithKey :: Applicative f => (MonoKey mono -> Element mono -> f (Element mono)) -> mono -> f mono
272270

273271
-- |
@@ -281,42 +279,39 @@ class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTr
281279

282280

283281
instance MonoTraversableWithKey BS.ByteString where
284-
285-
otraverseWithKey f = fmap BS.pack . traverse f . BS.unpack
286282
{-# INLINE otraverseWithKey #-}
287-
#if !MIN_VERSION_base(4,8,0)
288-
omapWithKeyM f = liftM BS.pack . mapM f . BS.unpack
289283
{-# INLINE omapWithKeyM #-}
290-
#endif
284+
285+
otraverseWithKey f = fmap BS.pack . traverseWithKey f . BS.unpack
291286

287+
omapWithKeyM f = liftM BS.pack . mapWithKeyM f . BS.unpack
292288

293-
instance MonoTraversableWithKey BSL.ByteString where
294289

295-
otraverseWithKey f = fmap BSL.pack . traverse f . BSL.unpack
290+
instance MonoTraversableWithKey BSL.ByteString where
296291
{-# INLINE otraverseWithKey #-}
297-
#if !MIN_VERSION_base(4,8,0)
298-
omapWithKeyM f = liftM BSL.pack . mapM f . BSL.unpack
299-
#endif
292+
{-# INLINE omapWithKeyM #-}
300293

294+
otraverseWithKey f = fmap BSL.pack . traverseWithKey f . BSL.unpack
295+
296+
omapWithKeyM f = liftM BSL.pack . mapWithKeyM f . BSL.unpack
301297

302-
instance MonoTraversableWithKey T.Text where
303298

304-
otraverseWithKey f = fmap T.pack . traverse f . T.unpack
299+
instance MonoTraversableWithKey T.Text where
305300
{-# INLINE otraverseWithKey #-}
306-
#if !MIN_VERSION_base(4,8,0)
307-
omapWithKeyM f = liftM T.pack . mapM f . T.unpack
308301
{-# INLINE omapWithKeyM #-}
309-
#endif
310302

303+
otraverseWithKey f = fmap T.pack . traverseWithKey f . T.unpack
304+
305+
omapWithKeyM f = liftM T.pack . mapWithKeyM f . T.unpack
311306

312-
instance MonoTraversableWithKey TL.Text where
313307

314-
otraverseWithKey f = fmap TL.pack . traverse f . TL.unpack
308+
instance MonoTraversableWithKey TL.Text where
315309
{-# INLINE otraverseWithKey #-}
316-
#if !MIN_VERSION_base(4,8,0)
317-
omapWithKeyM f = liftM TL.pack . mapM f . TL.unpack
318310
{-# INLINE omapWithKeyM #-}
319-
#endif
311+
312+
otraverseWithKey f = fmap TL.pack . traverseWithKey f . TL.unpack
313+
314+
omapWithKeyM f = liftM TL.pack . mapWithKeyM f . TL.unpack
320315

321316

322317
instance MonoTraversableWithKey [a]
@@ -359,42 +354,32 @@ instance MonoTraversableWithKey (Vector a)
359354

360355

361356
instance U.Unbox a => MonoTraversableWithKey (U.Vector a) where
362-
363-
-- FIXME do something more efficient
364-
otraverseWithKey f = fmap U.fromList . traverse f . U.toList
365-
#if MIN_VERSION_base(4,8,0)
366-
omapWithKeyM = otraverseWithKey
367-
#else
368-
omapWithKeyM = U.mapM
369-
#endif
370357
{-# INLINE otraverseWithKey #-}
371358
{-# INLINE omapWithKeyM #-}
372359

360+
otraverseWithKey f v = fmap (U.fromListN (U.length v)) . traverseWithKey f $ U.toList v
373361

374-
instance VS.Storable a => MonoTraversableWithKey (VS.Vector a) where
375-
376-
-- FIXME do something more efficient
377-
otraverseWithKey f = fmap VS.fromList . traverse f . VS.toList
378-
#if MIN_VERSION_base(4,8,0)
379362
omapWithKeyM = otraverseWithKey
380-
#else
381-
omapWithKeyM = VS.mapM
382-
#endif
363+
364+
365+
instance VS.Storable a => MonoTraversableWithKey (VS.Vector a) where
383366
{-# INLINE otraverseWithKey #-}
384367
{-# INLINE omapWithKeyM #-}
368+
369+
otraverseWithKey f v = fmap (VS.fromListN (VS.length v)) . traverseWithKey f $ VS.toList v
370+
371+
omapWithKeyM = otraverseWithKey
372+
373+
385374
instance MonoTraversableWithKey (Either a b) where
386-
otraverseWithKey _ (Left a) = pure (Left a)
387-
otraverseWithKey f (Right b) = fmap Right (f b)
388-
#if MIN_VERSION_base(4,8,0)
389-
omapWithKeyM _ (Left a) = pure (Left a)
390-
omapWithKeyM f (Right b) = fmap Right (f b)
391-
#else
392-
omapWithKeyM _ (Left a) = return (Left a)
393-
omapWithKeyM f (Right b) = liftM Right (f b)
394-
#endif
395375
{-# INLINE otraverseWithKey #-}
396376
{-# INLINE omapWithKeyM #-}
397377

378+
otraverseWithKey _ (Left a) = pure $ Left a
379+
otraverseWithKey f (Right b) = fmap Right $ f () b
380+
381+
omapWithKeyM = otraverseWithKey
382+
398383

399384
instance MonoTraversableWithKey (a, b)
400385

0 commit comments

Comments
 (0)