Copyright (C) 2011 Edward Kmett(C) 2007-2010 Wouter Swierstra Bas van Dijk BSD-style (see the file LICENSE) Edward Kmett provisional portable (Haskell 2010) Trustworthy Haskell98

Data.Stream.Infinite

Description

Synopsis

# The type of streams

data Stream a #

Constructors

 a :> (Stream a) infixr 5

Instances

 # Methods(>>=) :: Stream a -> (a -> Stream b) -> Stream b #(>>) :: Stream a -> Stream b -> Stream b #return :: a -> Stream a #fail :: String -> Stream a # # Methodsfmap :: (a -> b) -> Stream a -> Stream b #(<$) :: a -> Stream b -> Stream a # # Methodspure :: a -> Stream a #(<*>) :: Stream (a -> b) -> Stream a -> Stream b #(*>) :: Stream a -> Stream b -> Stream b #(<*) :: Stream a -> Stream b -> Stream a # # Methodsfold :: Monoid m => Stream m -> m #foldMap :: Monoid m => (a -> m) -> Stream a -> m #foldr :: (a -> b -> b) -> b -> Stream a -> b #foldr' :: (a -> b -> b) -> b -> Stream a -> b #foldl :: (b -> a -> b) -> b -> Stream a -> b #foldl' :: (b -> a -> b) -> b -> Stream a -> b #foldr1 :: (a -> a -> a) -> Stream a -> a #foldl1 :: (a -> a -> a) -> Stream a -> a #toList :: Stream a -> [a] #null :: Stream a -> Bool #length :: Stream a -> Int #elem :: Eq a => a -> Stream a -> Bool #maximum :: Ord a => Stream a -> a #minimum :: Ord a => Stream a -> a #sum :: Num a => Stream a -> a #product :: Num a => Stream a -> a # # Methodstraverse :: Applicative f => (a -> f b) -> Stream a -> f (Stream b) #sequenceA :: Applicative f => Stream (f a) -> f (Stream a) #mapM :: Monad m => (a -> m b) -> Stream a -> m (Stream b) #sequence :: Monad m => Stream (m a) -> m (Stream a) # # Methodsdistribute :: Functor f => f (Stream a) -> Stream (f a) #collect :: Functor f => (a -> Stream b) -> f a -> Stream (f b) #distributeM :: Monad m => m (Stream a) -> Stream (m a) #collectM :: Monad m => (a -> Stream b) -> m a -> Stream (m b) # # Associated Typestype Rep (Stream :: * -> *) :: * # Methodstabulate :: (Rep Stream -> a) -> Stream a #index :: Stream a -> Rep Stream -> a # # Methodsextract :: Stream a -> a #duplicate :: Stream a -> Stream (Stream a) #extend :: (Stream a -> b) -> Stream a -> Stream b # # Methods(<@>) :: Stream (a -> b) -> Stream a -> Stream b #(@>) :: Stream a -> Stream b -> Stream b #(<@) :: Stream a -> Stream b -> Stream a # # Methodstraverse1 :: Apply f => (a -> f b) -> Stream a -> f (Stream b) #sequence1 :: Apply f => Stream (f b) -> f (Stream b) # # Methods(<.>) :: Stream (a -> b) -> Stream a -> Stream b #(.>) :: Stream a -> Stream b -> Stream b #(<.) :: Stream a -> Stream b -> Stream a # # Methodsduplicated :: Stream a -> Stream (Stream a) #extended :: (Stream a -> b) -> Stream a -> Stream b # # Methodsfold1 :: Semigroup m => Stream m -> m #foldMap1 :: Semigroup m => (a -> m) -> Stream a -> m #toNonEmpty :: Stream a -> NonEmpty a # Data a => Data (Stream a) # Methodsgfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b) -> (forall g. g -> c g) -> Stream a -> c (Stream a) #gunfold :: (forall b r. Data b => c (b -> r) -> c r) -> (forall r. r -> c r) -> Constr -> c (Stream a) #toConstr :: Stream a -> Constr #dataCast1 :: Typeable (* -> *) t => (forall d. Data d => c (t d)) -> Maybe (c (Stream a)) #dataCast2 :: Typeable (* -> * -> *) t => (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (Stream a)) #gmapT :: (forall b. Data b => b -> b) -> Stream a -> Stream a #gmapQl :: (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> Stream a -> r #gmapQr :: (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> Stream a -> r #gmapQ :: (forall d. Data d => d -> u) -> Stream a -> [u] #gmapQi :: Int -> (forall d. Data d => d -> u) -> Stream a -> u #gmapM :: Monad m => (forall d. Data d => d -> m d) -> Stream a -> m (Stream a) #gmapMp :: MonadPlus m => (forall d. Data d => d -> m d) -> Stream a -> m (Stream a) #gmapMo :: MonadPlus m => (forall d. Data d => d -> m d) -> Stream a -> m (Stream a) # Show a => Show (Stream a) # MethodsshowsPrec :: Int -> Stream a -> ShowS #show :: Stream a -> String #showList :: [Stream a] -> ShowS # type Rep Stream # type Rep Stream = Int # Basic functions tail :: Stream a -> Stream a # Extract the sequence following the head of the stream. inits :: Stream a -> Stream [a] # The inits function takes a stream xs and returns all the finite prefixes of xs. Note that this inits is lazier then Data.List.inits: inits _|_ = [] ::: _|_ while for Data.List.inits: inits _|_ = _|_ prepend :: Foldable f => f a -> Stream a -> Stream a # Prepend a list to a stream. concat :: Foldable f => Stream (f a) -> Stream a # Flatten a stream of lists into a stream. # Stream transformations intersperse :: a -> Stream a -> Stream a # intersperse y xs creates an alternating stream of elements from xs and y. interleave :: Stream a -> Stream a -> Stream a # Interleave two Streams xs and ys, alternating elements from each list. [x1,x2,...] interleave [y1,y2,...] == [x1,y1,x2,y2,...] scanl :: (a -> b -> a) -> a -> Stream b -> Stream a # scanl yields a stream of successive reduced values from: scanl f z [x1, x2, ...] == [z, z f x1, (z f x1) f x2, ...] scanl' :: (a -> b -> a) -> a -> Stream b -> Stream a # scanl yields a stream of successive reduced values from: scanl f z [x1, x2, ...] == [z, z f x1, (z f x1) f x2, ...] scanl1 :: (a -> a -> a) -> Stream a -> Stream a # scanl1 is a variant of scanl that has no starting value argument: scanl1 f [x1, x2, ...] == [x1, x1 f x2, ...] scanl1' :: (a -> a -> a) -> Stream a -> Stream a # scanl1' is a strict scanl that has no starting value. transpose :: Stream (Stream a) -> Stream (Stream a) # transpose computes the transposition of a stream of streams. # Building streams iterate :: (a -> a) -> a -> Stream a # iterate f x produces the infinite sequence of repeated applications of f to x. iterate f x = [x, f x, f (f x), ..] cycle :: NonEmpty a -> Stream a # cycle xs returns the infinite repetition of xs: cycle [1,2,3] = Cons 1 (Cons 2 (Cons 3 (Cons 1 (Cons 2 ... unfold :: (a -> (b, a)) -> a -> Stream b # The unfold function is similar to the unfold for lists. Note there is no base case: all streams must be infinite. # Extracting sublists take :: Int -> Stream a -> [a] # take n xs returns the first n elements of xs. Beware: passing a negative integer as the first argument will cause an error. drop :: Int -> Stream a -> Stream a # 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. splitAt :: Int -> Stream a -> ([a], Stream a) # splitAt n xs 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. takeWhile :: (a -> Bool) -> Stream a -> [a] # takeWhile p xs returns the longest prefix of the stream xs for which the predicate p holds. dropWhile :: (a -> Bool) -> Stream a -> Stream a # 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. span :: (a -> Bool) -> Stream a -> ([a], Stream a) # span p xs returns the longest prefix of xs that satisfies p, together with the remainder of the stream. break :: (a -> Bool) -> Stream a -> ([a], Stream a) # The break p function is equivalent to span not . p. filter :: (a -> Bool) -> Stream a -> Stream a # 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. partition :: (a -> Bool) -> Stream a -> (Stream a, Stream a) # The partition function takes a predicate p and a stream 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. group :: Eq a => Stream a -> Stream (NonEmpty a) # The group function takes a stream and returns a stream of lists such that flattening the resulting stream is equal to the argument. Moreover, each sublist in the resulting stream contains only equal elements. For example, group$ cycle "Mississippi" = "M" ::: "i" ::: "ss" ::: "i" ::: "ss" ::: "i" ::: "pp" ::: "i" ::: "M" ::: "i" ::: ...

