[auto ekmett@gmail.com**20090329235204] { hunk ./doc/html/monoids/Data-Monoid-Combinators.html 803 ->
mapReduce getTraversal -+ mapReduce getTraversal + +-- --- > 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 #-} - --- | @'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 #-} - --- | @'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 #-} - --- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' -or :: (Generator c, Elem c ~ Bool) => c -> Bool -or = reduceWith getAny -{-# INLINE or #-} - --- | 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 #-} - --- | 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 #-} - --- | 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 +-- @ +-- '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 #-} + +-- | @'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 #-} + +-- | @'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 #-} + +-- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' +or :: (Generator c, Elem c ~ Bool) => c -> Bool +or = reduceWith getAny +{-# INLINE or #-} + +-- | 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 #-} + +-- | 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 #-} + +-- | 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 }