Module Data.Stream

Implementation of and functions for the Stream data type

Imports

Table of Content

Definitions

data Stream a

Streams are infinite lists. Most operations on streams are completely analogous to the definitions for [].

Beware: If you use any function from the Eq or Ord class to compare two equal streams, these functions will diverge.

Constructors

Cons a   (Stream a)
infixr  13
<:>aStream a → Stream a
infixr  13

The <:> operator is a left strict infix version of the Stream.Cons constructor.

In this module, it will always be used when the head part of a Stream result is computed with some function passed as argument.

This way, the strictness of the passed function is indirectly taken in account.

For example, in the result of

 zipWith (,) stream1 stream2

the data of stream1 and stream2 are not evaluated, because the tuple constructor doesn't do it.

Hence

 let us = repeat undefined in length • take 10 • zipWith (,) us $ us

will be 10. Whereas

 let us = repeat undefined in length • take 10 • zipWith (+) us $ us

will be undefined.

initsStream 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 inits:

 inits _|_ = [] ::: _|_

while for inits:

 inits _|_ = _|_
tailsStream a → Stream (Stream a)

The tails function takes a stream xs and returns all the suffixes of xs.

map ∷ (a → b) → Stream aStream b

Apply a function uniformly over all elements of a sequence.

intersperse ∷ a → Stream aStream a

intersperse y xs creates an alternating stream of elements from xs and y

interleaveStream aStream a → Stream a

Interleave two Streams xs and ys, alternating elements from each list.

x1,x2,...
`interleave` [y1,y2,...] == [x1,y1,x2,y2,...]
scan ∷ (a → b → a) → aStream bStream a

scan yields a stream of successive reduced values from:

scan f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...]

scan' ∷ (a → b → a) → aStream b → Stream a

scan' is a strict scan.

scan1 ∷ (a → a → a) → Stream aStream a

scan1 is a variant of scan that has no starting value argument:

scan1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]

scan1' ∷ (a → a → a) → Stream aStream a

scan1' is a strict scan that has no starting value.

transposeStream (Stream a)Stream (Stream a)

transpose computes the transposition of a stream of streams.

iterate ∷ (a → a) → aStream a

iterate f x function produces the infinite sequence of repeated applications of f to x.

iterate f x = [x, f x, f (f x), ..]

repeat ∷ a → Stream a

repeat x returns a constant stream, where all elements are equal to x.

