module Data.Stream
(
Stream(..)
, (<:>)
, head
, tail
, inits
, tails
, map
, intersperse
, interleave
, scan
, scan'
, scan1
, scan1'
, transpose
, iterate
, repeat
, cycle
, unfold
, prefix
, take
, drop
, splitAt
, takeWhile
, dropWhile
, span
, break
, filter
, partition
, group
, isPrefixOf
, (!!)
, elemIndex
, elemIndices
, findIndex
, findIndices
, zip
, zipWith
, unzip
, zip3
, zipWith3
, unzip3
, distribute
, words
, unwords
, lines
, unlines
, toList
, fromList
)
where
import Prelude hiding (head, tail, map, scanl, scanl1,
iterate, take, drop, takeWhile,
dropWhile, repeat, cycle, filter, (!!),
zip, unzip, zipWith, zip3, unzip3, zipWith3,
words,unwords,lines,unlines, break, span, splitAt)
import Control.Applicative
import Control.Monad (liftM2)
import Data.Monoid (mappend)
import Data.Char (isSpace)
import Test.QuickCheck (Arbitrary, CoArbitrary, arbitrary, coarbitrary)
import Test.LazySmallCheck (Serial, series, cons2)
data Stream a = Cons a (Stream a) deriving (Stream a -> Stream a -> Bool
forall a. Eq a => Stream a -> Stream a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Stream a -> Stream a -> Bool
$c/= :: forall a. Eq a => Stream a -> Stream a -> Bool
== :: Stream a -> Stream a -> Bool
$c== :: forall a. Eq a => Stream a -> Stream a -> Bool
Eq, Stream a -> Stream a -> Bool
Stream a -> Stream a -> Ordering
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall {a}. Ord a => Eq (Stream a)
forall a. Ord a => Stream a -> Stream a -> Bool
forall a. Ord a => Stream a -> Stream a -> Ordering
forall a. Ord a => Stream a -> Stream a -> Stream a
min :: Stream a -> Stream a -> Stream a
$cmin :: forall a. Ord a => Stream a -> Stream a -> Stream a
max :: Stream a -> Stream a -> Stream a
$cmax :: forall a. Ord a => Stream a -> Stream a -> Stream a
>= :: Stream a -> Stream a -> Bool
$c>= :: forall a. Ord a => Stream a -> Stream a -> Bool
> :: Stream a -> Stream a -> Bool
$c> :: forall a. Ord a => Stream a -> Stream a -> Bool
<= :: Stream a -> Stream a -> Bool
$c<= :: forall a. Ord a => Stream a -> Stream a -> Bool
< :: Stream a -> Stream a -> Bool
$c< :: forall a. Ord a => Stream a -> Stream a -> Bool
compare :: Stream a -> Stream a -> Ordering
$ccompare :: forall a. Ord a => Stream a -> Stream a -> Ordering
Ord)
infixr 5 `Cons`
instance Functor Stream where
fmap :: forall a b. (a -> b) -> Stream a -> Stream b
fmap a -> b
f ~(Cons a
x Stream a
xs) = forall a. a -> Stream a -> Stream a
Cons (a -> b
f a
x) (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> b
f Stream a
xs)
instance Applicative Stream where
pure :: forall a. a -> Stream a
pure = forall a. a -> Stream a
repeat
<*> :: forall a b. Stream (a -> b) -> Stream a -> Stream b
(<*>) = forall a b c. (a -> b -> c) -> Stream a -> Stream b -> Stream c
zipWith forall a b. (a -> b) -> a -> b
($)
instance Monad Stream where
return :: forall a. a -> Stream a
return = forall a. a -> Stream a
repeat
Stream a
xs >>= :: forall a b. Stream a -> (a -> Stream b) -> Stream b
>>= a -> Stream b
f = forall a. Stream (Stream a) -> Stream a
join (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap a -> Stream b
f Stream a
xs)
where
join :: Stream (Stream a) -> Stream a
join :: forall a. Stream (Stream a) -> Stream a
join ~(Cons Stream a
xs Stream (Stream a)
xss) = forall a. a -> Stream a -> Stream a
Cons (forall a. Stream a -> a
head Stream a
xs) (forall a. Stream (Stream a) -> Stream a
join (forall a b. (a -> b) -> Stream a -> Stream b
map forall a. Stream a -> Stream a
tail Stream (Stream a)
xss))
instance Arbitrary a => Arbitrary (Stream a) where
arbitrary :: Gen (Stream a)
arbitrary = forall (m :: * -> *) a1 a2 r.
Monad m =>
(a1 -> a2 -> r) -> m a1 -> m a2 -> m r
liftM2 forall a. a -> Stream a -> Stream a
Cons forall a. Arbitrary a => Gen a
arbitrary forall a. Arbitrary a => Gen a
arbitrary
instance CoArbitrary a => CoArbitrary (Stream a) where
coarbitrary :: forall b. Stream a -> Gen b -> Gen b
coarbitrary Stream a
xs Gen b
gen = do
Int
n <- forall a. Arbitrary a => Gen a
arbitrary
forall a b. CoArbitrary a => a -> Gen b -> Gen b
coarbitrary (forall a. Int -> Stream a -> [a]
take (forall a. Num a => a -> a
abs Int
n) Stream a
xs) Gen b
gen
instance Serial a => Serial (Stream a) where
series :: Series (Stream a)
series = forall a b c. (Serial a, Serial b) => (a -> b -> c) -> Series c
cons2 forall a. a -> Stream a -> Stream a
Cons
instance Show a => Show (Stream a) where
showsPrec :: Int -> Stream a -> ShowS
showsPrec Int
p (Cons a
x Stream a
xs) =
Bool -> ShowS -> ShowS
showParen (Int
p forall a. Ord a => a -> a -> Bool
> Int
consPrecedence) forall a b. (a -> b) -> a -> b
$
forall a. Show a => Int -> a -> ShowS
showsPrec (Int
consPrecedence forall a. Num a => a -> a -> a
+ Int
1) a
x forall b c a. (b -> c) -> (a -> b) -> a -> c
.
String -> ShowS
showString String
" <:> " forall b c a. (b -> c) -> (a -> b) -> a -> c
.
forall a. Show a => Int -> a -> ShowS
showsPrec Int
consPrecedence Stream a
xs
where
consPrecedence :: Int
consPrecedence = Int
5 :: Int
infixr 5 <:>
(<:>) :: a -> Stream a -> Stream a
<:> :: forall a. a -> Stream a -> Stream a
(<:>) = forall a. a -> Stream a -> Stream a
Cons
head :: Stream a -> a
head :: forall a. Stream a -> a
head (Cons a
x Stream a
_ ) = a
x
tail :: Stream a -> Stream a
tail :: forall a. Stream a -> Stream a
tail (Cons a
_ Stream a
xs) = Stream a
xs
inits :: Stream a -> Stream ([a])
inits :: forall a. Stream a -> Stream [a]
inits Stream a
xs = forall a. a -> Stream a -> Stream a
Cons [] (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (forall a. Stream a -> a
head Stream a
xs forall a. a -> [a] -> [a]
:) (forall a. Stream a -> Stream [a]
inits (forall a. Stream a -> Stream a
tail Stream a
xs)))
tails :: Stream a -> Stream (Stream a)
tails :: forall a. Stream a -> Stream (Stream a)
tails Stream a
xs = forall a. a -> Stream a -> Stream a
Cons Stream a
xs (forall a. Stream a -> Stream (Stream a)
tails (forall a. Stream a -> Stream a
tail Stream a
xs))
map :: (a -> b) -> Stream a -> Stream b
map :: forall a b. (a -> b) -> Stream a -> Stream b
map a -> b
f ~(Cons a
x Stream a
xs) = forall a. a -> Stream a -> Stream a
Cons (a -> b
f a
x) (forall a b. (a -> b) -> Stream a -> Stream b
map a -> b
f Stream a
xs)
intersperse :: a -> Stream a -> Stream a
intersperse :: forall a. a -> Stream a -> Stream a
intersperse a
y ~(Cons a
x Stream a
xs) = forall a. a -> Stream a -> Stream a
Cons a
x (forall a. a -> Stream a -> Stream a
Cons a
y (forall a. a -> Stream a -> Stream a
intersperse a
y Stream a
xs))
interleave :: Stream a -> Stream a -> Stream a
interleave :: forall a. Stream a -> Stream a -> Stream a
interleave ~(Cons a
x Stream a
xs) Stream a
ys = forall a. a -> Stream a -> Stream a
Cons a
x (forall a. Stream a -> Stream a -> Stream a
interleave Stream a
ys Stream a
xs)
scan :: (a -> b -> a) -> a -> Stream b -> Stream a
scan :: forall a b. (a -> b -> a) -> a -> Stream b -> Stream a
scan a -> b -> a
f a
z ~(Cons b
x Stream b
xs) = a
z forall a. a -> Stream a -> Stream a
<:> forall a b. (a -> b -> a) -> a -> Stream b -> Stream a
scan a -> b -> a
f (a -> b -> a
f a
z b
x) Stream b
xs
scan' :: (a -> b -> a) -> a -> Stream b -> Stream a
scan' :: forall a b. (a -> b -> a) -> a -> Stream b -> Stream a
scan' a -> b -> a
f a
z Stream b
xs = a
z forall a. a -> Stream a -> Stream a
<:> (forall a b. (a -> b -> a) -> a -> Stream b -> Stream a
scan' a -> b -> a
f forall a b. (a -> b) -> a -> b
$! (a -> b -> a
f a
z (forall a. Stream a -> a
head Stream b
xs))) (forall a. Stream a -> Stream a
tail Stream b
xs)
scan1 :: (a -> a -> a) -> Stream a -> Stream a
scan1 :: forall a. (a -> a -> a) -> Stream a -> Stream a
scan1 a -> a -> a
f ~(Cons a
x Stream a
xs) = forall a b. (a -> b -> a) -> a -> Stream b -> Stream a
scan a -> a -> a
f a
x Stream a
xs
scan1' :: (a -> a -> a) -> Stream a -> Stream a
scan1' :: forall a. (a -> a -> a) -> Stream a -> Stream a
scan1' a -> a -> a
f ~(Cons a
x Stream a
xs) = forall a b. (a -> b -> a) -> a -> Stream b -> Stream a
scan' a -> a -> a
f a
x Stream a
xs
transpose :: Stream (Stream a) -> Stream (Stream a)
transpose :: forall a. Stream (Stream a) -> Stream (Stream a)
transpose ~(Cons (Cons a
x Stream a
xs) Stream (Stream a)
yss) =
(a
x forall a. a -> Stream a -> Stream a
<:> forall a b. (a -> b) -> Stream a -> Stream b
map forall a. Stream a -> a
head Stream (Stream a)
yss) forall a. a -> Stream a -> Stream a
<:> forall a. Stream (Stream a) -> Stream (Stream a)
transpose (Stream a
xs forall a. a -> Stream a -> Stream a
<:> forall a b. (a -> b) -> Stream a -> Stream b
map forall a. Stream a -> Stream a
tail Stream (Stream a)
yss)
iterate :: (a -> a) -> a -> Stream a
iterate :: forall a. (a -> a) -> a -> Stream a
iterate a -> a
f a
x = forall a. a -> Stream a -> Stream a
Cons a
x (forall a. (a -> a) -> a -> Stream a
iterate a -> a
f (a -> a
f a
x))
prefix :: [a] -> Stream a -> Stream a
prefix :: forall a. [a] -> Stream a -> Stream a
prefix [a]
xs Stream a
ys = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. a -> Stream a -> Stream a
Cons Stream a
ys [a]
xs
repeat :: a -> Stream a
repeat :: forall a. a -> Stream a
repeat a
x = forall a. a -> Stream a -> Stream a
Cons a
x (forall a. a -> Stream a
repeat a
x)
cycle :: [a] -> Stream a
cycle :: forall a. [a] -> Stream a
cycle [a]
xs = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. a -> Stream a -> Stream a
Cons (forall a. [a] -> Stream a
cycle [a]
xs) [a]
xs
unfold :: (c -> (a,c)) -> c -> Stream a
unfold :: forall c a. (c -> (a, c)) -> c -> Stream a
unfold c -> (a, c)
f c
c =
let (a
x,c
d) = c -> (a, c)
f c
c
in forall a. a -> Stream a -> Stream a
Cons a
x (forall c a. (c -> (a, c)) -> c -> Stream a
unfold c -> (a, c)
f c
d)
take :: Int -> Stream a -> [a]
take :: forall a. Int -> Stream a -> [a]
take Int
n ~(Cons a
x Stream a
xs)
| Int
n forall a. Eq a => a -> a -> Bool
== Int
0 = []
| Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = a
x forall a. a -> [a] -> [a]
: (forall a. Int -> Stream a -> [a]
take (Int
n forall a. Num a => a -> a -> a
- Int
1) Stream a
xs)
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"Stream.take: negative argument."
drop :: Int -> Stream a -> Stream a
drop :: forall a. Int -> Stream a -> Stream a
drop Int
n Stream a
xs
| Int
n forall a. Eq a => a -> a -> Bool
== Int
0 = Stream a
xs
| Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = forall a. Int -> Stream a -> Stream a
drop (Int
n forall a. Num a => a -> a -> a
- Int
1) (forall a. Stream a -> Stream a
tail Stream a
xs)
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"Stream.drop: negative argument."
splitAt :: Int -> Stream a -> ([a], Stream a)
splitAt :: forall a. Int -> Stream a -> ([a], Stream a)
splitAt Int
n Stream a
xs
| Int
n forall a. Eq a => a -> a -> Bool
== Int
0 = ([],Stream a
xs)
| Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = let ([a]
prefix,Stream a
rest) = forall a. Int -> Stream a -> ([a], Stream a)
splitAt (Int
nforall a. Num a => a -> a -> a
-Int
1) (forall a. Stream a -> Stream a
tail Stream a
xs)
in (forall a. Stream a -> a
head Stream a
xs forall a. a -> [a] -> [a]
: [a]
prefix, Stream a
rest)
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"Stream.splitAt negative argument."
takeWhile :: (a -> Bool) -> Stream a -> [a]
takeWhile :: forall a. (a -> Bool) -> Stream a -> [a]
takeWhile a -> Bool
p (Cons a
x Stream a
xs)
| a -> Bool
p a
x = a
x forall a. a -> [a] -> [a]
: forall a. (a -> Bool) -> Stream a -> [a]
takeWhile a -> Bool
p Stream a
xs
| Bool
otherwise = []
dropWhile :: (a -> Bool) -> Stream a -> Stream a
dropWhile :: forall a. (a -> Bool) -> Stream a -> Stream a
dropWhile a -> Bool
p ~(Cons a
x Stream a
xs)
| a -> Bool
p a
x = forall a. (a -> Bool) -> Stream a -> Stream a
dropWhile a -> Bool
p Stream a
xs
| Bool
otherwise = forall a. a -> Stream a -> Stream a
Cons a
x Stream a
xs
span :: (a -> Bool) -> Stream a -> ([a], Stream a)
span :: forall a. (a -> Bool) -> Stream a -> ([a], Stream a)
span a -> Bool
p (Cons a
x Stream a
xs)
| a -> Bool
p a
x = let ([a]
trues, Stream a
falses) = forall a. (a -> Bool) -> Stream a -> ([a], Stream a)
span a -> Bool
p Stream a
xs
in (a
x forall a. a -> [a] -> [a]
: [a]
trues, Stream a
falses)
| Bool
otherwise = ([], forall a. a -> Stream a -> Stream a
Cons a
x Stream a
xs)
break :: (a -> Bool) -> Stream a -> ([a], Stream a)
break :: forall a. (a -> Bool) -> Stream a -> ([a], Stream a)
break a -> Bool
p = forall a. (a -> Bool) -> Stream a -> ([a], Stream a)
span (Bool -> Bool
not forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Bool
p)
filter :: (a -> Bool) -> Stream a -> Stream a
filter :: forall a. (a -> Bool) -> Stream a -> Stream a
filter a -> Bool
p ~(Cons a
x Stream a
xs)
| a -> Bool
p a
x = forall a. a -> Stream a -> Stream a
Cons a
x (forall a. (a -> Bool) -> Stream a -> Stream a
filter a -> Bool
p Stream a
xs)
| Bool
otherwise = forall a. (a -> Bool) -> Stream a -> Stream a
filter a -> Bool
p Stream a
xs
partition :: (a -> Bool) -> Stream a -> (Stream a, Stream a)
partition :: forall a. (a -> Bool) -> Stream a -> (Stream a, Stream a)
partition a -> Bool
p ~(Cons a
x Stream a
xs) =
let (Stream a
trues,Stream a
falses) = forall a. (a -> Bool) -> Stream a -> (Stream a, Stream a)
partition a -> Bool
p Stream a
xs
in if a -> Bool
p a
x then (forall a. a -> Stream a -> Stream a
Cons a
x Stream a
trues, Stream a
falses)
else (Stream a
trues, forall a. a -> Stream a -> Stream a
Cons a
x Stream a
falses)
group :: Eq a => Stream a -> Stream [a]
group :: forall a. Eq a => Stream a -> Stream [a]
group ~(Cons a
x Stream a
ys) = let ([a]
xs, Stream a
zs) = forall a. (a -> Bool) -> Stream a -> ([a], Stream a)
span (\a
y -> a
x forall a. Eq a => a -> a -> Bool
== a
y) Stream a
ys
in (a
x forall a. a -> [a] -> [a]
: [a]
xs) forall a. a -> Stream a -> Stream a
<:> forall a. Eq a => Stream a -> Stream [a]
group Stream a
zs
isPrefixOf :: Eq a => [a] -> Stream a -> Bool
isPrefixOf :: forall a. Eq a => [a] -> Stream a -> Bool
isPrefixOf [] Stream a
_ = Bool
True
isPrefixOf (a
y:[a]
ys) (Cons a
x Stream a
xs)
| a
y forall a. Eq a => a -> a -> Bool
== a
x = forall a. Eq a => [a] -> Stream a -> Bool
isPrefixOf [a]
ys Stream a
xs
| Bool
otherwise = Bool
False
(!!) :: Stream a -> Int -> a
!! :: forall a. Stream a -> Int -> a
(!!) (Cons a
x Stream a
xs) Int
n
| Int
n forall a. Eq a => a -> a -> Bool
== Int
0 = a
x
| Int
n forall a. Ord a => a -> a -> Bool
> Int
0 = Stream a
xs forall a. Stream a -> Int -> a
!! (Int
n forall a. Num a => a -> a -> a
- Int
1)
| Bool
otherwise = forall a. HasCallStack => String -> a
error String
"Stream.!! negative argument"
elemIndex :: Eq a => a -> Stream a -> Int
elemIndex :: forall a. Eq a => a -> Stream a -> Int
elemIndex a
x = forall a. (a -> Bool) -> Stream a -> Int
findIndex (\a
y -> a
x forall a. Eq a => a -> a -> Bool
== a
y)
elemIndices :: Eq a => a -> Stream a -> Stream Int
elemIndices :: forall a. Eq a => a -> Stream a -> Stream Int
elemIndices a
x = forall a. (a -> Bool) -> Stream a -> Stream Int
findIndices (a
xforall a. Eq a => a -> a -> Bool
==)
findIndex :: (a -> Bool) -> Stream a -> Int
findIndex :: forall a. (a -> Bool) -> Stream a -> Int
findIndex a -> Bool
p = forall {a}. Num a => a -> Stream a -> a
indexFrom Int
0
where
indexFrom :: a -> Stream a -> a
indexFrom a
ix (Cons a
x Stream a
xs)
| a -> Bool
p a
x = a
ix
| Bool
otherwise = (a -> Stream a -> a
indexFrom forall a b. (a -> b) -> a -> b
$! (a
ix forall a. Num a => a -> a -> a
+ a
1)) Stream a
xs
findIndices :: (a -> Bool) -> Stream a -> Stream Int
findIndices :: forall a. (a -> Bool) -> Stream a -> Stream Int
findIndices a -> Bool
p = forall {a}. Num a => a -> Stream a -> Stream a
indicesFrom Int
0
where
indicesFrom :: a -> Stream a -> Stream a
indicesFrom a
ix (Cons a
x Stream a
xs) =
let ixs :: Stream a
ixs = (a -> Stream a -> Stream a
indicesFrom forall a b. (a -> b) -> a -> b
$! (a
ixforall a. Num a => a -> a -> a
+a
1)) Stream a
xs
in if a -> Bool
p a
x then forall a. a -> Stream a -> Stream a
Cons a
ix Stream a
ixs else Stream a
ixs
zip :: Stream a -> Stream b -> Stream (a,b)
zip :: forall a b. Stream a -> Stream b -> Stream (a, b)
zip ~(Cons a
x Stream a
xs) ~(Cons b
y Stream b
ys) = forall a. a -> Stream a -> Stream a
Cons (a
x,b
y) (forall a b. Stream a -> Stream b -> Stream (a, b)
zip Stream a
xs Stream b
ys)
zip3 :: Stream a -> Stream b -> Stream c -> Stream (a,b,c)
zip3 :: forall a b c. Stream a -> Stream b -> Stream c -> Stream (a, b, c)
zip3 ~(Cons a
x Stream a
xs) ~(Cons b
y Stream b
ys) ~(Cons c
z Stream c
zs) = forall a. a -> Stream a -> Stream a
Cons (a
x,b
y,c
z) (forall a b c. Stream a -> Stream b -> Stream c -> Stream (a, b, c)
zip3 Stream a
xs Stream b
ys Stream c
zs)
zipWith :: (a -> b -> c) -> Stream a -> Stream b -> Stream c
zipWith :: forall a b c. (a -> b -> c) -> Stream a -> Stream b -> Stream c
zipWith a -> b -> c
f ~(Cons a
x Stream a
xs) ~(Cons b
y Stream b
ys) = forall a. a -> Stream a -> Stream a
Cons (a -> b -> c
f a
x b
y) (forall a b c. (a -> b -> c) -> Stream a -> Stream b -> Stream c
zipWith a -> b -> c
f Stream a
xs Stream b
ys)
zipWith3 :: (a -> b -> c -> d) -> Stream a -> Stream b -> Stream c -> Stream d
zipWith3 :: forall a b c d.
(a -> b -> c -> d) -> Stream a -> Stream b -> Stream c -> Stream d
zipWith3 a -> b -> c -> d
f ~(Cons a
x Stream a
xs) ~(Cons b
y Stream b
ys) (Cons c
z Stream c
zs) = forall a. a -> Stream a -> Stream a
Cons (a -> b -> c -> d
f a
x b
y c
z) (forall a b c d.
(a -> b -> c -> d) -> Stream a -> Stream b -> Stream c -> Stream d
zipWith3 a -> b -> c -> d
f Stream a
xs Stream b
ys Stream c
zs)
distribute :: (Functor f) => f (Stream a) -> Stream (f a)
distribute :: forall (f :: * -> *) a. Functor f => f (Stream a) -> Stream (f a)
distribute f (Stream a)
t = forall a. a -> Stream a -> Stream a
Cons (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Stream a -> a
head f (Stream a)
t) (forall (f :: * -> *) a. Functor f => f (Stream a) -> Stream (f a)
distribute (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Stream a -> Stream a
tail f (Stream a)
t))
unzip :: Stream (a,b) -> (Stream a, Stream b)
unzip :: forall a b. Stream (a, b) -> (Stream a, Stream b)
unzip ~(Cons (a
x,b
y) Stream (a, b)
xys) = (forall a. a -> Stream a -> Stream a
Cons a
x (forall a b. (a, b) -> a
fst (forall a b. Stream (a, b) -> (Stream a, Stream b)
unzip Stream (a, b)
xys)),
forall a. a -> Stream a -> Stream a
Cons b
y (forall a b. (a, b) -> b
snd (forall a b. Stream (a, b) -> (Stream a, Stream b)
unzip Stream (a, b)
xys)))
unzip3 :: Stream (a,b,c) -> (Stream a, Stream b, Stream c)
unzip3 :: forall a b c. Stream (a, b, c) -> (Stream a, Stream b, Stream c)
unzip3 ~(Cons (a
x,b
y,c
z) Stream (a, b, c)
xyzs) = ( forall a. a -> Stream a -> Stream a
Cons a
x (forall a b c. (a, b, c) -> a
fst3 (forall a b c. Stream (a, b, c) -> (Stream a, Stream b, Stream c)
unzip3 Stream (a, b, c)
xyzs))
, forall a. a -> Stream a -> Stream a
Cons b
y (forall a b c. (a, b, c) -> b
snd3 (forall a b c. Stream (a, b, c) -> (Stream a, Stream b, Stream c)
unzip3 Stream (a, b, c)
xyzs))
, forall a. a -> Stream a -> Stream a
Cons c
z (forall a b c. (a, b, c) -> c
thd3 (forall a b c. Stream (a, b, c) -> (Stream a, Stream b, Stream c)
unzip3 Stream (a, b, c)
xyzs)))
where
fst3 :: (a,b,c) -> a
fst3 :: forall a b c. (a, b, c) -> a
fst3 (a
x,b
_,c
_) = a
x
snd3 :: (a,b,c) -> b
snd3 :: forall a b c. (a, b, c) -> b
snd3 (a
_,b
y,c
_) = b
y
thd3 :: (a,b,c) -> c
thd3 :: forall a b c. (a, b, c) -> c
thd3 (a
_,b
_,c
z) = c
z
words :: Stream Char -> Stream String
words :: Stream Char -> Stream String
words Stream Char
xs = let (String
w, Stream Char
ys) = forall a. (a -> Bool) -> Stream a -> ([a], Stream a)
break Char -> Bool
isSpace Stream Char
xs
in forall a. a -> Stream a -> Stream a
Cons String
w (Stream Char -> Stream String
words Stream Char
ys)
unwords :: Stream String -> Stream Char
unwords :: Stream String -> Stream Char
unwords ~(Cons String
x Stream String
xs) = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. a -> Stream a -> Stream a
Cons (forall a. a -> Stream a -> Stream a
Cons Char
' ' (Stream String -> Stream Char
unwords Stream String
xs)) String
x
lines :: Stream Char -> Stream String
lines :: Stream Char -> Stream String
lines Stream Char
xs = let (String
l, Stream Char
ys) = forall a. (a -> Bool) -> Stream a -> ([a], Stream a)
break (forall a. Eq a => a -> a -> Bool
== Char
'\n') Stream Char
xs
in forall a. a -> Stream a -> Stream a
Cons String
l (Stream Char -> Stream String
lines (forall a. Stream a -> Stream a
tail Stream Char
ys))
unlines :: Stream String -> Stream Char
unlines :: Stream String -> Stream Char
unlines ~(Cons String
x Stream String
xs) = forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. a -> Stream a -> Stream a
Cons (forall a. a -> Stream a -> Stream a
Cons Char
'\n' (Stream String -> Stream Char
unlines Stream String
xs)) String
x
toList :: Stream a -> [a]
toList :: forall a. Stream a -> [a]
toList (Cons a
x Stream a
xs) = a
x forall a. a -> [a] -> [a]
: forall a. Stream a -> [a]
toList Stream a
xs
fromList :: [a] -> Stream a
fromList :: forall a. [a] -> Stream a
fromList (a
x:[a]
xs) = forall a. a -> Stream a -> Stream a
Cons a
x (forall a. [a] -> Stream a
fromList [a]
xs)
fromList [] = forall a. HasCallStack => String -> a
error String
"Stream.fromList applied to finite list"