Skip to content

Commit 66f4be1

Browse files
Correcting missing methods.
1 parent bc1bec3 commit 66f4be1

File tree

1 file changed

+118
-26
lines changed

1 file changed

+118
-26
lines changed

src/Data/MonoTraversable/Keys.hs

Lines changed: 118 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -239,18 +239,29 @@ class MonoFunctor mono => MonoAdjustable mono where
239239
-- Monomorphic containers that can be folded over thier pairs of elements and
240240
-- corresponding keys.
241241
class MonoFoldable mono => MonoFoldableWithKey mono where
242-
{-# MINIMAL ofoldMapWithKey | ofoldrWithKey #-}
242+
{-# MINIMAL ofoldMapWithKey | ofoldlWithKey #-}
243243

244244
otoKeyedList :: mono -> [(MonoKey mono, Element mono)]
245245
otoKeyedList = ofoldrWithKey (\k v t -> (k,v):t) []
246-
246+
247247
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
249249

250250
ofoldrWithKey :: (MonoKey mono -> Element mono -> a -> a) -> a -> mono -> a
251251
ofoldrWithKey f z t = appEndo (ofoldMapWithKey (\k v -> Endo (f k v)) t) z
252252

253253
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+
--}
254265
ofoldlWithKey f z t = appEndo (getDual (ofoldMapWithKey (\k a -> Dual (Endo (\b -> f b k a))) t)) z
255266

256267

@@ -267,6 +278,9 @@ class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTr
267278
-- evaluate these actions from left to right, and collect the results.
268279
-- {-# INLINE otraverseWithKey #-}
269280
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
270284

271285
-- |
272286
-- Like 'otraverse' but with a Monad constraint.
@@ -275,6 +289,9 @@ class (MonoKeyed mono, MonoFoldableWithKey mono, MonoTraversable mono) => MonoTr
275289
omapWithKeyM f = unwrapMonad . otraverseWithKey (fmap WrapMonad . f)
276290

277291

292+
monoTraversableWithUnitKey f = otraverse (f ())
293+
294+
278295
-- * Instances
279296

280297

@@ -314,43 +331,82 @@ instance MonoTraversableWithKey TL.Text where
314331
omapWithKeyM f = liftM TL.pack . mapWithKeyM f . TL.unpack
315332

316333

317-
instance MonoTraversableWithKey [a]
334+
instance MonoTraversableWithKey [a] where
335+
{-# INLINE otraverseWithKey #-}
336+
337+
otraverseWithKey = traverseWithKey
338+
318339

340+
instance MonoTraversableWithKey (Maybe a) where
341+
{-# INLINE otraverseWithKey #-}
319342

320-
instance MonoTraversableWithKey (Maybe a)
343+
otraverseWithKey = traverseWithKey
321344

322345

323-
instance MonoTraversableWithKey (Tree a)
346+
instance MonoTraversableWithKey (Tree a) where
347+
{-# INLINE otraverseWithKey #-}
324348

349+
otraverseWithKey = traverseWithKey
325350

326-
instance MonoTraversableWithKey (Seq a)
327351

352+
instance MonoTraversableWithKey (Seq a) where
353+
{-# INLINE otraverseWithKey #-}
328354

329-
instance MonoTraversableWithKey (ViewL a)
355+
otraverseWithKey = traverseWithKey
330356

331357

332-
instance MonoTraversableWithKey (ViewR a)
358+
instance MonoTraversableWithKey (ViewL a) where
359+
{-# INLINE otraverseWithKey #-}
360+
361+
otraverseWithKey = monoTraversableWithUnitKey
333362

334363

335-
instance MonoTraversableWithKey (IntMap a)
364+
instance MonoTraversableWithKey (ViewR a) where
365+
{-# INLINE otraverseWithKey #-}
366+
367+
otraverseWithKey = monoTraversableWithUnitKey
336368

337369

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 #-}
339390

391+
otraverseWithKey = traverseWithKey
340392

341-
instance MonoTraversableWithKey (NonEmpty a)
342393

394+
instance MonoTraversableWithKey (Map k v) where
395+
{-# INLINE otraverseWithKey #-}
343396

344-
instance MonoTraversableWithKey (Identity a)
397+
otraverseWithKey = traverseWithKey
345398

346399

347-
instance MonoTraversableWithKey (Map k v)
400+
instance MonoTraversableWithKey (HashMap k v) where
401+
{-# INLINE otraverseWithKey #-}
348402

403+
otraverseWithKey = traverseWithKey
349404

350-
instance MonoTraversableWithKey (HashMap k v)
351405

406+
instance MonoTraversableWithKey (Vector a) where
407+
{-# INLINE otraverseWithKey #-}
352408

353-
instance MonoTraversableWithKey (Vector a)
409+
otraverseWithKey = traverseWithKey
354410

355411

356412
instance U.Unbox a => MonoTraversableWithKey (U.Vector a) where
@@ -381,31 +437,57 @@ instance MonoTraversableWithKey (Either a b) where
381437
omapWithKeyM = otraverseWithKey
382438

383439

384-
instance MonoTraversableWithKey (a, b)
440+
instance MonoTraversableWithKey (a, b) where
441+
{-# INLINE otraverseWithKey #-}
442+
443+
otraverseWithKey = monoTraversableWithUnitKey
385444

386445

387-
instance MonoTraversableWithKey (Const m a)
446+
instance MonoTraversableWithKey (Const m a) where
447+
{-# INLINE otraverseWithKey #-}
448+
449+
otraverseWithKey = monoTraversableWithUnitKey
388450

389451

390-
instance Traversable f => MonoTraversableWithKey (MaybeT f a)
452+
instance Traversable f => MonoTraversableWithKey (MaybeT f a) where
453+
{-# INLINE otraverseWithKey #-}
454+
455+
otraverseWithKey = monoTraversableWithUnitKey
391456

392457

393-
instance Traversable f => MonoTraversableWithKey (ListT f a)
458+
instance Traversable f => MonoTraversableWithKey (ListT f a) where
394459

460+
otraverseWithKey f = fmap ListT . traverse (traverseWithKey f) . runListT
395461

396-
instance Traversable f => MonoTraversableWithKey (IdentityT f a)
397462

463+
instance Traversable f => MonoTraversableWithKey (IdentityT f a) where
464+
{-# INLINE otraverseWithKey #-}
465+
466+
otraverseWithKey = monoTraversableWithUnitKey
398467

399-
instance Traversable f => MonoTraversableWithKey (WriterT w f a)
400468

469+
instance Traversable f => MonoTraversableWithKey (WriterT w f a) where
470+
{-# INLINE otraverseWithKey #-}
471+
472+
otraverseWithKey = monoTraversableWithUnitKey
401473

402-
instance Traversable f => MonoTraversableWithKey (S.WriterT w f a)
403474

475+
instance Traversable f => MonoTraversableWithKey (S.WriterT w f a) where
476+
{-# INLINE otraverseWithKey #-}
477+
478+
otraverseWithKey = monoTraversableWithUnitKey
404479

405-
instance (Traversable f, Traversable g) => MonoTraversableWithKey (Compose f g a)
406480

481+
instance (Traversable f, Traversable g) => MonoTraversableWithKey (Compose f g a) where
482+
{-# INLINE otraverseWithKey #-}
483+
484+
otraverseWithKey = monoTraversableWithUnitKey
407485

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
409491

410492

411493
omapWithUnitKey f = omap (f ())
@@ -746,7 +828,17 @@ instance MonoFoldableWithKey (Maybe a) where
746828
ofoldMapWithKey = monoFoldableWithUnitKey
747829

748830

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+
750842

751843

752844
instance MonoFoldableWithKey (Seq a) where

0 commit comments

Comments
 (0)