cycle[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 ∷ (c → (a, c)) → c → Stream a

The unfold function is similar to the unfold for lists. Note there is no base case: all streams must be infinite.

splitAtIntStream a → ([a], Stream a)

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.

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 aStream 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 aStream 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.

groupEq a ⇒ Stream aStream [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" ::: ...

isPrefixOfEq a ⇒ [a]Stream a → Bool

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

!!Stream aInt → 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.

elemIndexEq a ⇒ a → Stream aInt

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

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

elemIndicesEq a ⇒ a → Stream aStream 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 aInt

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 aStream 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.

zipStream aStream bStream (a, b)

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

zipWith ∷ (a → b → c) → Stream aStream bStream 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.

unzipStream (a, b) → (Stream a, Stream b)

The unzip function is the inverse of the zip function.

wordsStream CharStream String

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.

unwordsStream StringStream Char

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

linesStream CharStream String

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.

unlinesStream StringStream Char

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

fromList[a]Stream a

The fromList converts an infinite list to a stream.

Beware: Passing a finite list, will cause an error.

Instances

instance Eq a ⇒ Eq (Stream a)

Member Functions

!=Eq α ⇒ Stream αStream αBool
infix  7

inherited from Eq.!=

==Eq α ⇒ Stream αStream αBool
infix  7

Function generated for derived instance.

hashCodeEq α ⇒ Stream αInt

Function generated for derived instance.

instance ListSource Stream

toList converts a stream into an infinite list.

Member Functions

toListStream α → [α]
instance ListView Stream

Member Functions

dropIntStream αStream α

inherited from ListView.drop

emptyStream α
headStream α → α

inherited from ListView.head

lengthStream α → Int
nullStream α → Bool
tailStream αStream α

inherited from ListView.tail

takeIntStream αStream α
unconsStream αMaybe (α, Stream α)
instance Monad Stream

Member Functions

*>Stream αStream βStream β
infixl  4

inherited from Applicative.*>

<*Stream αStream βStream α
infixl  4

inherited from Applicative.<*

<*>Stream (α→β)Stream αStream β
infixl  4
>>Stream αStream β → Stream β
infixl  3

inherited from Monad.>>

>>=Stream α → (α → Stream β) → Stream β
infixl  3
fmap ∷ (α → β) → Stream αStream β
infixl  4
joinStream (Stream α)Stream α
pure ∷ α → Stream α
instance Monoid a ⇒ Monoid (Stream a)

Member Functions

mconcatMonoid α ⇒ [Stream α]Stream α

inherited from Monoid.mconcat

memptyMonoid α ⇒ Stream α
mtimesMonoid α ⇒ IntStream α → Stream α

inherited from Monoid.mtimes

instance Ord a ⇒ Ord (Stream a)

Member Functions

<Ord α ⇒ Stream αStream αBool
infix  9

inherited from Ord.<

<=Ord α ⇒ Stream αStream αBool
infix  9

inherited from Ord.<=

<=>Ord α ⇒ Stream αStream αOrdering
infix  8

Function generated for derived instance.

>Ord α ⇒ Stream αStream αBool
infix  9

inherited from Ord.>

>=Ord α ⇒ Stream αStream αBool
infix  9

inherited from Ord.>=

compareOrd α ⇒ Stream αStream αOrdering
infix  8

inherited from Ord.compare

maxOrd α ⇒ Stream αStream αStream α

inherited from Ord.max

minOrd α ⇒ Stream αStream αStream α

inherited from Ord.min

instance Semigroup a ⇒ Semigroup (Stream a)

Member Functions

mappendSemigroup α ⇒ Stream αStream αStream α
infixr  13
sconcatSemigroup α ⇒ [Stream α]Stream α

inherited from Semigroup.sconcat

stimesSemigroup α ⇒ IntStream α → Stream α

inherited from Semigroup.stimes

instance Show a ⇒ Show (Stream a)

A Show instance for Streams. Note that Show.show returns an infinite String. Hence you can't use this function on old fashioned computers with finite memory.

Member Functions

displayShow α ⇒ Stream αString

inherited from Show.display

showShow α ⇒ Stream αString
showCharsShow α ⇒ Stream α → [Char]

inherited from Show.showChars

showListShow α ⇒ [Stream α]StringString

inherited from Show.showList

showsPrecShow α ⇒ IntStream αStringString

inherited from Show.showsPrec

showsubShow α ⇒ Stream αString

inherited from Show.showsub

Functions and Values by Type

Stream StringStream Char

unlines, unwords

Stream CharStream String

lines, words

(a → a → a) → Stream a → Stream a

scan1, scan1'

(a → Bool) → Stream a → (Stream a, Stream a)

partition

(a → Bool) → Stream a → ([a], Stream a)

break, span

(a → Bool) → Stream a → Stream Int

findIndices

(a → Bool) → Stream a → Stream a

dropWhile, filter

(a → Bool) → Stream a → [a]

takeWhile

(a → Bool) → Stream a → Int

findIndex

(a → a) → a → Stream a

iterate

Stream (Stream a) → Stream (Stream a)

transpose

Stream (Stream α) → Stream α

Monad_Stream.join

Stream a → Stream a → Stream a

interleave

Stream a → Int → a

!!

Stream a → Stream (Stream a)

tails

Stream a → Stream [a]

inits

Stream α → Stream α

ListView_Stream.tail

Stream α → Maybe (α, Stream α)

ListView_Stream.uncons

Stream α → [α]

ListSource_Stream.toList

Stream α → Bool

ListView_Stream.null

Stream α → Int

ListView_Stream.length

Stream α → α

ListView_Stream.head

[a] → Stream a

cycle, fromList

IntStream a → ([a], Stream a)

splitAt

IntStream α → Stream α

ListView_Stream.take, ListView_Stream.drop

a → Stream a → Stream a

<:>, intersperse, Stream.Cons

a → Stream a

repeat

α → Stream α

Monad_Stream.pure

Monoid α ⇒ [Stream α] → Stream α

Monoid_Stream.mconcat

Monoid α ⇒ IntStream α → Stream α

Monoid_Stream.mtimes

Semigroup α ⇒ Stream α → Stream α → Stream α

Semigroup_Stream.mappend

Semigroup α ⇒ [Stream α] → Stream α

Semigroup_Stream.sconcat

Semigroup α ⇒ IntStream α → Stream α

Semigroup_Stream.stimes

Eq a ⇒ Stream a → Stream [a]

group

Eq a ⇒ [a] → Stream a → Bool

isPrefixOf

Eq a ⇒ a → Stream a → Stream Int

elemIndices

Eq a ⇒ a → Stream a → Int

elemIndex

Eq α ⇒ Stream α → Stream α → Bool

Eq_Stream.!=, Eq_Stream.==

Eq α ⇒ Stream α → Int

Eq_Stream.hashCode

Ord α ⇒ Stream α → Stream α → Stream α

Ord_Stream.min, Ord_Stream.max

Ord α ⇒ Stream α → Stream α → Bool

Ord_Stream.>=, Ord_Stream.<, Ord_Stream.<=, Ord_Stream.>

Ord α ⇒ Stream α → Stream α → Ordering

Ord_Stream.compare, Ord_Stream.<=>

Show α ⇒ Stream α → String

Show_Stream.showsub, Show_Stream.display, Show_Stream.show

Show α ⇒ Stream α → [Char]

Show_Stream.showChars

Show α ⇒ [Stream α] → StringString

Show_Stream.showList

Show α ⇒ IntStream α → StringString

Show_Stream.showsPrec

Stream α

ListView_Stream.empty

Monoid α ⇒ Stream α

Monoid_Stream.mempty

(a → b → a) → a → Stream b → Stream a

scan, scan'

(a → b) → Stream a → Stream b

map

(c → (a, c)) → c → Stream a

unfold

(α → β) → Stream α → Stream β

Monad_Stream.fmap

Stream (a, b) → (Stream a, Stream b)

unzip

Stream (α→β) → Stream α → Stream β

Monad_Stream.<*>

Stream a → Stream b → Stream (a, b)

zip

Stream α → (α → Stream β) → Stream β

Monad_Stream.>>=

Stream α → Stream β → Stream α

Monad_Stream.<*

Stream α → Stream β → Stream β

Monad_Stream.>>, Monad_Stream.*>

(a → b → c) → Stream a → Stream b → Stream c

zipWith

Valid HTML 4.01 Strict