reducers 3.10.1.1 → 3.10.2
raw patch · 13 files changed
+74/−69 lines, 13 filesdep ~base
Dependency ranges changed: base
Files
- reducers.cabal +1/−1
- src/Data/Generator.hs +7/−7
- src/Data/Generator/Combinators.hs +7/−7
- src/Data/Semigroup/Alt.hs +8/−5
- src/Data/Semigroup/Alternative.hs +5/−5
- src/Data/Semigroup/Applicative.hs +4/−4
- src/Data/Semigroup/Apply.hs +3/−3
- src/Data/Semigroup/Generator.hs +5/−5
- src/Data/Semigroup/Monad.hs +2/−1
- src/Data/Semigroup/MonadPlus.hs +2/−1
- src/Data/Semigroup/Reducer.hs +16/−16
- src/Data/Semigroup/Reducer/With.hs +5/−5
- src/Data/Semigroup/Union.hs +9/−9
reducers.cabal view
@@ -1,6 +1,6 @@ name: reducers category: Data, Math, Numerical, Semigroups-version: 3.10.1.1+version: 3.10.2 license: BSD3 cabal-version: >= 1.6 license-file: LICENSE
src/Data/Generator.hs view
@@ -12,7 +12,7 @@ -- Stability : experimental -- Portability : portable ----- A 'Generator' @c@ is a possibly-specialized container, which contains values of +-- A 'Generator' @c@ is a possibly-specialized container, which contains values of -- type 'Elem' @c@, and which knows how to efficiently apply a 'Reducer' to extract -- an answer. --@@ -37,7 +37,7 @@ import Data.Monoid (Monoid, mappend, mempty) -import Data.Array +import Data.Array import Data.Text (Text) import qualified Data.Text as Text import qualified Data.ByteString as Strict (ByteString, foldl')@@ -69,7 +69,7 @@ class Generator c where type Elem c mapReduce :: (Reducer e m, Monoid m) => (Elem c -> e) -> c -> m- mapTo :: (Reducer e m, Monoid m) => (Elem c -> e) -> m -> c -> m + mapTo :: (Reducer e m, Monoid m) => (Elem c -> e) -> m -> c -> m mapFrom :: (Reducer e m, Monoid m) => (Elem c -> e) -> c -> m -> m mapReduce f = mapTo f mempty@@ -123,7 +123,7 @@ mapReduce f = mapReduce f . IntMap.toList instance Generator (Map k v) where- type Elem (Map k v) = (k,v) + type Elem (Map k v) = (k,v) mapReduce f = mapReduce f . Map.toList instance Generator (HashMap k v) where@@ -135,7 +135,7 @@ mapReduce f = mapReduce f . assocs -- | a 'Generator' transformer that asks only for the keys of an indexed container-newtype Keys c = Keys { getKeys :: c } +newtype Keys c = Keys { getKeys :: c } instance Generator (Keys (IntMap v)) where type Elem (Keys (IntMap v)) = Int@@ -150,7 +150,7 @@ mapReduce f = mapReduce f . range . bounds . getKeys -- | a 'Generator' transformer that asks only for the values contained in an indexed container-newtype Values c = Values { getValues :: c } +newtype Values c = Values { getValues :: c } instance Generator (Values (IntMap v)) where type Elem (Values (IntMap v)) = v@@ -166,7 +166,7 @@ -- | a 'Generator' transformer that treats 'Word8' as 'Char' -- This lets you use a 'ByteString' as a 'Char' source without going through a 'Monoid' transformer like 'UTF8'-newtype Char8 c = Char8 { getChar8 :: c } +newtype Char8 c = Char8 { getChar8 :: c } instance Generator (Char8 Strict.ByteString) where type Elem (Char8 Strict.ByteString) = Char
src/Data/Generator/Combinators.hs view
@@ -38,7 +38,7 @@ -- * Monoidal Reduction , foldMap , fold- , toList + , toList -- * List-Like Reduction , concatMap , elem@@ -50,7 +50,7 @@ , notElem ) where -import Prelude hiding +import Prelude hiding ( mapM_, any, all, elem, filter, concatMap, and, or , sum, product, notElem, replicate, cycle, repeat )@@ -73,7 +73,7 @@ traverse_ :: (Generator c, Applicative f) => (Elem c -> f b) -> c -> f () traverse_ = mapReduceWith getTraversal {-# INLINE traverse_ #-}- + -- | Convenience function as found in "Data.Foldable" -- -- @@@ -87,16 +87,16 @@ -- -- @ -- 'reduceWith' 'getAlt'--- @ +-- @ asum :: (Generator c, Alternative f, f a ~ Elem c) => c -> f a asum = reduceWith getAlternate {-# INLINE asum #-} -- | Efficiently 'mapReduce' a 'Generator' using the 'Action' monoid. A specialized version of its namesake from "Data.Foldable" and "Control.Monad"--- +-- -- @ -- 'mapReduceWith' 'getAction'--- @ +-- @ mapM_ :: (Generator c, Monad m) => (Elem c -> m b) -> c -> m () mapM_ = mapReduceWith getAction {-# INLINE mapM_ #-}@@ -219,7 +219,7 @@ {-# INLINE filter #-} -- | Allows idiomatic specialization of filter by proving a function that will be used to transform the output-filterWith :: (Generator c, Reducer (Elem c) m, Monoid m) => (m -> n) -> (Elem c -> Bool) -> c -> n +filterWith :: (Generator c, Reducer (Elem c) m, Monoid m) => (m -> n) -> (Elem c -> Bool) -> c -> n filterWith f p = f . filter p {-# INLINE filterWith #-}
src/Data/Semigroup/Alt.hs view
@@ -13,7 +13,7 @@ -- Stability : experimental -- Portability : non-portable (MPTCs) ----- A semigroup for working 'Alt' or 'Plus' +-- A semigroup for working 'Alt' or 'Plus' -- ----------------------------------------------------------------------------- @@ -28,15 +28,18 @@ -- | A 'Alter' turns any 'Alt' instance into a 'Semigroup'. -newtype Alter f a = Alter { getAlter :: f a } - deriving (Functor,Alt,Plus)+newtype Alter f a = Alter { getAlter :: f a }+ deriving (Functor,Plus) +instance Alt f => Alt (Alter f) where+ Alter a <!> Alter b = Alter (a <!> b)+ instance Alt f => Semigroup (Alter f a) where- Alter a <> Alter b = Alter (a <!> b) + Alter a <> Alter b = Alter (a <!> b) instance Plus f => Monoid (Alter f a) where mempty = zero- Alter a `mappend` Alter b = Alter (a <!> b) + Alter a `mappend` Alter b = Alter (a <!> b) instance Alt f => Reducer (f a) (Alter f a) where unit = Alter
src/Data/Semigroup/Alternative.hs view
@@ -28,16 +28,16 @@ -- | A 'Alternate' turns any 'Alternative' instance into a 'Monoid'. -newtype Alternate f a = Alternate { getAlternate :: f a } +newtype Alternate f a = Alternate { getAlternate :: f a } deriving (Functor,Applicative,Alternative) instance Alternative f => Semigroup (Alternate f a) where Alternate a <> Alternate b = Alternate (a <|> b)- + instance Alternative f => Monoid (Alternate f a) where- mempty = empty - Alternate a `mappend` Alternate b = Alternate (a <|> b) + mempty = empty+ Alternate a `mappend` Alternate b = Alternate (a <|> b) instance Alternative f => Reducer (f a) (Alternate f a) where- unit = Alternate + unit = Alternate
src/Data/Semigroup/Applicative.hs view
@@ -17,7 +17,7 @@ -- ----------------------------------------------------------------------------- -module Data.Semigroup.Applicative +module Data.Semigroup.Applicative ( Traversal(..) , Ap(..) ) where@@ -28,9 +28,9 @@ import Data.Semigroup.Reducer (Reducer(..)) -- | A 'Traversal' uses an glues together 'Applicative' actions with (*>)--- in the manner of 'traverse_' from "Data.Foldable". Any values returned by +-- in the manner of 'traverse_' from "Data.Foldable". Any values returned by -- reduced actions are discarded.-newtype Traversal f = Traversal { getTraversal :: f () } +newtype Traversal f = Traversal { getTraversal :: f () } instance Applicative f => Semigroup (Traversal f) where Traversal a <> Traversal b = Traversal (a *> b)@@ -51,7 +51,7 @@ {-# RULES "unitTraversal" unit = Traversal #-} {-# RULES "snocTraversal" snoc = snocTraversal #-} -newtype Ap f m = Ap { getApp :: f m } +newtype Ap f m = Ap { getApp :: f m } deriving (Functor,Applicative) instance (Applicative f, Semigroup m) => Semigroup (Ap f m) where
src/Data/Semigroup/Apply.hs view
@@ -28,9 +28,9 @@ import Data.Semigroup.Reducer (Reducer(..)) -- | A 'Trav' uses an glues together 'Applicative' actions with (*>)--- in the manner of 'traverse_' from "Data.Foldable". Any values returned by +-- in the manner of 'traverse_' from "Data.Foldable". Any values returned by -- reduced actions are discarded.-newtype Trav f = Trav { getTrav :: f () } +newtype Trav f = Trav { getTrav :: f () } instance Apply f => Semigroup (Trav f) where Trav a <> Trav b = Trav (a .> b)@@ -49,7 +49,7 @@ -- | A 'App' turns any 'Apply' wrapped around a 'Semigroup' into a 'Semigroup' -newtype App f m = App { getApp :: f m } +newtype App f m = App { getApp :: f m } deriving (Functor,Apply) instance (Apply f, Semigroup m) => Semigroup (App f m) where
src/Data/Semigroup/Generator.hs view
@@ -12,7 +12,7 @@ -- Stability : experimental -- Portability : portable ----- A 'Generator1' @c@ is a possibly-specialized container, which contains values of +-- A 'Generator1' @c@ is a possibly-specialized container, which contains values of -- type 'Elem' @c@, and which knows how to efficiently apply a 'Reducer' to extract -- an answer. --@@ -40,7 +40,7 @@ -- | minimal definition 'mapReduce1' or 'mapTo1' class Generator c => Generator1 c where mapReduce1 :: Reducer e m => (Elem c -> e) -> c -> m- mapTo1 :: Reducer e m => (Elem c -> e) -> m -> c -> m + mapTo1 :: Reducer e m => (Elem c -> e) -> m -> c -> m mapFrom1 :: Reducer e m => (Elem c -> e) -> c -> m -> m mapTo1 f m = (<>) m . mapReduce1 f@@ -51,13 +51,13 @@ {- mapReduceDefault :: (Generator1 c, Reducer (Elem c) m, Monoid m) => (Elem c -> e) -> c -> m-mapReduceDefault f = unwrapMonoid . mapReduce1 f +mapReduceDefault f = unwrapMonoid . mapReduce1 f mapToDefault :: (Generator1 c, Reducer (Elem c) m, Monoid m) => (Elem c -> e) -> m -> c -> m-mapToDefault f = unwrapMonoid . mapTo1 f +mapToDefault f = unwrapMonoid . mapTo1 f mapFromDefault :: (Generator1 c, Reducer (Elem c) m, Monoid m) => (Elem c -> e) -> m -> c -> m-mapFromDefault f = unwrapMonoid . mapFrom1 f +mapFromDefault f = unwrapMonoid . mapFrom1 f -} -- | Apply a 'Reducer' directly to the elements of a 'Generator'
src/Data/Semigroup/Monad.hs view
@@ -23,6 +23,7 @@ ) where import Control.Monad (liftM, liftM2)+import Control.Applicative (Applicative(..)) import Data.Monoid (Monoid(..)) import Data.Semigroup (Semigroup(..)) import Data.Semigroup.Reducer (Reducer(..))@@ -52,7 +53,7 @@ {-# RULES "snocAction" snoc = snocAction #-} newtype Mon f m = Mon { getMon :: f m }- deriving (Monad)+ deriving (Functor,Applicative,Monad) instance (Monad f, Semigroup m) => Semigroup (Mon f m) where (<>) = liftM2 (<>)
src/Data/Semigroup/MonadPlus.hs view
@@ -22,6 +22,7 @@ ) where import Control.Monad (MonadPlus(..))+import Control.Applicative (Applicative(..),Alternative(..)) import Data.Monoid (Monoid(..)) import Data.Semigroup (Semigroup(..)) import Data.Semigroup.Reducer (Reducer(..))@@ -29,7 +30,7 @@ -- | A 'MonadSum' turns any 'MonadPlus' instance into a 'Monoid'. newtype MonadSum f a = MonadSum { getMonadSum :: f a }- deriving (Monad,MonadPlus)+ deriving (Functor,Applicative,Alternative,Monad,MonadPlus) instance MonadPlus f => Semigroup (MonadSum f a) where MonadSum a <> MonadSum b = MonadSum (mplus a b)
src/Data/Semigroup/Reducer.hs view
@@ -17,13 +17,13 @@ ----------------------------------------------------------------------------- module Data.Semigroup.Reducer- ( Reducer(..)- , foldMapReduce, foldMapReduce1- , foldReduce, foldReduce1- , pureUnit- , returnUnit- , Count(..)- ) where+ ( Reducer(..)+ , foldMapReduce, foldMapReduce1+ , foldReduce, foldReduce1+ , pureUnit+ , returnUnit+ , Count(..)+ ) where import Control.Applicative @@ -64,17 +64,17 @@ -- First and Last may reduce both @a@ and 'Maybe' @a@. Since a 'Generator' has a fixed element -- type, the input to the reducer is generally known and extracting from the monoid usually -- is sufficient to fix the result type. Combinators are available for most scenarios where--- this is not the case, and the few remaining cases can be handled by using an explicit +-- this is not the case, and the few remaining cases can be handled by using an explicit -- type annotation. -- -- Minimal definition: 'unit' class Semigroup m => Reducer c m where -- | Convert a value into a 'Semigroup'- unit :: c -> m + unit :: c -> m -- | Append a value to a 'Semigroup' for use in left-to-right reduction snoc :: m -> c -> m -- | Prepend a value onto a 'Semigroup' for use during right-to-left reduction- cons :: c -> m -> m + cons :: c -> m -> m snoc m = (<>) m . unit cons = (<>) . unit@@ -94,13 +94,13 @@ foldReduce1 :: (Foldable1 f, Reducer e m) => f e -> m foldReduce1 = foldMap1 unit -returnUnit :: (Monad m, Reducer c n) => c -> m n +returnUnit :: (Monad m, Reducer c n) => c -> m n returnUnit = return . unit pureUnit :: (Applicative f, Reducer c n) => c -> f n pureUnit = pure . unit -newtype Count = Count { getCount :: Int } deriving +newtype Count = Count { getCount :: Int } deriving ( Eq, Ord, Show, Read #ifdef LANGUAGE_DeriveDataTypeable , Data, Typeable@@ -122,7 +122,7 @@ unit _ = Count 1 Count n `snoc` _ = Count (n + 1) _ `cons` Count n = Count (n + 1)- + instance (Reducer c m, Reducer c n) => Reducer c (m,n) where unit x = (unit x,unit x) (m,n) `snoc` x = (m `snoc` x, n `snoc` x)@@ -159,7 +159,7 @@ instance Semigroup a => Reducer a (Dual a) where unit = Dual- + instance Num a => Reducer a (Sum a) where unit = Sum @@ -187,7 +187,7 @@ instance Measured v a => Reducer a (FingerTree v a) where unit = singleton cons = (<|)- snoc = (|>) + snoc = (|>) --instance (Stream s m t, Reducer c a) => Reducer c (ParsecT s u m a) where -- unit = return . unit@@ -206,7 +206,7 @@ unit = Set.singleton cons = Set.insert -- pedantic about order in case 'Eq' doesn't implement structural equality- snoc s m | Set.member m s = s + snoc s m | Set.member m s = s | otherwise = Set.insert m s instance Reducer (Int, v) (IntMap v) where
src/Data/Semigroup/Reducer/With.hs view
@@ -16,8 +16,8 @@ ----------------------------------------------------------------------------- module Data.Semigroup.Reducer.With- ( WithReducer(..)- ) where+ ( WithReducer(..)+ ) where import Control.Applicative import Data.FingerTree@@ -34,7 +34,7 @@ -- This can be used to quickly select a "Reducer" for use as a 'FingerTree' -- 'measure'. -newtype WithReducer m c = WithReducer { withoutReducer :: c } +newtype WithReducer m c = WithReducer { withoutReducer :: c } deriving (Eq, Ord, Show, Read) instance Hashable c => Hashable (WithReducer m c) where@@ -56,7 +56,7 @@ traverse1 f (WithReducer a) = WithReducer <$> f a instance Reducer c m => Reducer (WithReducer m c) m where- unit = unit . withoutReducer + unit = unit . withoutReducer instance (Monoid m, Reducer c m) => Measured m (WithReducer m c) where- measure = unit . withoutReducer+ measure = unit . withoutReducer
src/Data/Semigroup/Union.hs view
@@ -104,7 +104,7 @@ -- | The 'Monoid' @('union','empty')@-newtype Union f = Union { getUnion :: f } +newtype Union f = Union { getUnion :: f } deriving (Eq,Ord,Show,Read) instance HasUnion f => Semigroup (Union f) where@@ -141,30 +141,30 @@ {-# SPECIALIZE unionWith :: Eq k => (a -> a -> a) -> HashMap k a -> HashMap k a -> HashMap k a #-} class HasUnionWith f => HasUnionWith0 f where- emptyWith :: f a + emptyWith :: f a -instance HasUnionWith IntMap where +instance HasUnionWith IntMap where unionWith = IntMap.unionWith -instance HasUnionWith0 IntMap where +instance HasUnionWith0 IntMap where emptyWith = IntMap.empty -instance Ord k => HasUnionWith (Map k) where +instance Ord k => HasUnionWith (Map k) where unionWith = Map.unionWith -instance Ord k => HasUnionWith0 (Map k) where +instance Ord k => HasUnionWith0 (Map k) where emptyWith = Map.empty -- TODO: add unionWith to unordered-containers ---instance Eq k => HasUnionWith (HashMap k) where +--instance Eq k => HasUnionWith (HashMap k) where -- unionWith = HashMap.unionWith ---instance Ord k => HasUnionWith0 (Map k) where +--instance Ord k => HasUnionWith0 (Map k) where -- emptyWith = Map.empty -- | The 'Monoid' @('unionWith mappend','empty')@ for containers full of monoids.-newtype UnionWith f m = UnionWith { getUnionWith :: f m } +newtype UnionWith f m = UnionWith { getUnionWith :: f m } instance (HasUnionWith f, Semigroup m) => Semigroup (UnionWith f m) where UnionWith a <> UnionWith b = UnionWith (unionWith (<>) a b)