6 Streams
(require pfds/stream) | package: pfds |
Streams are nothing but lazy lists. They are similar to ordinary lists and they provide the same functionality as that of lists. The difference between Streams and lists is that they are lazy in nature and each cell of a Stream is suspended and is forced only when required. Streams have been used in some of the below mentioned data structures. Since each suspension comes with a little overhead, Streams should be used only when there is a good enough reason to do so.
syntax
(Stream A)
> (stream 1 2 3 4 5 6)
- : (Rec
g328657
(U (Boxof (U (-> (Pairof Integer g328657)) (Pairof Integer g328657)))
Null))
'#&(1 . #&(2 . #&(3 . #&(4 . #&(5 . #&(6))))))
In the above example, the stream obtained will be similar to lists but will lazy in nature. It will have 1 as its first element.
value
empty-stream : (Stream Nothing)
procedure
(empty-stream? strm) → Boolean
strm : (Stream A)
> (empty-stream? (stream 1 2 3 4 5 6)) - : Boolean
#f
> (empty-stream? empty-stream) - : Boolean
#t
procedure
(stream-cons a strm) → (Stream A)
a : A strm : (Stream A)
> (stream-cons 10 (stream 1 2 3 4 5 6))
- : (Boxof
(-> (Pairof
Positive-Byte
(Rec
g328681
(U (Boxof
(U (-> (Pairof Integer g328681)) (Pairof Integer g328681)))
Null)))))
'#&#<procedure:...fds/pfds/stream.rkt:40:7>
In the above example, (stream-cons 10 (stream 1 2 3 4 5 6)) returns the stream (stream 10 1 2 3 4 5 6).
procedure
(stream-car strm) → A
strm : (Stream A)
> (stream-car (stream 1 2 3 4 5 6)) - : Integer
1
> (stream-car empty-stream) stream-car: given stream is empty
procedure
(stream-cdr strm) → (Stream A)
strm : (Stream A)
> (stream-cdr (stream 1 2 3 4 5 6))
- : (Rec
g328703
(U (Boxof (U (-> (Pairof Integer g328703)) (Pairof Integer g328703)))
Null))
'#&(2 . #&(3 . #&(4 . #&(5 . #&(6)))))
> (stream-cdr empty-stream) stream-cdr: given stream is empty
In the above example, (stream-cdr strm) returns (stream 2 3 4 5 6).
procedure
(stream-append strm1 strm2) → (Stream A)
strm1 : (Stream A) strm2 : (Stream A)
> (define strm1 (stream 1 2 3 4 5 6)) > (define strm2 (stream 51 32 42)) > (stream-append strm1 strm2)
- : (Rec
g328725
(U (Boxof (U (-> (Pairof Integer g328725)) (Pairof Integer g328725)))
Null))
'#&#<procedure:...fds/pfds/stream.rkt:40:7>
In the above example, (stream-append strm1 strm2) returns the stream, (stream 1 2 3 4 5 6 51 32 42).
procedure
(stream-reverse strm) → (Stream A)
strm : (Stream A)
> (stream-reverse (stream 1 2 3 4 5 6))
- : (Rec
g328734
(U (Boxof (U (-> (Pairof Integer g328734)) (Pairof Integer g328734)))
Null))
'#&#<procedure:...fds/pfds/stream.rkt:40:7>
In the above example, (stream-reverse (stream 1 2 3 4 5 6)) returns (stream 6 5 4 3 2 1).
procedure
(stream->list strm) → (Listof A)
strm : (Stream A)
> (stream->list (stream 1 2 3 4 5 6)) - : (Listof Integer)
'(1 2 3 4 5 6)
> (stream->list empty-stream) - : (Listof Nothing)
'()
> (drop 3 (stream 1 2 3 4 5 6))
- : (Rec
g328756
(U (Boxof (U (-> (Pairof Integer g328756)) (Pairof Integer g328756)))
Null))
'#&(4 . #&(5 . #&(6)))
> (drop 10 (stream 1 2 3 4 5 6)) drop: not enough elements to drop
In the above example, (drop 3 (stream 1 2 3 4 5 6)) returns (stream 4 5 6).
(take 5 (stream 1)) does not throw any error because of its lazy nature. take returns a suspension rather than finishing the whole computation.
> (take 3 (stream 1 2 3 4 5 6))
- : (Rec
g328774
(U (Boxof (U (-> (Pairof Integer g328774)) (Pairof Integer g328774)))
Null))
'#&#<procedure:...fds/pfds/stream.rkt:40:7>
> (take 5 (stream 1))
- : (Rec
g328783
(U (Boxof (U (-> (Pairof Integer g328783)) (Pairof Integer g328783)))
Null))
'#&#<procedure:...fds/pfds/stream.rkt:40:7>
In the above example, (take 3 (stream 1 2 3 4 5 6)) returns (stream 1 2 3).