module Data.Vector.Fusion.Stream (
Step(..), Stream, MStream,
inplace,
size, sized,
length, null,
empty, singleton, cons, snoc, replicate, generate, (++),
head, last, (!!), (!?),
slice, init, tail, take, drop,
map, concatMap, flatten, unbox,
indexed, indexedR,
zipWith, zipWith3, zipWith4, zipWith5, zipWith6,
zip, zip3, zip4, zip5, zip6,
filter, takeWhile, dropWhile,
elem, notElem, find, findIndex,
foldl, foldl1, foldl', foldl1', foldr, foldr1,
and, or,
unfoldr, unfoldrN, iterateN,
prescanl, prescanl',
postscanl, postscanl',
scanl, scanl',
scanl1, scanl1',
enumFromStepN, enumFromTo, enumFromThenTo,
toList, fromList, fromListN, unsafeFromList, liftStream,
mapM, mapM_, zipWithM, zipWithM_, filterM, foldM, fold1M, foldM', fold1M',
eq, cmp
) where
import Data.Vector.Fusion.Stream.Size
import Data.Vector.Fusion.Util
import Data.Vector.Fusion.Stream.Monadic ( Step(..) )
import qualified Data.Vector.Fusion.Stream.Monadic as M
import Prelude hiding ( length, null,
replicate, (++),
head, last, (!!),
init, tail, take, drop,
map, concatMap,
zipWith, zipWith3, zip, zip3,
filter, takeWhile, dropWhile,
elem, notElem,
foldl, foldl1, foldr, foldr1,
and, or,
scanl, scanl1,
enumFromTo, enumFromThenTo,
mapM, mapM_ )
import GHC.Base ( build )
#include "vector.h"
type Stream = M.Stream Id
type MStream = M.Stream
inplace :: (forall m. Monad m => M.Stream m a -> M.Stream m b)
-> Stream a -> Stream b
inplace f s = s `seq` f s
liftStream :: Monad m => Stream a -> M.Stream m a
liftStream (M.Stream step s sz) = M.Stream (return . unId . step) s sz
size :: Stream a -> Size
size = M.size
sized :: Stream a -> Size -> Stream a
sized = M.sized
length :: Stream a -> Int
length = unId . M.length
null :: Stream a -> Bool
null = unId . M.null
empty :: Stream a
empty = M.empty
singleton :: a -> Stream a
singleton = M.singleton
replicate :: Int -> a -> Stream a
replicate = M.replicate
generate :: Int -> (Int -> a) -> Stream a
generate = M.generate
cons :: a -> Stream a -> Stream a
cons = M.cons
snoc :: Stream a -> a -> Stream a
snoc = M.snoc
infixr 5 ++
(++) :: Stream a -> Stream a -> Stream a
(++) = (M.++)
head :: Stream a -> a
head = unId . M.head
last :: Stream a -> a
last = unId . M.last
infixl 9 !!
(!!) :: Stream a -> Int -> a
s !! i = unId (s M.!! i)
infixl 9 !?
(!?) :: Stream a -> Int -> Maybe a
s !? i = unId (s M.!? i)
slice :: Int
-> Int
-> Stream a
-> Stream a
slice = M.slice
init :: Stream a -> Stream a
init = M.init
tail :: Stream a -> Stream a
tail = M.tail
take :: Int -> Stream a -> Stream a
take = M.take
drop :: Int -> Stream a -> Stream a
drop = M.drop
map :: (a -> b) -> Stream a -> Stream b
map = M.map
unbox :: Stream (Box a) -> Stream a
unbox = M.unbox
concatMap :: (a -> Stream b) -> Stream a -> Stream b
concatMap = M.concatMap
indexed :: Stream a -> Stream (Int,a)
indexed = M.indexed
indexedR :: Int -> Stream a -> Stream (Int,a)
indexedR = M.indexedR
zipWith :: (a -> b -> c) -> Stream a -> Stream b -> Stream c
zipWith = M.zipWith
zipWith3 :: (a -> b -> c -> d) -> Stream a -> Stream b -> Stream c -> Stream d
zipWith3 = M.zipWith3
zipWith4 :: (a -> b -> c -> d -> e)
-> Stream a -> Stream b -> Stream c -> Stream d
-> Stream e
zipWith4 = M.zipWith4
zipWith5 :: (a -> b -> c -> d -> e -> f)
-> Stream a -> Stream b -> Stream c -> Stream d
-> Stream e -> Stream f
zipWith5 = M.zipWith5
zipWith6 :: (a -> b -> c -> d -> e -> f -> g)
-> Stream a -> Stream b -> Stream c -> Stream d
-> Stream e -> Stream f -> Stream g
zipWith6 = M.zipWith6
zip :: Stream a -> Stream b -> Stream (a,b)
zip = M.zip
zip3 :: Stream a -> Stream b -> Stream c -> Stream (a,b,c)
zip3 = M.zip3
zip4 :: Stream a -> Stream b -> Stream c -> Stream d
-> Stream (a,b,c,d)
zip4 = M.zip4
zip5 :: Stream a -> Stream b -> Stream c -> Stream d
-> Stream e -> Stream (a,b,c,d,e)
zip5 = M.zip5
zip6 :: Stream a -> Stream b -> Stream c -> Stream d
-> Stream e -> Stream f -> Stream (a,b,c,d,e,f)
zip6 = M.zip6
filter :: (a -> Bool) -> Stream a -> Stream a
filter = M.filter
takeWhile :: (a -> Bool) -> Stream a -> Stream a
takeWhile = M.takeWhile
dropWhile :: (a -> Bool) -> Stream a -> Stream a
dropWhile = M.dropWhile
infix 4 `elem`
elem :: Eq a => a -> Stream a -> Bool
elem x = unId . M.elem x
infix 4 `notElem`
notElem :: Eq a => a -> Stream a -> Bool
notElem x = unId . M.notElem x
find :: (a -> Bool) -> Stream a -> Maybe a
find f = unId . M.find f
findIndex :: (a -> Bool) -> Stream a -> Maybe Int
findIndex f = unId . M.findIndex f
foldl :: (a -> b -> a) -> a -> Stream b -> a
foldl f z = unId . M.foldl f z
foldl1 :: (a -> a -> a) -> Stream a -> a
foldl1 f = unId . M.foldl1 f
foldl' :: (a -> b -> a) -> a -> Stream b -> a
foldl' f z = unId . M.foldl' f z
foldl1' :: (a -> a -> a) -> Stream a -> a
foldl1' f = unId . M.foldl1' f
foldr :: (a -> b -> b) -> b -> Stream a -> b
foldr f z = unId . M.foldr f z
foldr1 :: (a -> a -> a) -> Stream a -> a
foldr1 f = unId . M.foldr1 f
and :: Stream Bool -> Bool
and = unId . M.and
or :: Stream Bool -> Bool
or = unId . M.or
unfoldr :: (s -> Maybe (a, s)) -> s -> Stream a
unfoldr = M.unfoldr
unfoldrN :: Int -> (s -> Maybe (a, s)) -> s -> Stream a
unfoldrN = M.unfoldrN
iterateN :: Int -> (a -> a) -> a -> Stream a
iterateN = M.iterateN
prescanl :: (a -> b -> a) -> a -> Stream b -> Stream a
prescanl = M.prescanl
prescanl' :: (a -> b -> a) -> a -> Stream b -> Stream a
prescanl' = M.prescanl'
postscanl :: (a -> b -> a) -> a -> Stream b -> Stream a
postscanl = M.postscanl
postscanl' :: (a -> b -> a) -> a -> Stream b -> Stream a
postscanl' = M.postscanl'
scanl :: (a -> b -> a) -> a -> Stream b -> Stream a
scanl = M.scanl
scanl' :: (a -> b -> a) -> a -> Stream b -> Stream a
scanl' = M.scanl'
scanl1 :: (a -> a -> a) -> Stream a -> Stream a
scanl1 = M.scanl1
scanl1' :: (a -> a -> a) -> Stream a -> Stream a
scanl1' = M.scanl1'
eq :: Eq a => Stream a -> Stream a -> Bool
eq (M.Stream step1 s1 _) (M.Stream step2 s2 _) = eq_loop0 s1 s2
where
eq_loop0 s1 s2 = case unId (step1 s1) of
Yield x s1' -> eq_loop1 x s1' s2
Skip s1' -> eq_loop0 s1' s2
Done -> null (M.Stream step2 s2 Unknown)
eq_loop1 x s1 s2 = case unId (step2 s2) of
Yield y s2' -> x == y && eq_loop0 s1 s2'
Skip s2' -> eq_loop1 x s1 s2'
Done -> False
cmp :: Ord a => Stream a -> Stream a -> Ordering
cmp (M.Stream step1 s1 _) (M.Stream step2 s2 _) = cmp_loop0 s1 s2
where
cmp_loop0 s1 s2 = case unId (step1 s1) of
Yield x s1' -> cmp_loop1 x s1' s2
Skip s1' -> cmp_loop0 s1' s2
Done -> if null (M.Stream step2 s2 Unknown)
then EQ else LT
cmp_loop1 x s1 s2 = case unId (step2 s2) of
Yield y s2' -> case x `compare` y of
EQ -> cmp_loop0 s1 s2'
c -> c
Skip s2' -> cmp_loop1 x s1 s2'
Done -> GT
instance Eq a => Eq (M.Stream Id a) where
(==) = eq
instance Ord a => Ord (M.Stream Id a) where
compare = cmp
mapM :: Monad m => (a -> m b) -> Stream a -> M.Stream m b
mapM f = M.mapM f . liftStream
mapM_ :: Monad m => (a -> m b) -> Stream a -> m ()
mapM_ f = M.mapM_ f . liftStream
zipWithM :: Monad m => (a -> b -> m c) -> Stream a -> Stream b -> M.Stream m c
zipWithM f as bs = M.zipWithM f (liftStream as) (liftStream bs)
zipWithM_ :: Monad m => (a -> b -> m c) -> Stream a -> Stream b -> m ()
zipWithM_ f as bs = M.zipWithM_ f (liftStream as) (liftStream bs)
filterM :: Monad m => (a -> m Bool) -> Stream a -> M.Stream m a
filterM f = M.filterM f . liftStream
foldM :: Monad m => (a -> b -> m a) -> a -> Stream b -> m a
foldM m z = M.foldM m z . liftStream
fold1M :: Monad m => (a -> a -> m a) -> Stream a -> m a
fold1M m = M.fold1M m . liftStream
foldM' :: Monad m => (a -> b -> m a) -> a -> Stream b -> m a
foldM' m z = M.foldM' m z . liftStream
fold1M' :: Monad m => (a -> a -> m a) -> Stream a -> m a
fold1M' m = M.fold1M' m . liftStream
enumFromStepN :: Num a => a -> a -> Int -> Stream a
enumFromStepN = M.enumFromStepN
enumFromTo :: Enum a => a -> a -> Stream a
enumFromTo = M.enumFromTo
enumFromThenTo :: Enum a => a -> a -> a -> Stream a
enumFromThenTo = M.enumFromThenTo
toList :: Stream a -> [a]
toList s = build (\c n -> toListFB c n s)
toListFB :: (a -> b -> b) -> b -> Stream a -> b
toListFB c n (M.Stream step s _) = go s
where
go s = case unId (step s) of
Yield x s' -> x `c` go s'
Skip s' -> go s'
Done -> n
fromList :: [a] -> Stream a
fromList = M.fromList
fromListN :: Int -> [a] -> Stream a
fromListN = M.fromListN
unsafeFromList :: Size -> [a] -> Stream a
unsafeFromList = M.unsafeFromList
flatten :: (a -> s) -> (s -> Step s b) -> Size -> Stream a -> Stream b
flatten mk istep sz = M.flatten (return . mk) (return . istep) sz . liftStream