@@ -239,18 +239,29 @@ class MonoFunctor mono => MonoAdjustable mono where
239
239
-- Monomorphic containers that can be folded over thier pairs of elements and
240
240
-- corresponding keys.
241
241
class MonoFoldable mono => MonoFoldableWithKey mono where
242
- {-# MINIMAL ofoldMapWithKey | ofoldrWithKey #-}
242
+ {-# MINIMAL ofoldMapWithKey | ofoldlWithKey #-}
243
243
244
244
otoKeyedList :: mono -> [(MonoKey mono , Element mono )]
245
245
otoKeyedList = ofoldrWithKey (\ k v t -> (k,v): t) []
246
-
246
+
247
247
ofoldMapWithKey :: Monoid m => (MonoKey mono -> Element mono -> m ) -> mono -> m
248
- ofoldMapWithKey f = ofoldrWithKey (\ k v -> mappend (f k v)) mempty
248
+ ofoldMapWithKey f = ofoldlWithKey (\ a k v -> mappend (f k v) a ) mempty
249
249
250
250
ofoldrWithKey :: (MonoKey mono -> Element mono -> a -> a ) -> a -> mono -> a
251
251
ofoldrWithKey f z t = appEndo (ofoldMapWithKey (\ k v -> Endo (f k v)) t) z
252
252
253
253
ofoldlWithKey :: (a -> MonoKey mono -> Element mono -> a ) -> a -> mono -> a
254
+ {- -
255
+ default ofoldlWithKey
256
+ :: ( Keyed f
257
+ , Element (f a) ~ a
258
+ , MonoKey (f a) ~ Key f
259
+ , f a ~ mono
260
+ , FoldableWithKey f
261
+ )
262
+ => (a -> MonoKey mono -> Element mono -> a) -> a -> mono -> a
263
+ ofoldlWithKey = foldlWithKey
264
+ --}
254
265
ofoldlWithKey f z t = appEndo (getDual (ofoldMapWithKey (\ k a -> Dual (Endo (\ b -> f b k a))) t)) z
255
266
256
267
@@ -267,6 +278,9 @@ class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTr
267
278
-- evaluate these actions from left to right, and collect the results.
268
279
-- {-# INLINE otraverseWithKey #-}
269
280
otraverseWithKey :: Applicative f => (MonoKey mono -> Element mono -> f (Element mono )) -> mono -> f mono
281
+ default otraverseWithKey :: (Applicative g , Keyed f , Element (f a ) ~ a , MonoKey (f a ) ~ Key f , f a ~ mono , TraversableWithKey f )
282
+ => (MonoKey mono -> Element mono -> g (Element mono)) -> mono -> g mono
283
+ otraverseWithKey = traverseWithKey
270
284
271
285
-- |
272
286
-- Like 'otraverse' but with a Monad constraint.
@@ -275,6 +289,9 @@ class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTr
275
289
omapWithKeyM f = unwrapMonad . otraverseWithKey (fmap WrapMonad . f)
276
290
277
291
292
+ monoTraversableWithUnitKey f = otraverse (f () )
293
+
294
+
278
295
-- * Instances
279
296
280
297
@@ -314,43 +331,82 @@ instance MonoTraversableWithKey TL.Text where
314
331
omapWithKeyM f = liftM TL. pack . mapWithKeyM f . TL. unpack
315
332
316
333
317
- instance MonoTraversableWithKey [a ]
334
+ instance MonoTraversableWithKey [a ] where
335
+ {-# INLINE otraverseWithKey #-}
336
+
337
+ otraverseWithKey = traverseWithKey
338
+
318
339
340
+ instance MonoTraversableWithKey (Maybe a ) where
341
+ {-# INLINE otraverseWithKey #-}
319
342
320
- instance MonoTraversableWithKey ( Maybe a )
343
+ otraverseWithKey = traverseWithKey
321
344
322
345
323
- instance MonoTraversableWithKey (Tree a )
346
+ instance MonoTraversableWithKey (Tree a ) where
347
+ {-# INLINE otraverseWithKey #-}
324
348
349
+ otraverseWithKey = traverseWithKey
325
350
326
- instance MonoTraversableWithKey (Seq a )
327
351
352
+ instance MonoTraversableWithKey (Seq a ) where
353
+ {-# INLINE otraverseWithKey #-}
328
354
329
- instance MonoTraversableWithKey ( ViewL a )
355
+ otraverseWithKey = traverseWithKey
330
356
331
357
332
- instance MonoTraversableWithKey (ViewR a )
358
+ instance MonoTraversableWithKey (ViewL a ) where
359
+ {-# INLINE otraverseWithKey #-}
360
+
361
+ otraverseWithKey = monoTraversableWithUnitKey
333
362
334
363
335
- instance MonoTraversableWithKey (IntMap a )
364
+ instance MonoTraversableWithKey (ViewR a ) where
365
+ {-# INLINE otraverseWithKey #-}
366
+
367
+ otraverseWithKey = monoTraversableWithUnitKey
336
368
337
369
338
- instance MonoTraversableWithKey (Option a )
370
+ instance MonoTraversableWithKey (IntMap a ) where
371
+ {-# INLINE otraverseWithKey #-}
372
+
373
+ otraverseWithKey = traverseWithKey
374
+
375
+
376
+ instance MonoTraversableWithKey (Option a ) where
377
+ {-# INLINE otraverseWithKey #-}
378
+
379
+ otraverseWithKey = monoTraversableWithUnitKey
380
+
381
+
382
+ instance MonoTraversableWithKey (NonEmpty a ) where
383
+ {-# INLINE otraverseWithKey #-}
384
+
385
+ otraverseWithKey = traverseWithKey
386
+
387
+
388
+ instance MonoTraversableWithKey (Identity a ) where
389
+ {-# INLINE otraverseWithKey #-}
339
390
391
+ otraverseWithKey = traverseWithKey
340
392
341
- instance MonoTraversableWithKey (NonEmpty a )
342
393
394
+ instance MonoTraversableWithKey (Map k v ) where
395
+ {-# INLINE otraverseWithKey #-}
343
396
344
- instance MonoTraversableWithKey ( Identity a )
397
+ otraverseWithKey = traverseWithKey
345
398
346
399
347
- instance MonoTraversableWithKey (Map k v )
400
+ instance MonoTraversableWithKey (HashMap k v ) where
401
+ {-# INLINE otraverseWithKey #-}
348
402
403
+ otraverseWithKey = traverseWithKey
349
404
350
- instance MonoTraversableWithKey (HashMap k v )
351
405
406
+ instance MonoTraversableWithKey (Vector a ) where
407
+ {-# INLINE otraverseWithKey #-}
352
408
353
- instance MonoTraversableWithKey ( Vector a )
409
+ otraverseWithKey = traverseWithKey
354
410
355
411
356
412
instance U. Unbox a => MonoTraversableWithKey (U. Vector a ) where
@@ -381,31 +437,57 @@ instance MonoTraversableWithKey (Either a b) where
381
437
omapWithKeyM = otraverseWithKey
382
438
383
439
384
- instance MonoTraversableWithKey (a , b )
440
+ instance MonoTraversableWithKey (a , b ) where
441
+ {-# INLINE otraverseWithKey #-}
442
+
443
+ otraverseWithKey = monoTraversableWithUnitKey
385
444
386
445
387
- instance MonoTraversableWithKey (Const m a )
446
+ instance MonoTraversableWithKey (Const m a ) where
447
+ {-# INLINE otraverseWithKey #-}
448
+
449
+ otraverseWithKey = monoTraversableWithUnitKey
388
450
389
451
390
- instance Traversable f => MonoTraversableWithKey (MaybeT f a )
452
+ instance Traversable f => MonoTraversableWithKey (MaybeT f a ) where
453
+ {-# INLINE otraverseWithKey #-}
454
+
455
+ otraverseWithKey = monoTraversableWithUnitKey
391
456
392
457
393
- instance Traversable f => MonoTraversableWithKey (ListT f a )
458
+ instance Traversable f => MonoTraversableWithKey (ListT f a ) where
394
459
460
+ otraverseWithKey f = fmap ListT . traverse (traverseWithKey f) . runListT
395
461
396
- instance Traversable f => MonoTraversableWithKey (IdentityT f a )
397
462
463
+ instance Traversable f => MonoTraversableWithKey (IdentityT f a ) where
464
+ {-# INLINE otraverseWithKey #-}
465
+
466
+ otraverseWithKey = monoTraversableWithUnitKey
398
467
399
- instance Traversable f => MonoTraversableWithKey (WriterT w f a )
400
468
469
+ instance Traversable f => MonoTraversableWithKey (WriterT w f a ) where
470
+ {-# INLINE otraverseWithKey #-}
471
+
472
+ otraverseWithKey = monoTraversableWithUnitKey
401
473
402
- instance Traversable f => MonoTraversableWithKey (S. WriterT w f a )
403
474
475
+ instance Traversable f => MonoTraversableWithKey (S. WriterT w f a ) where
476
+ {-# INLINE otraverseWithKey #-}
477
+
478
+ otraverseWithKey = monoTraversableWithUnitKey
404
479
405
- instance (Traversable f , Traversable g ) => MonoTraversableWithKey (Compose f g a )
406
480
481
+ instance (Traversable f , Traversable g ) => MonoTraversableWithKey (Compose f g a ) where
482
+ {-# INLINE otraverseWithKey #-}
483
+
484
+ otraverseWithKey = monoTraversableWithUnitKey
407
485
408
- instance (Traversable f , Traversable g ) => MonoTraversableWithKey (Product f g a )
486
+
487
+ instance (Traversable f , Traversable g ) => MonoTraversableWithKey (Product f g a ) where
488
+ {-# INLINE otraverseWithKey #-}
489
+
490
+ otraverseWithKey = monoTraversableWithUnitKey
409
491
410
492
411
493
omapWithUnitKey f = omap (f () )
@@ -746,7 +828,17 @@ instance MonoFoldableWithKey (Maybe a) where
746
828
ofoldMapWithKey = monoFoldableWithUnitKey
747
829
748
830
749
- instance MonoFoldableWithKey (Tree a )
831
+ instance MonoFoldableWithKey (Tree a ) where
832
+ {-# INLINE ofoldMapWithKey #-}
833
+ {-# INLINE ofoldrWithKey #-}
834
+ {-# INLINE ofoldlWithKey #-}
835
+
836
+ ofoldMapWithKey = foldMapWithKey
837
+
838
+ ofoldrWithKey = foldrWithKey
839
+
840
+ ofoldlWithKey = foldlWithKey
841
+
750
842
751
843
752
844
instance MonoFoldableWithKey (Seq a ) where
0 commit comments