Next: , Previous: , Up: streams   [Index]


1.15.11 Mapping and side effects

Function: stream-map proc stream ...

Apply a procedure element–wise to corresponding elements of the input streams, returning a newly–allocated stream containing elements that are the results of those procedure applications. The output stream has as many elements as the minimum–length input stream, and may be infinite.

Example:

(define (square x)
  (* x x))

(stream-map square (stream 9 3))
⇒ 81 9

(define (sigma f m n)
  (stream-fold + 0
    (stream-map f (stream-range m (+ n 1)))))

(sigma square 1 100)
⇒ 338350
Function: stream-for-each proc stream ...

Apply a procedure element–wise to corresponding elements of the input streams for its side–effects; return nothing. stream-for-each stops as soon as any of its input streams is exhausted.

Example, the following procedure displays the contents of a file:

(define (display-file filename)
  (stream-for-each display
    (file->stream filename)))