Stream 0.1 → 0.1.1
raw patch · 2 files changed
+45/−14 lines, 2 filesPVP: major bump suggested
API removals or changes: PVP suggests a major version bump
API changes (from Hackage documentation)
- Data.Stream: drop :: (Num a, Ord a) => a -> Stream a1 -> Stream a1
+ Data.Stream: drop :: Int -> Stream a -> Stream a
Files
- Data/Stream.hs +42/−11
- Stream.cabal +3/−3
Data/Stream.hs view
@@ -66,7 +66,6 @@ return = repeat (Cons x xs) >>= f = Cons (head (f x)) (tail (xs >>= f)) - -- | Extract the first element of the sequence. head :: Stream a -> a head (Cons x _ ) = x@@ -75,6 +74,8 @@ tail :: Stream a -> Stream a tail (Cons _ xs) = xs +-- | 'intersperse' @y@ @xs@ creates an alternating stream of+-- elements from @xs@ and @y@. intersperse :: a -> Stream a -> Stream a intersperse y (Cons x xs) = Cons x (Cons y (intersperse y xs)) @@ -82,7 +83,8 @@ map :: (a -> b) -> Stream a -> Stream b map f (Cons x xs) = Cons (f x) (map f xs) --- | The unfold function is similar to the unfold for lists. Note there is no base case: all streams must be infinite.+-- | 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 @@ -92,11 +94,13 @@ -- 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)) --- | 'take' @n@ @xs@ returns the first @n@ elements of @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] take n (Cons x xs) | n == 0 = []@@ -104,18 +108,25 @@ | otherwise = error "Stream.take: negative argument." -- | '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 drop n xs | n == 0 = xs | n > 0 = drop (n - 1) (tail xs) | otherwise = error "Stream.drop: negative argument." --- | 'takeWhile' @p@ @xs@ returns the longest prefix of the stream @xs@ for which the predicate @p@ holds.+-- | 'takeWhile' @p@ @xs@ returns the longest prefix of the stream+-- @xs@ for which the predicate @p@ holds. takeWhile :: (a -> Bool) -> Stream a -> [a] takeWhile p (Cons x xs) | p x = x : takeWhile p xs | otherwise = [] -- | 'dropWhile' @p@ @xs@ returns the suffix remaining after 'takeWhile' @p@ @xs@.+--+-- /Beware/: this function may diverge if every element of @xs@ satisfies @p@, e.g.+-- @dropWhile even (repeat 0)@ will loop. dropWhile :: (a -> Bool) -> Stream a -> Stream a dropWhile p (Cons x xs) | p x = dropWhile p xs@@ -132,6 +143,9 @@ 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 filter p (Cons x xs) | p x = Cons x (filter p xs)@@ -139,6 +153,9 @@ -- | @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. (!!) :: Int -> Stream a -> a (!!) n (Cons x xs) | n == 0 = x@@ -174,6 +191,9 @@ -- | The 'words' function breaks a stream of characters into a stream of words, -- which were delimited by white space.+--+-- /Beware/: if the stream of characters @xs@ does not contain white+-- space, accessing the tail of @words xs@ will loop. words :: Stream Char -> Stream String words xs = let (w, ys) = break isSpace xs in Cons w (words ys)@@ -186,6 +206,9 @@ -- | The 'lines' function breaks a stream of characters into a list -- of strings at newline characters. The resulting strings do not -- contain newlines.+--+-- /Beware/: if the stream of characters @xs@ does not contain+-- newline characters, accessing the tail of @lines xs@ will loop. lines :: Stream Char -> Stream String lines xs = let (l, ys) = break (== '\n') xs in Cons l (lines (tail ys))@@ -195,8 +218,8 @@ unlines :: Stream String -> Stream Char unlines (Cons x xs) = foldr Cons (Cons '\n' (unlines xs)) x ---- | The 'isPrefix' function returns @True@ if the first argument is a prefix of the second.+-- | The 'isPrefix' function returns @True@ if the first argument is+-- a prefix of the second. isPrefixOf :: Eq a => [a] -> Stream a -> Bool isPrefixOf [] _ = True isPrefixOf (y:ys) (Cons x xs)@@ -207,6 +230,10 @@ -- @xs@, and returns a pair of streams. The first stream corresponds -- to the elements of @xs@ for which @p@ holds; the second stream -- corresponds to the elements of @xs@ for which @p@ does not hold.+--+-- /Beware/: One of the elements of the tuple may be undefined. For+-- 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) = let (trues,falses) = partition p xs@@ -223,10 +250,12 @@ tails :: Stream a -> Stream (Stream a) tails xs = Cons xs (tails (tail xs)) - -- | The 'splitAt' function takes an integer @n@ and a stream @xs@--- | and returns a pair consisting of the prefix of @xs@ of length--- | @n@ and the remaining stream immediately following this prefix.+-- and returns a pair consisting of the prefix of @xs@ of length+-- @n@ and the remaining stream immediately following this prefix.+--+-- /Beware/: passing a negative integer as the first argument will+-- cause an error. splitAt :: Int -> Stream a -> ([a], Stream a) splitAt n xs | n == 0 = ([],xs)@@ -239,7 +268,9 @@ streamToList (Cons x xs) = x : streamToList xs -- | The 'listToStream' converts an infinite list to a--- stream. Passing a finite list will result in an error.+-- stream. +--+-- /Beware/: Passing a finite list, will cause an error. listToStream :: [a] -> Stream a listToStream (x:xs) = Cons x (listToStream xs) listToStream [] = error "Stream.listToStream applied to finite list"
Stream.cabal view
@@ -1,13 +1,13 @@ Name: Stream-Version: 0.1+Version: 0.1.1 License: BSD3 License-file: LICENSE Author: Wouter Swierstra Maintainer: Wouter Swierstra <wss@cs.nott.ac.uk> Homepage: http://www.cs.nott.ac.uk/~wss/repos/Stream/dist/doc/html/ Synopsis: A library for manipulating infinite lists.-Description: This package implements quite a few functions analogous- to those from Data.List to create and manipulate infinite lists.+Description: This package implements functions, analogous+ to those from Data.List, to create and manipulate infinite lists. Category: Data Build-Depends: base Exposed-modules: Data.Stream