Stream 0.2.2 → 0.2.3
raw patch · 2 files changed
+25/−18 lines, 2 filesdep +QuickCheckPVP ok
version bump matches the API change (PVP)
Dependencies added: QuickCheck
API changes (from Hackage documentation)
+ Data.Stream: instance (Arbitrary a) => Arbitrary (Stream a)
Files
- Data/Stream.hs +22/−16
- Stream.cabal +3/−2
Data/Stream.hs view
@@ -4,16 +4,16 @@ module Data.Stream ( -- * The type of streams- Stream(..) + Stream(..) -- * Basic functions , (<:>)- , head + , head , tail , inits , tails -- * Stream transformations , map- , intersperse + , intersperse , interleave , scanl , scanl1@@ -21,7 +21,7 @@ , iterate , repeat , cycle- , unfold + , unfold -- * Extracting sublists , take , drop@@ -58,6 +58,8 @@ import Control.Applicative import Data.Char (isSpace)+import Control.Monad (liftM2)+import Test.QuickCheck -- | An infinite sequence. data Stream a = Cons a (Stream a) deriving (Show, Eq)@@ -75,6 +77,10 @@ return = repeat (Cons x xs) >>= f = Cons (head (f x)) (tail (xs >>= f)) +instance Arbitrary a => Arbitrary (Stream a) where+ arbitrary = liftM2 Cons arbitrary arbitrary+ coarbitrary (Cons x xs) = coarbitrary x . coarbitrary xs+ infixr 5 <:> -- | The @ \<:\> @ operator is an infix version of the 'Cons' -- constructor.@@ -108,20 +114,20 @@ -- | The unfold function is similar to the unfold for lists. Note -- there is no base case: all streams must be infinite. unfold :: (c -> (a,c)) -> c -> Stream a-unfold f c = - let (x,d) = f c +unfold f c =+ let (x,d) = f c in Cons x (unfold f d) -- | 'iterate' @f@ @x@ function produces the infinite sequence -- of repeated applications of @f@ to @x@.--- +-- -- > iterate f x = [x, f x, f (f x), ..] iterate :: (a -> a) -> a -> Stream a iterate f x = Cons x (iterate f (f x)) -- | 'scanl' yields a stream of successive reduced values from the -- | left:--- +-- -- > scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...] scanl :: (a -> b -> a) -> a -> Stream b -> Stream a scanl f z (Cons x xs) = z <:> scanl f (f z x) xs@@ -133,7 +139,7 @@ scanl1 f (Cons x xs) = scanl f x xs -- | 'take' @n@ @xs@ returns the first @n@ elements of @xs@.--- +-- -- /Beware/: passing a negative integer as the first argument will -- cause an error. take :: Int -> Stream a -> [a]@@ -144,7 +150,7 @@ -- | 'drop' @n@ @xs@ drops the first @n@ elements off the front of -- the sequence @xs@.--- +-- -- /Beware/: passing a negative integer as the first argument will -- cause an error. drop :: Int -> Stream a -> Stream a@@ -182,7 +188,7 @@ cycle xs = foldr Cons (cycle xs) xs -- | 'filter' @p@ @xs@, removes any elements from @xs@ that do not satisfy @p@.--- +-- -- /Beware/: this function may diverge if there is no element of -- @xs@ that satisfies @p@, e.g. @filter odd (repeat 0)@ will loop. filter :: (a -> Bool) -> Stream a -> Stream a@@ -192,7 +198,7 @@ -- | @xs !! n@ returns the element of the stream @xs@ at index -- @n@. Note that the head of the stream has index 0.--- +-- -- /Beware/: passing a negative integer as the first argument will cause -- an error. (!!) :: Stream a -> Int -> a@@ -209,7 +215,7 @@ -- | The 'unzip' function is the inverse of the 'zip' function. unzip :: Stream (a,b) -> (Stream a, Stream b) unzip (Cons (x,y) xys) = (Cons x (fst (unzip xys)),- Cons y (snd (unzip xys))) + Cons y (snd (unzip xys))) -- | The 'zipWith' function generalizes 'zip'. Rather than tupling -- the functions, the elements are combined using the function@@ -218,7 +224,7 @@ zipWith f (Cons x xs) (Cons y ys) = Cons (f x y) (zipWith f xs ys) -- | 'span' @p@ @xs@ returns the longest prefix of @xs@ that satisfies--- @p@, together with the remainder of the stream. +-- @p@, together with the remainder of the stream. span :: (a -> Bool) -> Stream a -> ([a], Stream a) span p (Cons x xs) | p x = let (trues, falses) = span p xs@@ -275,7 +281,7 @@ -- example, @fst (partition even (repeat 0)) == repeat 0@; on the -- other hand @snd (partition even (repeat 0))@ is undefined. partition :: (a -> Bool) -> Stream a -> (Stream a, Stream a)-partition p (Cons x xs) = +partition p (Cons x xs) = let (trues,falses) = partition p xs in if p x then (Cons x trues, falses) else (trues, Cons x falses)@@ -308,7 +314,7 @@ streamToList (Cons x xs) = x : streamToList xs -- | The 'listToStream' converts an infinite list to a--- stream. +-- stream. -- -- /Beware/: Passing a finite list, will cause an error. listToStream :: [a] -> Stream a
Stream.cabal view
@@ -1,5 +1,5 @@ Name: Stream-Version: 0.2.2+Version: 0.2.3 License: BSD3 License-file: LICENSE Author: Wouter Swierstra@@ -9,6 +9,7 @@ Description: This package implements functions, analogous to those from Data.List, to create and manipulate infinite lists. Category: Data-Build-Depends: base+Build-Depends: base, QuickCheck < 2+Build-Type: Simple Exposed-modules: Data.Stream