@@ -28,11 +28,7 @@ module Data.MonoTraversable.Keys where
28
28
import Control.Applicative
29
29
import Control.Category
30
30
import Control.Comonad.Cofree
31
- #if MIN_VERSION_base(4,8,0)
32
- import Control.Monad (Monad (.. ))
33
- #else
34
31
import Control.Monad (Monad (.. ), liftM )
35
- #endif
36
32
import Control.Monad.Free
37
33
import qualified Data.ByteString as BS
38
34
import qualified Data.ByteString.Lazy as BSL
@@ -102,6 +98,8 @@ import Control.Monad.Trans.Identity (IdentityT)
102
98
import GHC.Generics
103
99
import Data.MonoTraversable (Element , MonoFunctor (.. ), MonoFoldable (.. ), MonoTraversable (.. ))
104
100
import Data.Vector.Instances
101
+ import Data.Semigroup (Dual (.. ), Endo (.. ))
102
+
105
103
106
104
-- |
107
105
-- Type family for getting the type of the key of a monomorphic container.
@@ -197,7 +195,7 @@ class MonoFunctor mono => MonoZip mono where
197
195
{-# MINIMAL ozipWith #-}
198
196
199
197
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
201
199
202
200
203
201
-- |
@@ -207,7 +205,7 @@ class (MonoKeyed mono, MonoZip mono) => MonoZipWithKey mono where
207
205
{-# MINIMAL ozipWithKey #-}
208
206
209
207
ozipWithKey :: (MonoKey mono -> Element mono -> Element mono -> Element mono ) -> mono -> mono -> mono
210
- ozipWithKey f = ozap . omapWithKey f
208
+ -- ozipWithKey f = ozap . omapWithKey f
211
209
212
210
213
211
-- |
@@ -267,7 +265,7 @@ class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTr
267
265
-- |
268
266
-- Map each key-element pair of a monomorphic container to an action,
269
267
-- evaluate these actions from left to right, and collect the results.
270
- {-# INLINE otraverseWithKey #-}
268
+ -- {-# INLINE otraverseWithKey #-}
271
269
otraverseWithKey :: Applicative f => (MonoKey mono -> Element mono -> f (Element mono )) -> mono -> f mono
272
270
273
271
-- |
@@ -281,42 +279,39 @@ class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTr
281
279
282
280
283
281
instance MonoTraversableWithKey BS. ByteString where
284
-
285
- otraverseWithKey f = fmap BS. pack . traverse f . BS. unpack
286
282
{-# INLINE otraverseWithKey #-}
287
- #if !MIN_VERSION_base(4,8,0)
288
- omapWithKeyM f = liftM BS. pack . mapM f . BS. unpack
289
283
{-# INLINE omapWithKeyM #-}
290
- #endif
284
+
285
+ otraverseWithKey f = fmap BS. pack . traverseWithKey f . BS. unpack
291
286
287
+ omapWithKeyM f = liftM BS. pack . mapWithKeyM f . BS. unpack
292
288
293
- instance MonoTraversableWithKey BSL. ByteString where
294
289
295
- otraverseWithKey f = fmap BSL. pack . traverse f . BSL. unpack
290
+ instance MonoTraversableWithKey BSL. ByteString where
296
291
{-# 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 #-}
300
293
294
+ otraverseWithKey f = fmap BSL. pack . traverseWithKey f . BSL. unpack
295
+
296
+ omapWithKeyM f = liftM BSL. pack . mapWithKeyM f . BSL. unpack
301
297
302
- instance MonoTraversableWithKey T. Text where
303
298
304
- otraverseWithKey f = fmap T. pack . traverse f . T. unpack
299
+ instance MonoTraversableWithKey T. Text where
305
300
{-# INLINE otraverseWithKey #-}
306
- #if !MIN_VERSION_base(4,8,0)
307
- omapWithKeyM f = liftM T. pack . mapM f . T. unpack
308
301
{-# INLINE omapWithKeyM #-}
309
- #endif
310
302
303
+ otraverseWithKey f = fmap T. pack . traverseWithKey f . T. unpack
304
+
305
+ omapWithKeyM f = liftM T. pack . mapWithKeyM f . T. unpack
311
306
312
- instance MonoTraversableWithKey TL. Text where
313
307
314
- otraverseWithKey f = fmap TL. pack . traverse f . TL. unpack
308
+ instance MonoTraversableWithKey TL. Text where
315
309
{-# INLINE otraverseWithKey #-}
316
- #if !MIN_VERSION_base(4,8,0)
317
- omapWithKeyM f = liftM TL. pack . mapM f . TL. unpack
318
310
{-# 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
320
315
321
316
322
317
instance MonoTraversableWithKey [a ]
@@ -359,42 +354,32 @@ instance MonoTraversableWithKey (Vector a)
359
354
360
355
361
356
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
370
357
{-# INLINE otraverseWithKey #-}
371
358
{-# INLINE omapWithKeyM #-}
372
359
360
+ otraverseWithKey f v = fmap (U. fromListN (U. length v)) . traverseWithKey f $ U. toList v
373
361
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)
379
362
omapWithKeyM = otraverseWithKey
380
- #else
381
- omapWithKeyM = VS. mapM
382
- #endif
363
+
364
+
365
+ instance VS. Storable a => MonoTraversableWithKey ( VS. Vector a ) where
383
366
{-# INLINE otraverseWithKey #-}
384
367
{-# INLINE omapWithKeyM #-}
368
+
369
+ otraverseWithKey f v = fmap (VS. fromListN (VS. length v)) . traverseWithKey f $ VS. toList v
370
+
371
+ omapWithKeyM = otraverseWithKey
372
+
373
+
385
374
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
395
375
{-# INLINE otraverseWithKey #-}
396
376
{-# INLINE omapWithKeyM #-}
397
377
378
+ otraverseWithKey _ (Left a) = pure $ Left a
379
+ otraverseWithKey f (Right b) = fmap Right $ f () b
380
+
381
+ omapWithKeyM = otraverseWithKey
382
+
398
383
399
384
instance MonoTraversableWithKey (a , b )
400
385
0 commit comments