module Data.Enumerator.List (
head
, drop
, dropWhile
, take
, takeWhile
, consume
, require
, isolate
) where
import Data.Enumerator hiding (consume, head, peek, drop, dropWhile)
import Control.Exception (ErrorCall(..))
import Prelude hiding (head, drop, dropWhile, take, takeWhile)
import qualified Data.List as L
head :: Monad m => Iteratee a m (Maybe a)
head = continue loop where
loop (Chunks []) = head
loop (Chunks (x:xs)) = yield (Just x) (Chunks xs)
loop EOF = yield Nothing EOF
drop :: Monad m => Integer -> Iteratee a m ()
drop n | n <= 0 = return ()
drop n = continue (loop n) where
loop n' (Chunks xs) = iter where
len = L.genericLength xs
iter = if len < n'
then drop (n' len)
else yield () (Chunks (L.genericDrop n' xs))
loop _ EOF = yield () EOF
dropWhile :: Monad m => (a -> Bool) -> Iteratee a m ()
dropWhile p = continue loop where
loop (Chunks xs) = case L.dropWhile p xs of
[] -> continue loop
xs' -> yield () (Chunks xs')
loop EOF = yield () EOF
take :: Monad m => Integer -> Iteratee a m [a]
take n | n <= 0 = return []
take n = continue (loop id n) where
len = L.genericLength
loop acc n' (Chunks xs)
| len xs < n' = continue (loop (acc . (xs ++)) (n' len xs))
| otherwise = let
(xs', extra) = L.genericSplitAt n' xs
in yield (acc xs') (Chunks extra)
loop acc _ EOF = yield (acc []) EOF
takeWhile :: Monad m => (a -> Bool) -> Iteratee a m [a]
takeWhile p = continue (loop id) where
loop acc (Chunks []) = continue (loop acc)
loop acc (Chunks xs) = case Prelude.span p xs of
(_, []) -> continue (loop (acc . (xs ++)))
(xs', extra) -> yield (acc xs') (Chunks extra)
loop acc EOF = yield (acc []) EOF
consume :: Monad m => Iteratee a m [a]
consume = continue (loop id) where
loop acc (Chunks []) = continue (loop acc)
loop acc (Chunks xs) = continue (loop (acc . (xs ++)))
loop acc EOF = yield (acc []) EOF
require :: Monad m => Integer -> Iteratee a m ()
require n | n <= 0 = return ()
require n = continue (loop id n) where
len = L.genericLength
loop acc n' (Chunks xs)
| len xs < n' = continue (loop (acc . (xs ++)) (n' len xs))
| otherwise = yield () (Chunks (acc xs))
loop _ _ EOF = throwError (ErrorCall "require: Unexpected EOF")
isolate :: Monad m => Integer -> Enumeratee a a m b
isolate n step | n <= 0 = return step
isolate n (Continue k) = continue loop where
len = L.genericLength
loop (Chunks []) = continue loop
loop (Chunks xs)
| len xs <= n = k (Chunks xs) >>== isolate (n len xs)
| otherwise = let
(s1, s2) = L.genericSplitAt n xs
in k (Chunks s1) >>== (\step -> yield step (Chunks s2))
loop EOF = k EOF >>== (\step -> yield step EOF)
isolate n step = drop n >> return step