[auto ekmett@gmail.com**20090330000017] { hunk ./doc/html/monoids/Data-Monoid-Combinators.html 450 +>filterWith :: (Generator c, Reducer (Elem c) m) => (m -> n) -> (Elem c -> Bool) -> c -> nfilterWith :: (Generator c, Reducer (Elem c) m) => (m -> n) -> (Elem c -> Bool) -> c -> nSource,find -,sum -,product -,notElem --- * List-Like Monoid Generation -,repeat -,replicate -,cycle --- * QuickCheck Properties -,prop_replicate_right_distributive -)where - -importPreludehiding(mapM_,any,elem,filter,concatMap,and,or,all,sum,product,notElem,replicate,cycle,repeat) -importControl.Applicative -importControl.Monad(MonadPlus) -importData.Monoid.Generator -importData.Monoid.Applicative -importData.Monoid.Self -importData.Monoid.Monad -importTest.QuickCheck - --- | Efficiently 'mapReduce' a 'Generator' using the 'Traversal' monoid. A specialized version of its namesake from "Data.Foldable" --- --- @ --- 'mapReduce' 'getTraversal' --- @ -traverse_::(Generatorc,Applicativef)=>(Elemc->fb)->c->f() -traverse_=mapReduceWithgetTraversal -{-# INLINE traverse_ #-} - --- | Convenience function as found in "Data.Foldable" --- --- @ --- 'flip' 'traverse_' --- @ -for_::(Generatorc,Applicativef)=>c->(Elemc->fb)->f() -for_=fliptraverse_ -{-# INLINE for_ #-} - --- | The sum of a collection of actions, generalizing 'concat' --- --- @ --- 'reduceWith' 'getAlt' --- @ -asum::(Generatorc,Alternativef,fa~Elemc)=>c->fa -asum=reduceWithgetAlt -{-# 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_::(Generatorc,Monadm)=>(Elemc->mb)->c->m() -mapM_=mapReduceWithgetAction -{-# INLINE mapM_ #-} - --- | Convenience function as found in "Data.Foldable" and "Control.Monad" --- --- @ --- 'flip' 'mapM_' --- @ -forM_::(Generatorc,Monadm)=>c->(Elemc->mb)->m() -forM_=flipmapM_ -{-# INLINE forM_ #-} - --- | The sum of a collection of actions, generalizing 'concat' --- --- @ --- 'reduceWith' 'getMonadSum' --- @ -msum::(Generatorc,MonadPlusm,ma~Elemc)=>c->ma -msum=reduceWithgetMonadSum -{-# INLINE msum #-} - --- | Efficiently 'mapReduce' a 'Generator' using the 'Self' monoid. A specialized version of its namesake from "Data.Foldable" --- --- @ --- 'mapReduceWith' 'getSelf' --- @ -foldMap::(Monoidm,Generatorc)=>(Elemc->m)->c->m -foldMap=mapReduceWithgetSelf -{-# INLINE foldMap #-} - --- | Type specialization of "foldMap" above -concatMap::Generatorc=>(Elemc->[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::(Monoidm,Generatorc,Elemc~m)=>c->m -fold=reduceWithgetSelf -{-# INLINE fold #-} - --- | Convert any 'Generator' to a list of its contents. Specialization of 'reduce' -toList::Generatorc=>c->[Elemc] -toList=reduce -{-# INLINE toList #-} - --- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' --- --- @ --- 'reduceWith' 'getAll' --- @ -and::(Generatorc,Elemc~Bool)=>c->Bool -and=reduceWithgetAll -{-# INLINE and #-} - --- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' --- --- @ --- 'reduceWith' 'getAny' --- @ -or::(Generatorc,Elemc~Bool)=>c->Bool -or=reduceWithgetAny -{-# INLINE or #-} - --- | Efficiently 'mapReduce' any 'Generator' checking to see if any of its values match the supplied predicate --- --- @ --- 'mapReduceWith' 'getAny' --- @ -any::Generatorc=>(Elemc->Bool)->c->Bool -any=mapReduceWithgetAny -{-# INLINE any #-} - --- | Efficiently 'mapReduce' any 'Generator' checking to see if all of its values match the supplied predicate --- --- @ --- 'mapReduceWith' 'getAll' --- @ -all::Generatorc=>(Elemc->Bool)->c->Bool -all=mapReduceWithgetAll -{-# INLINE all #-} - --- | Efficiently sum over the members of any 'Generator' --- --- @ --- 'reduceWith' 'getSum' --- @ -sum::(Generatorc,Num(Elemc))=>c->Elemc -sum=reduceWithgetSum -{-# INLINE sum #-} - --- | Efficiently take the product of every member of a 'Generator' --- --- @ --- 'reduceWith' 'getProduct' --- @ -product::(Generatorc,Num(Elemc))=>c->Elemc -product=reduceWithgetProduct -{-# INLINE product #-} - --- | Check to see if 'any' member of the 'Generator' matches the supplied value -elem::(Generatorc,Eq(Elemc))=>Elemc->c->Bool -elem=any.(==) -{-# INLINE elem #-} - --- | Check to make sure that the supplied value is not a member of the 'Generator' -notElem::(Generatorc,Eq(Elemc))=>Elemc->c->Bool -notElemx=not.elemx -{-# INLINE notElem #-} - --- | Efficiently 'mapReduce' a subset of the elements in a 'Generator' -filter::(Generatorc,Elemc`Reducer`m)=>(Elemc->Bool)->c->m -filterp=foldMapfwhere -fx|px=unitx -|otherwise=mempty -{-# INLINE filter #-} - -filterWith::(Generatorc,Elemc`Reducer`m)=>(m->n)->(Elemc->Bool)->c->n -filterWithfp=f.filterp -{-# INLINE filterWith #-} - --- | A specialization of 'filter' using the 'First' 'Monoid', analogous to 'Data.List.find' --- --- @ --- 'filterWith' 'getProduct' --- @ -find::Generatorc=>(Elemc->Bool)->c->Maybe(Elemc) -find=filterWithgetFirst -{-# 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::(Monoidm,Integraln)=>m->n->m -replicatex0y0 -|y0<0=mempty-- error "negative length" -|y0==0=mempty -|otherwise=fx0y0 -where -fxy -|eveny=f(x`mappend`x)(y`quot`2) -|y==1=x -|otherwise=g(x`mappend`x)((y-1)`quot`2)x -gxyz -|eveny=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::Monoidm=>m->m -cyclexs=xs'wherexs'=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 -repeatx=xswherexs=consxxs - -prop_replicate_right_distributive::(Eqm,Monoidm,Arbitrarym,Integraln)=>m->n->n->Bool -prop_replicate_right_distributivemxy -=replicatem(x+y)==replicatemx`mappend`replicatemy +,filterWith +,find +,sum +,product +,notElem +-- * List-Like Monoid Generation +,repeat +,replicate +,cycle +-- * QuickCheck Properties +,prop_replicate_right_distributive +)where + +importPreludehiding(mapM_,any,elem,filter,concatMap,and,or,all,sum,product,notElem,replicate,cycle,repeat) +importControl.Applicative +importControl.Monad(MonadPlus) +importData.Monoid.Generator +importData.Monoid.Applicative +importData.Monoid.Self +importData.Monoid.Monad +importTest.QuickCheck + +-- | Efficiently 'mapReduce' a 'Generator' using the 'Traversal' monoid. A specialized version of its namesake from "Data.Foldable" +-- +-- @ +-- 'mapReduce' 'getTraversal' +-- @ +traverse_::(Generatorc,Applicativef)=>(Elemc->fb)->c->f() +traverse_=mapReduceWithgetTraversal +{-# INLINE traverse_ #-} + +-- | Convenience function as found in "Data.Foldable" +-- +-- @ +-- 'flip' 'traverse_' +-- @ +for_::(Generatorc,Applicativef)=>c->(Elemc->fb)->f() +for_=fliptraverse_ +{-# INLINE for_ #-} + +-- | The sum of a collection of actions, generalizing 'concat' +-- +-- @ +-- 'reduceWith' 'getAlt' +-- @ +asum::(Generatorc,Alternativef,fa~Elemc)=>c->fa +asum=reduceWithgetAlt +{-# 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_::(Generatorc,Monadm)=>(Elemc->mb)->c->m() +mapM_=mapReduceWithgetAction +{-# INLINE mapM_ #-} + +-- | Convenience function as found in "Data.Foldable" and "Control.Monad" +-- +-- @ +-- 'flip' 'mapM_' +-- @ +forM_::(Generatorc,Monadm)=>c->(Elemc->mb)->m() +forM_=flipmapM_ +{-# INLINE forM_ #-} + +-- | The sum of a collection of actions, generalizing 'concat' +-- +-- @ +-- 'reduceWith' 'getMonadSum' +-- @ +msum::(Generatorc,MonadPlusm,ma~Elemc)=>c->ma +msum=reduceWithgetMonadSum +{-# INLINE msum #-} + +-- | Efficiently 'mapReduce' a 'Generator' using the 'Self' monoid. A specialized version of its namesake from "Data.Foldable" +-- +-- @ +-- 'mapReduceWith' 'getSelf' +-- @ +foldMap::(Monoidm,Generatorc)=>(Elemc->m)->c->m +foldMap=mapReduceWithgetSelf +{-# INLINE foldMap #-} + +-- | Type specialization of "foldMap" above +concatMap::Generatorc=>(Elemc->[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::(Monoidm,Generatorc,Elemc~m)=>c->m +fold=reduceWithgetSelf +{-# INLINE fold #-} + +-- | Convert any 'Generator' to a list of its contents. Specialization of 'reduce' +toList::Generatorc=>c->[Elemc] +toList=reduce +{-# INLINE toList #-} + +-- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' +-- +-- @ +-- 'reduceWith' 'getAll' +-- @ +and::(Generatorc,Elemc~Bool)=>c->Bool +and=reduceWithgetAll +{-# INLINE and #-} + +-- | Efficiently 'reduce' a 'Generator' that contains values of type 'Bool' +-- +-- @ +-- 'reduceWith' 'getAny' +-- @ +or::(Generatorc,Elemc~Bool)=>c->Bool +or=reduceWithgetAny +{-# INLINE or #-} + +-- | Efficiently 'mapReduce' any 'Generator' checking to see if any of its values match the supplied predicate +-- +-- @ +-- 'mapReduceWith' 'getAny' +-- @ +any::Generatorc=>(Elemc->Bool)->c->Bool +any=mapReduceWithgetAny +{-# INLINE any #-} + +-- | Efficiently 'mapReduce' any 'Generator' checking to see if all of its values match the supplied predicate +-- +-- @ +-- 'mapReduceWith' 'getAll' +-- @ +all::Generatorc=>(Elemc->Bool)->c->Bool +all=mapReduceWithgetAll +{-# INLINE all #-} + +-- | Efficiently sum over the members of any 'Generator' +-- +-- @ +-- 'reduceWith' 'getSum' +-- @ +sum::(Generatorc,Num(Elemc))=>c->Elemc +sum=reduceWithgetSum +{-# INLINE sum #-} + +-- | Efficiently take the product of every member of a 'Generator' +-- +-- @ +-- 'reduceWith' 'getProduct' +-- @ +product::(Generatorc,Num(Elemc))=>c->Elemc +product=reduceWithgetProduct +{-# INLINE product #-} + +-- | Check to see if 'any' member of the 'Generator' matches the supplied value +elem::(Generatorc,Eq(Elemc))=>Elemc->c->Bool +elem=any.(==) +{-# INLINE elem #-} + +-- | Check to make sure that the supplied value is not a member of the 'Generator' +notElem::(Generatorc,Eq(Elemc))=>Elemc->c->Bool +notElemx=not.elemx +{-# INLINE notElem #-} + +-- | Efficiently 'mapReduce' a subset of the elements in a 'Generator' +filter::(Generatorc,Elemc`Reducer`m)=>(Elemc->Bool)->c->m +filterp=foldMapfwhere +fx|px=unitx +|otherwise=mempty +{-# INLINE filter #-} + +filterWith::(Generatorc,Elemc`Reducer`m)=>(m->n)->(Elemc->Bool)->c->n +filterWithfp=f.filterp +{-# INLINE filterWith #-} + +-- | A specialization of 'filter' using the 'First' 'Monoid', analogous to 'Data.List.find' +-- +-- @ +-- 'filterWith' 'getProduct' +-- @ +find::Generatorc=>(Elemc->Bool)->c->Maybe(Elemc) +find=filterWithgetFirst +{-# 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::(Monoidm,Integraln)=>m->n->m +replicatex0y0 +|y0<0=mempty-- error "negative length" +|y0==0=mempty +|otherwise=fx0y0 +where +fxy +|eveny=f(x`mappend`x)(y`quot`2) +|y==1=x +|otherwise=g(x`mappend`x)((y-1)`quot`2)x +gxyz +|eveny=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::Monoidm=>m->m +cyclexs=xs'wherexs'=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 +repeatx=xswherexs=consxxs + +prop_replicate_right_distributive::(Eqm,Monoidm,Arbitrarym,Integraln)=>m->n->n->Bool +prop_replicate_right_distributivemxy +=replicatem(x+y)==replicatemx`mappend`replicatemy }
filterWithfilterWithfilterWith