From cde264fd3e579e2e8923f716abc8a5059d0407c5 Mon Sep 17 00:00:00 2001 From: Marco Perone Date: Tue, 10 Sep 2024 15:22:56 +0200 Subject: [PATCH 1/2] add instances for Reverse --- mono-traversable/src/Data/MonoTraversable.hs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/mono-traversable/src/Data/MonoTraversable.hs b/mono-traversable/src/Data/MonoTraversable.hs index 85607c54..6fb921ed 100644 --- a/mono-traversable/src/Data/MonoTraversable.hs +++ b/mono-traversable/src/Data/MonoTraversable.hs @@ -37,7 +37,7 @@ import qualified Data.ByteString.Builder as B import qualified Data.Foldable as F import Data.Functor import Data.Maybe (fromMaybe) -import Data.Monoid (Monoid (..), Any (..), All (..)) +import Data.Monoid (Dual(..), Monoid (..), Any (..), All (..)) import Data.Proxy import qualified Data.Text as T import qualified Data.Text.Lazy as TL @@ -56,6 +56,7 @@ import qualified Foreign.ForeignPtr.Unsafe as Unsafe import Foreign.Ptr (plusPtr) import Foreign.ForeignPtr (touchForeignPtr) import Foreign.Storable (peek) +import Control.Applicative.Backwards (Backwards (..)) import Control.Arrow (Arrow) import Data.Tree (Tree (..)) import Data.Sequence (Seq, ViewL (..), ViewR (..)) @@ -65,6 +66,7 @@ import Data.IntSet (IntSet) import qualified Data.List as List import Data.List.NonEmpty (NonEmpty) import Data.Functor.Identity (Identity) +import Data.Functor.Reverse (Reverse (..)) import Data.Map (Map) import qualified Data.Map.Strict as Map import Data.HashMap.Strict (HashMap) @@ -168,6 +170,7 @@ type instance Element (Par1 a) = a type instance Element (U1 a) = a type instance Element (V1 a) = a type instance Element (Proxy a) = a +type instance Element (Reverse f a) = Element (f a) -- | Monomorphic containers that can be mapped over. class MonoFunctor mono where @@ -255,6 +258,8 @@ instance U.Unbox a => MonoFunctor (U.Vector a) where instance VS.Storable a => MonoFunctor (VS.Vector a) where omap = VS.map {-# INLINE omap #-} +instance MonoFunctor (f a) => MonoFunctor (Reverse f a) where + omap f (Reverse t) = Reverse (omap f t) -- | @'replaceElem' old new@ replaces all @old@ elements with @new@. -- @@ -823,6 +828,12 @@ instance MonoFoldable (U1 a) instance MonoFoldable (V1 a) -- | @since 1.0.11.0 instance MonoFoldable (Proxy a) +instance MonoFoldable (f a) => MonoFoldable (Reverse f a) where + ofoldMap f (Reverse t) = getDual (ofoldMap (Dual . f) t) + ofoldr f z (Reverse t) = ofoldl' (flip f) z t + ofoldl' f z (Reverse t) = ofoldr (flip f) z t + ofoldr1Ex f (Reverse t) = ofoldl1Ex' (flip f) t + ofoldl1Ex' f (Reverse t) = ofoldr1Ex (flip f) t -- | Safe version of 'headEx'. -- @@ -1086,6 +1097,8 @@ instance MonoTraversable (U1 a) instance MonoTraversable (V1 a) -- | @since 1.0.11.0 instance MonoTraversable (Proxy a) +instance (MonoTraversable (f a)) => MonoTraversable (Reverse f a) where + otraverse f (Reverse t) = (fmap Reverse . forwards) (otraverse (Backwards . f) t) -- | 'ofor' is 'otraverse' with its arguments flipped. ofor :: (MonoTraversable mono, Applicative f) => mono -> (Element mono -> f (Element mono)) -> f mono From 9ded34dab3fd6678e552a8711ddfd51b171f00e6 Mon Sep 17 00:00:00 2001 From: Marco Perone Date: Thu, 12 Sep 2024 09:53:03 +0200 Subject: [PATCH 2/2] new 1.0.20.0 version --- mono-traversable/ChangeLog.md | 4 ++++ mono-traversable/package.yaml | 2 +- mono-traversable/src/Data/MonoTraversable.hs | 3 +++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/mono-traversable/ChangeLog.md b/mono-traversable/ChangeLog.md index 19249609..2a4db776 100644 --- a/mono-traversable/ChangeLog.md +++ b/mono-traversable/ChangeLog.md @@ -1,5 +1,9 @@ # ChangeLog for mono-traversable +## 1.0.20.0 + +* Added instances for [`Reverse`](https://hackage.haskell.org/package/transformers-0.6.1.1/docs/Data-Functor-Reverse.html#t:Reverse) data structure. + ## 1.0.19.1 * Removed 'highly experimental' warning haddock comment from Data.Containers. diff --git a/mono-traversable/package.yaml b/mono-traversable/package.yaml index 0bae5d63..e2af2aed 100644 --- a/mono-traversable/package.yaml +++ b/mono-traversable/package.yaml @@ -1,5 +1,5 @@ name: mono-traversable -version: 1.0.19.0 +version: 1.0.20.0 synopsis: Type classes for mapping, folding, and traversing monomorphic containers description: Please see the README at category: Data diff --git a/mono-traversable/src/Data/MonoTraversable.hs b/mono-traversable/src/Data/MonoTraversable.hs index 6fb921ed..2093406f 100644 --- a/mono-traversable/src/Data/MonoTraversable.hs +++ b/mono-traversable/src/Data/MonoTraversable.hs @@ -258,6 +258,7 @@ instance U.Unbox a => MonoFunctor (U.Vector a) where instance VS.Storable a => MonoFunctor (VS.Vector a) where omap = VS.map {-# INLINE omap #-} +-- | @since 1.0.20.0 instance MonoFunctor (f a) => MonoFunctor (Reverse f a) where omap f (Reverse t) = Reverse (omap f t) @@ -828,6 +829,7 @@ instance MonoFoldable (U1 a) instance MonoFoldable (V1 a) -- | @since 1.0.11.0 instance MonoFoldable (Proxy a) +-- | @since 1.0.20.0 instance MonoFoldable (f a) => MonoFoldable (Reverse f a) where ofoldMap f (Reverse t) = getDual (ofoldMap (Dual . f) t) ofoldr f z (Reverse t) = ofoldl' (flip f) z t @@ -1097,6 +1099,7 @@ instance MonoTraversable (U1 a) instance MonoTraversable (V1 a) -- | @since 1.0.11.0 instance MonoTraversable (Proxy a) +-- | @since 1.0.20.0 instance (MonoTraversable (f a)) => MonoTraversable (Reverse f a) where otraverse f (Reverse t) = (fmap Reverse . forwards) (otraverse (Backwards . f) t)