[auto ekmett@gmail.com**20090329235915] { hunk ./doc/html/monoids/Data-Monoid-Combinators.html 988 ->'reduceWith getAll' Efficiently
Efficiently
+ reduceWith getAll
+
+ reduceWith getAny +
+ mapReduceWith getAny +
+ mapReduceWith getAll +mapReduceWith getSelf Efficiently
Efficiently
+ mapReduceWith getSelf
+
reduceWith getSelf Efficiently
Efficiently
+ reduceWith getSelf
+
reduce Convert any Convert any to a list of its contents.
+> to a list of its contents. Specialization of reduce
hunk ./doc/html/monoids/Data-Monoid-Combinators.html 1532
+>
+ filterWith getProduct +Efficiently mapReduce any
Efficiently sum over the members of any using the Sum +
+ reduceWithMonoidgetSumEfficiently mapReduce anyEfficiently take the product of every member of a using the Product +
+ reduceWithMonoidgetProduct-- | @'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 #-} +-- | Efficiently 'mapReduce' a 'Generator' using the 'Self' monoid. A specialized version of its namesake from "Data.Foldable" +-- +-- @ +-- 'mapReduceWith' 'getSelf' +-- @ +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 #-} + +-- | Efficiently 'reduce' a 'Generator' using the 'Self' monoid. A specialized version of its namesake from "Data.Foldable" +-- +-- @ +-- 'reduceWith' 'getSelf' +-- @ +fold :: (Monoid m, Generator c, Elem c ~ m) => c -> m +fold = reduceWith getSelf +{-# INLINE fold #-} + +-- | Convert any 'Generator' to a list of its contents. Specialization of 'reduce' +toList :: Generator c => c -> [Elem c] +toList = reduce +{-# INLINE toList #-} + +-- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' +-- +-- @ +-- 'reduceWith' 'getAll' +-- @ +and :: (Generator c, Elem c ~ Bool) => c -> Bool +and = reduceWith getAll +{-# INLINE and #-} + +-- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' +-- +-- @ +-- 'reduceWith' 'getAny' +-- @ +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 +-- +-- @ +-- 'mapReduceWith' 'getAny' +-- @ +any :: Generator c => (Elem c -> Bool) -> c -> Bool +any = mapReduceWith getAny +{-# INLINE any #-} hunk ./doc/html/monoids/src/Data-Monoid-Combinators.html 180 --- | 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 +-- | Efficiently 'mapReduce' any 'Generator' checking to see if all of its values match the supplied predicate +-- +-- @ +-- 'mapReduceWith' 'getAll' +-- @ +all :: Generator c => (Elem c -> Bool) -> c -> Bool +all = mapReduceWith getAll +{-# INLINE all #-} + +-- | Efficiently sum over the members of any 'Generator' +-- +-- @ +-- 'reduceWith' 'getSum' +-- @ +sum :: (Generator c, Num (Elem c)) => c -> Elem c +sum = reduceWith getSum +{-# INLINE sum #-} + +-- | Efficiently take the product of every member of a 'Generator' +-- +-- @ +-- 'reduceWith' 'getProduct' +-- @ +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' +-- +-- @ +-- 'filterWith' 'getProduct' +-- @ +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 }