Movatterモバイル変換


[0]ホーム

URL:


{-# LANGUAGE Safe #-}{-# LANGUAGE ScopedTypeVariables #-}------------------------------------------------------------------------------- |-- Module      :  Data.Bifoldable-- Copyright   :  (C) 2011-2016 Edward Kmett-- License     :  BSD-style (see the file LICENSE)---- Maintainer  :  libraries@haskell.org-- Stability   :  provisional-- Portability :  portable---- @since 4.10.0.0----------------------------------------------------------------------------moduleData.Bifoldable(Bifoldable(..),bifoldr',bifoldr1,bifoldrM,bifoldl',bifoldl1,bifoldlM,bitraverse_,bifor_,bimapM_,biforM_,bimsum,bisequenceA_,bisequence_,biasum,biList,binull,bilength,bielem,bimaximum,biminimum,bisum,biproduct,biconcat,biconcatMap,biand,bior,biany,biall,bimaximumBy,biminimumBy,binotElem,bifind)whereimportControl.ApplicativeimportData.Functor.Utils(Max(..),Min(..),(#.))importData.Maybe(fromMaybe)importData.MonoidimportGHC.Generics(K1(..))-- | 'Bifoldable' identifies foldable structures with two different varieties-- of elements (as opposed to 'Foldable', which has one variety of element).-- Common examples are 'Either' and '(,)':---- > instance Bifoldable Either where-- >   bifoldMap f _ (Left  a) = f a-- >   bifoldMap _ g (Right b) = g b-- >-- > instance Bifoldable (,) where-- >   bifoldr f g z (a, b) = f a (g b z)---- A minimal 'Bifoldable' definition consists of either 'bifoldMap' or-- 'bifoldr'. When defining more than this minimal set, one should ensure-- that the following identities hold:---- @-- 'bifold' ≡ 'bifoldMap' 'id' 'id'-- 'bifoldMap' f g ≡ 'bifoldr' ('mappend' . f) ('mappend' . g) 'mempty'-- 'bifoldr' f g z t ≡ 'appEndo' ('bifoldMap' (Endo . f) (Endo . g) t) z-- @---- If the type is also a 'Bifunctor' instance, it should satisfy:---- > 'bifoldMap' f g ≡ 'bifold' . 'bimap' f g---- which implies that---- > 'bifoldMap' f g . 'bimap' h i ≡ 'bifoldMap' (f . h) (g . i)---- @since 4.10.0.0classBifoldablepwhere{-# MINIMALbifoldr|bifoldMap#-}-- | Combines the elements of a structure using a monoid.---- @'bifold' ≡ 'bifoldMap' 'id' 'id'@---- @since 4.10.0.0bifold::Monoidm=>pmm->mbifold=bifoldMapidid-- | Combines the elements of a structure, given ways of mapping them to a-- common monoid.---- @'bifoldMap' f g--     ≡ 'bifoldr' ('mappend' . f) ('mappend' . g) 'mempty'@---- @since 4.10.0.0bifoldMap::Monoidm=>(a->m)->(b->m)->pab->mbifoldMapfg=bifoldr(mappend.f)(mappend.g)mempty-- | Combines the elements of a structure in a right associative manner.-- Given a hypothetical function @toEitherList :: p a b -> [Either a b]@-- yielding a list of all elements of a structure in order, the following-- would hold:---- @'bifoldr' f g z ≡ 'foldr' ('either' f g) z . toEitherList@---- @since 4.10.0.0bifoldr::(a->c->c)->(b->c->c)->c->pab->cbifoldrfgzt=appEndo(bifoldMap(Endo#.f)(Endo#.g)t)z-- | Combines the elements of a structure in a left associative manner. Given-- a hypothetical function @toEitherList :: p a b -> [Either a b]@ yielding a-- list of all elements of a structure in order, the following would hold:---- @'bifoldl' f g z--     ≡ 'foldl' (\acc -> 'either' (f acc) (g acc)) z . toEitherList@---- Note that if you want an efficient left-fold, you probably want to use-- 'bifoldl'' instead of 'bifoldl'. The reason is that the latter does not-- force the "inner" results, resulting in a thunk chain which then must be-- evaluated from the outside-in.---- @since 4.10.0.0bifoldl::(c->a->c)->(c->b->c)->c->pab->cbifoldlfgzt=appEndo(getDual(bifoldMap(Dual.Endo.flipf)(Dual.Endo.flipg)t))z-- | @since 4.10.0.0instanceBifoldable(,)wherebifoldMapfg~(a,b)=fa`mappend`gb-- | @since 4.10.0.0instanceBifoldableConstwherebifoldMapf_(Consta)=fa-- | @since 4.10.0.0instanceBifoldable(K1i)wherebifoldMapf_(K1c)=fc-- | @since 4.10.0.0instanceBifoldable((,,)x)wherebifoldMapfg~(_,a,b)=fa`mappend`gb-- | @since 4.10.0.0instanceBifoldable((,,,)xy)wherebifoldMapfg~(_,_,a,b)=fa`mappend`gb-- | @since 4.10.0.0instanceBifoldable((,,,,)xyz)wherebifoldMapfg~(_,_,_,a,b)=fa`mappend`gb-- | @since 4.10.0.0instanceBifoldable((,,,,,)xyzw)wherebifoldMapfg~(_,_,_,_,a,b)=fa`mappend`gb-- | @since 4.10.0.0instanceBifoldable((,,,,,,)xyzwv)wherebifoldMapfg~(_,_,_,_,_,a,b)=fa`mappend`gb-- | @since 4.10.0.0instanceBifoldableEitherwherebifoldMapf_(Lefta)=fabifoldMap_g(Rightb)=gb-- | As 'bifoldr', but strict in the result of the reduction functions at each-- step.---- @since 4.10.0.0bifoldr'::Bifoldablet=>(a->c->c)->(b->c->c)->c->tab->cbifoldr'fgz0xs=bifoldlf'g'idxsz0wheref'kxz=k$!fxzg'kxz=k$!gxz-- | A variant of 'bifoldr' that has no base case,-- and thus may only be applied to non-empty structures.---- @since 4.10.0.0bifoldr1::Bifoldablet=>(a->a->a)->taa->abifoldr1fxs=fromMaybe(error"bifoldr1: empty structure")(bifoldrmbfmbfNothingxs)wherembfxm=Just(casemofNothing->xJusty->fxy)-- | Right associative monadic bifold over a structure.---- @since 4.10.0.0bifoldrM::(Bifoldablet,Monadm)=>(a->c->mc)->(b->c->mc)->c->tab->mcbifoldrMfgz0xs=bifoldlf'g'returnxsz0wheref'kxz=fxz>>=kg'kxz=gxz>>=k-- | As 'bifoldl', but strict in the result of the reduction functions at each-- step.---- This ensures that each step of the bifold is forced to weak head normal form-- before being applied, avoiding the collection of thunks that would otherwise-- occur. This is often what you want to strictly reduce a finite structure to-- a single, monolithic result (e.g., 'bilength').---- @since 4.10.0.0bifoldl'::Bifoldablet=>(a->b->a)->(a->c->a)->a->tbc->abifoldl'fgz0xs=bifoldrf'g'idxsz0wheref'xkz=k$!fzxg'xkz=k$!gzx-- | A variant of 'bifoldl' that has no base case,-- and thus may only be applied to non-empty structures.---- @since 4.10.0.0bifoldl1::Bifoldablet=>(a->a->a)->taa->abifoldl1fxs=fromMaybe(error"bifoldl1: empty structure")(bifoldlmbfmbfNothingxs)wherembfmy=Just(casemofNothing->yJustx->fxy)-- | Left associative monadic bifold over a structure.---- @since 4.10.0.0bifoldlM::(Bifoldablet,Monadm)=>(a->b->ma)->(a->c->ma)->a->tbc->mabifoldlMfgz0xs=bifoldrf'g'returnxsz0wheref'xkz=fzx>>=kg'xkz=gzx>>=k-- | Map each element of a structure using one of two actions, evaluate these-- actions from left to right, and ignore the results. For a version that-- doesn't ignore the results, see 'Data.Bitraversable.bitraverse'.---- @since 4.10.0.0bitraverse_::(Bifoldablet,Applicativef)=>(a->fc)->(b->fd)->tab->f()bitraverse_fg=bifoldr((*>).f)((*>).g)(pure())-- | As 'bitraverse_', but with the structure as the primary argument. For a-- version that doesn't ignore the results, see 'Data.Bitraversable.bifor'.---- >>> > bifor_ ('a', "bc") print (print . reverse)-- 'a'-- "cb"---- @since 4.10.0.0bifor_::(Bifoldablet,Applicativef)=>tab->(a->fc)->(b->fd)->f()bifor_tfg=bitraverse_fgt-- | Alias for 'bitraverse_'.---- @since 4.10.0.0bimapM_::(Bifoldablet,Applicativef)=>(a->fc)->(b->fd)->tab->f()bimapM_=bitraverse_-- | Alias for 'bifor_'.---- @since 4.10.0.0biforM_::(Bifoldablet,Applicativef)=>tab->(a->fc)->(b->fd)->f()biforM_=bifor_-- | Alias for 'bisequence_'.---- @since 4.10.0.0bisequenceA_::(Bifoldablet,Applicativef)=>t(fa)(fb)->f()bisequenceA_=bisequence_-- | Evaluate each action in the structure from left to right, and ignore the-- results. For a version that doesn't ignore the results, see-- 'Data.Bitraversable.bisequence'.---- @since 4.10.0.0bisequence_::(Bifoldablet,Applicativef)=>t(fa)(fb)->f()bisequence_=bifoldr(*>)(*>)(pure())-- | The sum of a collection of actions, generalizing 'biconcat'.---- @since 4.10.0.0biasum::(Bifoldablet,Alternativef)=>t(fa)(fa)->fabiasum=bifoldr(<|>)(<|>)empty-- | Alias for 'biasum'.---- @since 4.10.0.0bimsum::(Bifoldablet,Alternativef)=>t(fa)(fa)->fabimsum=biasum-- | Collects the list of elements of a structure, from left to right.---- @since 4.10.0.0biList::Bifoldablet=>taa->[a]biList=bifoldr(:)(:)[]-- | Test whether the structure is empty.---- @since 4.10.0.0binull::Bifoldablet=>tab->Boolbinull=bifoldr(\__->False)(\__->False)True-- | Returns the size/length of a finite structure as an 'Int'.---- @since 4.10.0.0bilength::Bifoldablet=>tab->Intbilength=bifoldl'(\c_->c+1)(\c_->c+1)0-- | Does the element occur in the structure?---- @since 4.10.0.0bielem::(Bifoldablet,Eqa)=>a->taa->Boolbielemx=biany(==x)(==x)-- | Reduces a structure of lists to the concatenation of those lists.---- @since 4.10.0.0biconcat::Bifoldablet=>t[a][a]->[a]biconcat=bifold-- | The largest element of a non-empty structure.---- @since 4.10.0.0bimaximum::forallta.(Bifoldablet,Orda)=>taa->abimaximum=fromMaybe(error"bimaximum: empty structure").getMax.bifoldMapmjmjwheremj=Max#.(Just::a->Maybea)-- | The least element of a non-empty structure.---- @since 4.10.0.0biminimum::forallta.(Bifoldablet,Orda)=>taa->abiminimum=fromMaybe(error"biminimum: empty structure").getMin.bifoldMapmjmjwheremj=Min#.(Just::a->Maybea)-- | The 'bisum' function computes the sum of the numbers of a structure.---- @since 4.10.0.0bisum::(Bifoldablet,Numa)=>taa->abisum=getSum#.bifoldMapSumSum-- | The 'biproduct' function computes the product of the numbers of a-- structure.---- @since 4.10.0.0biproduct::(Bifoldablet,Numa)=>taa->abiproduct=getProduct#.bifoldMapProductProduct-- | Given a means of mapping the elements of a structure to lists, computes the-- concatenation of all such lists in order.---- @since 4.10.0.0biconcatMap::Bifoldablet=>(a->[c])->(b->[c])->tab->[c]biconcatMap=bifoldMap-- | 'biand' returns the conjunction of a container of Bools.  For the-- result to be 'True', the container must be finite; 'False', however,-- results from a 'False' value finitely far from the left end.---- @since 4.10.0.0biand::Bifoldablet=>tBoolBool->Boolbiand=getAll#.bifoldMapAllAll-- | 'bior' returns the disjunction of a container of Bools.  For the-- result to be 'False', the container must be finite; 'True', however,-- results from a 'True' value finitely far from the left end.---- @since 4.10.0.0bior::Bifoldablet=>tBoolBool->Boolbior=getAny#.bifoldMapAnyAny-- | Determines whether any element of the structure satisfies its appropriate-- predicate argument.---- @since 4.10.0.0biany::Bifoldablet=>(a->Bool)->(b->Bool)->tab->Boolbianypq=getAny#.bifoldMap(Any.p)(Any.q)-- | Determines whether all elements of the structure satisfy their appropriate-- predicate argument.---- @since 4.10.0.0biall::Bifoldablet=>(a->Bool)->(b->Bool)->tab->Boolbiallpq=getAll#.bifoldMap(All.p)(All.q)-- | The largest element of a non-empty structure with respect to the-- given comparison function.---- @since 4.10.0.0bimaximumBy::Bifoldablet=>(a->a->Ordering)->taa->abimaximumBycmp=bifoldr1max'wheremax'xy=casecmpxyofGT->x_->y-- | The least element of a non-empty structure with respect to the-- given comparison function.---- @since 4.10.0.0biminimumBy::Bifoldablet=>(a->a->Ordering)->taa->abiminimumBycmp=bifoldr1min'wheremin'xy=casecmpxyofGT->y_->x-- | 'binotElem' is the negation of 'bielem'.---- @since 4.10.0.0binotElem::(Bifoldablet,Eqa)=>a->taa->BoolbinotElemx=not.bielemx-- | The 'bifind' function takes a predicate and a structure and returns-- the leftmost element of the structure matching the predicate, or-- 'Nothing' if there is no such element.---- @since 4.10.0.0bifind::Bifoldablet=>(a->Bool)->taa->Maybeabifindp=getFirst.bifoldMapfinderfinderwherefinderx=First(ifpxthenJustxelseNothing)

[8]ページ先頭

©2009-2025 Movatter.jp