interstar
interstar

Reputation: 27246

Racket : How can I turn a stream into a list?

In Racket, how can I turn a stream into a list?

I assumed that there'd be a common interface, but it seems that list oriented functions like map don't work on streams. So how can I turn them into lists?

Upvotes: 1

Views: 1438

Answers (3)

Jack
Jack

Reputation: 2273

In addition to stream->list, there's also a more general sequence->list that turns any sequence? into a list.

> (sequence->list (stream 1 2 3))
'(1 2 3)
> (sequence->list "abc")
'(#\a #\b #\c)
> (sequence->list (set 1 2 3))
'(1 2 3)

Upvotes: 1

Óscar López
Óscar López

Reputation: 236170

There's a procedure for that: stream->list. For example:

(define s (stream 1 2 3 4 5))
(stream->list s)
=> '(1 2 3 4 5)

Make sure to check the documentation, there are several procedures for manipulating streams that mirror the ones available for lists.

Upvotes: 4

Eli Barzilay
Eli Barzilay

Reputation: 29554

There's a straightforwardly-named stream->list function. It's provided from the racket/stream library, and you can see many other list-like functions for streams, including stream-map.

(But if you're using that, note that this library can have severe performance penalties over using streams as is.)

Upvotes: 3

Related Questions