------------------------------------------------------------------------------- |-- Module: Data.Enumerator.List-- Copyright: 2010 John Millikin-- License: MIT---- Maintainer: jmillikin@gmail.com-- Portability: portable---- This module is intended to be imported qualified:---- @-- import qualified Data.Enumerator.List as EL-- @---- Since: 0.4.5-------------------------------------------------------------------------------moduleData.Enumerator.List(head,drop,dropWhile,take,takeWhile,consume,require,isolate)whereimportData.Enumeratorhiding(consume,head,peek,drop,dropWhile)importControl.Exception(ErrorCall(..))importPreludehiding(head,drop,dropWhile,take,takeWhile)importqualifiedData.ListasL-- | Get the next element from the stream, or 'Nothing' if the stream has-- ended.---- Since: 0.4.5head::Monadm=>Iterateeam(Maybea)head=continueloopwhereloop(Chunks[])=headloop(Chunks(x:xs))=yield(Justx)(Chunksxs)loopEOF=yieldNothingEOF-- | @drop n@ ignores /n/ input elements from the stream.---- Since: 0.4.5drop::Monadm=>Integer->Iterateeam()dropn|n<=0=return()dropn=continue(loopn)whereloopn'(Chunksxs)=iterwherelen=L.genericLengthxsiter=iflen<n'thendrop(n'-len)elseyield()(Chunks(L.genericDropn'xs))loop_EOF=yield()EOF-- | @dropWhile p@ ignores input from the stream until the first element-- which does not match the predicate.---- Since: 0.4.5dropWhile::Monadm=>(a->Bool)->Iterateeam()dropWhilep=continueloopwhereloop(Chunksxs)=caseL.dropWhilepxsof[]->continueloopxs'->yield()(Chunksxs')loopEOF=yield()EOF-- | @take n@ extracts the next /n/ elements from the stream, as a list.---- Since: 0.4.5take::Monadm=>Integer->Iterateeam[a]taken|n<=0=return[]taken=continue(loopidn)wherelen=L.genericLengthloopaccn'(Chunksxs)|lenxs<n'=continue(loop(acc.(xs++))(n'-lenxs))|otherwise=let(xs',extra)=L.genericSplitAtn'xsinyield(accxs')(Chunksextra)loopacc_EOF=yield(acc[])EOF-- | @takeWhile p@ extracts input from the stream until the first element-- which does not match the predicate.---- Since: 0.4.5takeWhile::Monadm=>(a->Bool)->Iterateeam[a]takeWhilep=continue(loopid)whereloopacc(Chunks[])=continue(loopacc)loopacc(Chunksxs)=casePrelude.spanpxsof(_,[])->continue(loop(acc.(xs++)))(xs',extra)->yield(accxs')(Chunksextra)loopaccEOF=yield(acc[])EOF-- | Read all remaining input elements from the stream, and return as a list.---- Since: 0.4.5consume::Monadm=>Iterateeam[a]consume=continue(loopid)whereloopacc(Chunks[])=continue(loopacc)loopacc(Chunksxs)=continue(loop(acc.(xs++)))loopaccEOF=yield(acc[])EOF-- | @require n@ buffers input until at least /n/ elements are available, or-- throws an error if the stream ends early.---- Since: 0.4.5require::Monadm=>Integer->Iterateeam()requiren|n<=0=return()requiren=continue(loopidn)wherelen=L.genericLengthloopaccn'(Chunksxs)|lenxs<n'=continue(loop(acc.(xs++))(n'-lenxs))|otherwise=yield()(Chunks(accxs))loop__EOF=throwError(ErrorCall"require: Unexpected EOF")-- | @isolate n@ reads at most /n/ elements from the stream, and passes them-- to its iteratee. If the iteratee finishes early, elements continue to be-- consumed from the outer stream until /n/ have been consumed.---- Since: 0.4.5isolate::Monadm=>Integer->Enumerateeaambisolatenstep|n<=0=returnstepisolaten(Continuek)=continueloopwherelen=L.genericLengthloop(Chunks[])=continuelooploop(Chunksxs)|lenxs<=n=k(Chunksxs)>>==isolate(n-lenxs)|otherwise=let(s1,s2)=L.genericSplitAtnxsink(Chunkss1)>>==(\step->yieldstep(Chunkss2))loopEOF=kEOF>>==(\step->yieldstepEOF)isolatenstep=dropn>>returnstep