[auto ekmett@gmail.com**20090329235520] { hunk ./doc/html/monoids/Data-Monoid-Combinators.html 638 ->mapReduceWith getAction - Efficiently
Efficiently
+ mapReduceWith getAction
+
Convenience function as found in Data.Foldable and Control.Monad +
+ as in Data.Foldable and Control.MonadThe sum of a collection of actions, generalizing concat +
+ The sum of a collection of actions, generalizing concat-Convenience function as found in Data.Foldable +
+ , as in Data.FoldableThe sum of a collection of actions, generalizing concat +
+ The sum of a collection of actions, generalizing concat-- @ --- 'mapReduce' 'getTraversal' --- @ -traverse_ :: (Generator c, Applicative f) => (Elem c -> f b) -> c -> f () -traverse_ = mapReduceWith getTraversal -{-# INLINE traverse_ #-} - --- | @'flip' 'traverse_'@, as in "Data.Foldable" -for_ :: (Generator c, Applicative f) => c -> (Elem c -> f b) -> f () -for_ = flip traverse_ -{-# INLINE for_ #-} - --- | @'reduceWith' 'getAlt'@ The sum of a collection of actions, generalizing 'concat' -asum :: (Generator c, Alternative f, f a ~ Elem c) => c -> f a -asum = reduceWith getAlt -{-# INLINE asum #-} +-- +-- @ +-- 'mapReduce' 'getTraversal' +-- @ +traverse_ :: (Generator c, Applicative f) => (Elem c -> f b) -> c -> f () +traverse_ = mapReduceWith getTraversal +{-# INLINE traverse_ #-} + +-- | Convenience function as found in "Data.Foldable" +-- +-- @ +-- 'flip' 'traverse_' +-- @ +for_ :: (Generator c, Applicative f) => c -> (Elem c -> f b) -> f () +for_ = flip traverse_ +{-# INLINE for_ #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 89 --- | @'mapReduceWith' 'getAction'@ --- Efficiently 'mapReduce' a 'Generator' using the 'Action' monoid. A specialized version of its namesake from "Data.Foldable" and "Control.Monad" -mapM_ :: (Generator c, Monad m) => (Elem c -> m b) -> c -> m () -mapM_ = mapReduceWith getAction -{-# INLINE mapM_ #-} - --- | @'flip' 'mapM_'@ as in "Data.Foldable" and "Control.Monad" -forM_ :: (Generator c, Monad m) => c -> (Elem c -> m b) -> m () -forM_ = flip mapM_ -{-# INLINE forM_ #-} - --- | @'reduceWith' 'getMonadSum'@ The sum of a collection of actions, generalizing 'concat' -msum :: (Generator c, MonadPlus m, m a ~ Elem c) => c -> m a -msum = reduceWith getMonadSum -{-# INLINE msum #-} - --- | @'mapReduceWith' 'getSelf'@ Efficiently 'mapReduce' a 'Generator' using the 'Self' monoid. A specialized version of its namesake from "Data.Foldable" -foldMap :: (Monoid m, Generator c) => (Elem c -> m) -> c -> m -foldMap = mapReduceWith getSelf -{-# INLINE foldMap #-} - --- | Type specialization of "foldMap" above -concatMap :: Generator c => (Elem c -> [b]) -> c -> [b] -concatMap = foldMap -{-# INLINE concatMap #-} - --- | @'reduceWith' 'getSelf'@ Efficiently 'reduce' a 'Generator' using the 'Self' monoid. A specialized version of its namesake from "Data.Foldable" -fold :: (Monoid m, Generator c, Elem c ~ m) => c -> m -fold = reduceWith getSelf -{-# INLINE fold #-} - --- | @'reduce'@ Convert any 'Generator' to a list of its contents. -toList :: Generator c => c -> [Elem c] -toList = reduce -{-# INLINE toList #-} +-- | The sum of a collection of actions, generalizing 'concat' +-- +-- @ +-- 'reduceWith' 'getAlt' +-- @ +asum :: (Generator c, Alternative f, f a ~ Elem c) => c -> f a +asum = reduceWith getAlt +{-# 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_ #-} + +-- | Convenience function as found in "Data.Foldable" and "Control.Monad" +-- +-- @ +-- 'flip' 'mapM_' +-- @ +forM_ :: (Generator c, Monad m) => c -> (Elem c -> m b) -> m () +forM_ = flip mapM_ +{-# INLINE forM_ #-} + +-- | The sum of a collection of actions, generalizing 'concat' +-- +-- @ +-- 'reduceWith' 'getMonadSum' +-- @ +msum :: (Generator c, MonadPlus m, m a ~ Elem c) => c -> m a +msum = reduceWith getMonadSum +{-# INLINE msum #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 125 --- | @'reduceWith getAll'@ Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' -and :: (Generator c, Elem c ~ Bool) => c -> Bool -and = reduceWith getAll -{-# INLINE and #-} +-- | @'mapReduceWith' 'getSelf'@ Efficiently 'mapReduce' a 'Generator' using the 'Self' monoid. A specialized version of its namesake from "Data.Foldable" +foldMap :: (Monoid m, Generator c) => (Elem c -> m) -> c -> m +foldMap = mapReduceWith getSelf +{-# INLINE foldMap #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 130 --- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' -or :: (Generator c, Elem c ~ Bool) => c -> Bool -or = reduceWith getAny -{-# INLINE or #-} +-- | Type specialization of "foldMap" above +concatMap :: Generator c => (Elem c -> [b]) -> c -> [b] +concatMap = foldMap +{-# INLINE concatMap #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 135 --- | Efficiently 'mapReduce' any 'Generator' checking to see if any of its values match the supplied predicate -any :: Generator c => (Elem c -> Bool) -> c -> Bool -any = mapReduceWith getAny -{-# INLINE any #-} +-- | @'reduceWith' 'getSelf'@ Efficiently 'reduce' a 'Generator' using the 'Self' monoid. A specialized version of its namesake from "Data.Foldable" +fold :: (Monoid m, Generator c, Elem c ~ m) => c -> m +fold = reduceWith getSelf +{-# INLINE fold #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 140 --- | Efficiently 'mapReduce' any 'Generator' checking to see if all of its values match the supplied predicate -all :: Generator c => (Elem c -> Bool) -> c -> Bool -all = mapReduceWith getAll -{-# INLINE all #-} +-- | @'reduce'@ Convert any 'Generator' to a list of its contents. +toList :: Generator c => c -> [Elem c] +toList = reduce +{-# INLINE toList #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 145 --- | Efficiently 'mapReduce' any 'Generator' using the 'Sum' 'Monoid' -sum :: (Generator c, Num (Elem c)) => c -> Elem c -sum = reduceWith getSum -{-# INLINE sum #-} +-- | @'reduceWith getAll'@ Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' +and :: (Generator c, Elem c ~ Bool) => c -> Bool +and = reduceWith getAll +{-# INLINE and #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 150 --- | Efficiently 'mapReduce' any 'Generator' using the 'Product' 'Monoid' -product :: (Generator c, Num (Elem c)) => c -> Elem c -product = reduceWith getProduct -{-# INLINE product #-} +-- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' +or :: (Generator c, Elem c ~ Bool) => c -> Bool +or = reduceWith getAny +{-# INLINE or #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 155 --- | Check to see if 'any' member of the 'Generator' matches the supplied value -elem :: (Generator c, Eq (Elem c)) => Elem c -> c -> Bool -elem = any . (==) -{-# INLINE elem #-} +-- | Efficiently 'mapReduce' any 'Generator' checking to see if any of its values match the supplied predicate +any :: Generator c => (Elem c -> Bool) -> c -> Bool +any = mapReduceWith getAny +{-# INLINE any #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 160 --- | Check to make sure that the supplied value is not a member of the 'Generator' -notElem :: (Generator c, Eq (Elem c)) => Elem c -> c -> Bool -notElem x = not . elem x -{-# INLINE notElem #-} +-- | Efficiently 'mapReduce' any 'Generator' checking to see if all of its values match the supplied predicate +all :: Generator c => (Elem c -> Bool) -> c -> Bool +all = mapReduceWith getAll +{-# INLINE all #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 165 --- | Efficiently 'mapReduce' a subset of the elements in a 'Generator' -filter :: (Generator c, Elem c `Reducer` m) => (Elem c -> Bool) -> c -> m -filter p = foldMap f where - f x | p x = unit x - | otherwise = mempty -{-# INLINE filter #-} - -filterWith :: (Generator c, Elem c `Reducer` m) => (m -> n) -> (Elem c -> Bool) -> c -> n -filterWith f p = f . filter p -{-# INLINE filterWith #-} - --- | A specialization of 'filter' using the 'First' 'Monoid', analogous to 'Data.List.find' -find :: Generator c => (Elem c -> Bool) -> c -> Maybe (Elem c) -find = filterWith getFirst -{-# INLINE find #-} - --- | A generalization of 'Data.List.replicate' to an arbitrary 'Monoid'. Adapted from --- <http://augustss.blogspot.com/2008/07/lost-and-found-if-i-write-108-in.html> -replicate :: (Monoid m, Integral n) => m -> n -> m -replicate x0 y0 - | y0 < 0 = mempty -- error "negative length" - | y0 == 0 = mempty - | otherwise = f x0 y0 - where - f x y - | even y = f (x `mappend` x) (y `quot` 2) - | y == 1 = x - | otherwise = g (x `mappend` x) ((y - 1) `quot` 2) x - g x y z - | even y = g (x `mappend` x) (y `quot` 2) z - | y == 1 = x `mappend` z - | otherwise = g (x `mappend` x) ((y - 1) `quot` 2) (x `mappend` z) -{-# INLINE replicate #-} - --- | A generalization of 'Data.List.cycle' to an arbitrary 'Monoid'. May fail to terminate for some values in some monoids. -cycle :: Monoid m => m -> m -cycle xs = xs' where xs' = xs `mappend` xs' - --- | A generalization of 'Data.List.repeat' to an arbitrary 'Monoid'. May fail to terminate for some values in some monoids. -repeat :: (e `Reducer` m) => e -> m -repeat x = xs where xs = cons x xs - -prop_replicate_right_distributive :: (Eq m, Monoid m, Arbitrary m, Integral n) => m -> n -> n -> Bool -prop_replicate_right_distributive m x y - = replicate m (x + y) == replicate m x `mappend` replicate m y +-- | Efficiently 'mapReduce' any 'Generator' using the 'Sum' 'Monoid' +sum :: (Generator c, Num (Elem c)) => c -> Elem c +sum = reduceWith getSum +{-# INLINE sum #-} + +-- | Efficiently 'mapReduce' any 'Generator' using the 'Product' 'Monoid' +product :: (Generator c, Num (Elem c)) => c -> Elem c +product = reduceWith getProduct +{-# INLINE product #-} + +-- | Check to see if 'any' member of the 'Generator' matches the supplied value +elem :: (Generator c, Eq (Elem c)) => Elem c -> c -> Bool +elem = any . (==) +{-# INLINE elem #-} + +-- | Check to make sure that the supplied value is not a member of the 'Generator' +notElem :: (Generator c, Eq (Elem c)) => Elem c -> c -> Bool +notElem x = not . elem x +{-# INLINE notElem #-} + +-- | Efficiently 'mapReduce' a subset of the elements in a 'Generator' +filter :: (Generator c, Elem c `Reducer` m) => (Elem c -> Bool) -> c -> m +filter p = foldMap f where + f x | p x = unit x + | otherwise = mempty +{-# INLINE filter #-} + +filterWith :: (Generator c, Elem c `Reducer` m) => (m -> n) -> (Elem c -> Bool) -> c -> n +filterWith f p = f . filter p +{-# INLINE filterWith #-} + +-- | A specialization of 'filter' using the 'First' 'Monoid', analogous to 'Data.List.find' +find :: Generator c => (Elem c -> Bool) -> c -> Maybe (Elem c) +find = filterWith getFirst +{-# INLINE find #-} + +-- | A generalization of 'Data.List.replicate' to an arbitrary 'Monoid'. Adapted from +-- <http://augustss.blogspot.com/2008/07/lost-and-found-if-i-write-108-in.html> +replicate :: (Monoid m, Integral n) => m -> n -> m +replicate x0 y0 + | y0 < 0 = mempty -- error "negative length" + | y0 == 0 = mempty + | otherwise = f x0 y0 + where + f x y + | even y = f (x `mappend` x) (y `quot` 2) + | y == 1 = x + | otherwise = g (x `mappend` x) ((y - 1) `quot` 2) x + g x y z + | even y = g (x `mappend` x) (y `quot` 2) z + | y == 1 = x `mappend` z + | otherwise = g (x `mappend` x) ((y - 1) `quot` 2) (x `mappend` z) +{-# INLINE replicate #-} + +-- | A generalization of 'Data.List.cycle' to an arbitrary 'Monoid'. May fail to terminate for some values in some monoids. +cycle :: Monoid m => m -> m +cycle xs = xs' where xs' = xs `mappend` xs' + +-- | A generalization of 'Data.List.repeat' to an arbitrary 'Monoid'. May fail to terminate for some values in some monoids. +repeat :: (e `Reducer` m) => e -> m +repeat x = xs where xs = cons x xs + +prop_replicate_right_distributive :: (Eq m, Monoid m, Arbitrary m, Integral n) => m -> n -> n -> Bool +prop_replicate_right_distributive m x y + = replicate m (x + y) == replicate m x `mappend` replicate m y }