groupBy :: (a -> a -> Bool) -> Stream a -> Stream (NonEmpty a) #

# Sublist predicates

isPrefixOf :: Eq a => [a] -> Stream a -> Bool #

The isPrefix function returns True if the first argument is a prefix of the second.

# Indexing streams

(!!) :: Stream a -> Int -> a #

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.

elemIndex :: Eq a => a -> Stream a -> Int #

The elemIndex function returns the index of the first element in the given stream which is equal (by ==) to the query element,

Beware: elemIndex x xs will diverge if none of the elements of xs equal x.

elemIndices :: Eq a => a -> Stream a -> Stream Int #

The elemIndices function extends elemIndex, by returning the indices of all elements equal to the query element, in ascending order.

Beware: elemIndices x xs will diverge if any suffix of xs does not contain x.

findIndex :: (a -> Bool) -> Stream a -> Int #

The findIndex function takes a predicate and a stream and returns the index of the first element in the stream that satisfies the predicate,

Beware: findIndex p xs will diverge if none of the elements of xs satisfy p.

findIndices :: (a -> Bool) -> Stream a -> Stream Int #

The findIndices function extends findIndex, by returning the indices of all elements satisfying the predicate, in ascending order.

Beware: findIndices p xs will diverge if all the elements of any suffix of xs fails to satisfy p.

# Zipping and unzipping streams

zip :: Stream a -> Stream b -> Stream (a, b) #

The zip function takes two streams and returns a list of corresponding pairs.

zipWith :: (a -> b -> c) -> Stream a -> Stream b -> Stream c #

The zipWith function generalizes zip. Rather than tupling the functions, the elements are combined using the function passed as the first argument to zipWith.

unzip :: Stream (a, b) -> (Stream a, Stream b) #

The unzip function is the inverse of the zip function.

# Functions on streams of characters

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.

The unwords function is an inverse operation to words. It joins words with separating spaces.

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.

The unlines function is an inverse operation to lines. It joins lines, after appending a terminating newline to each.