# Enumerated stream

**URL:** <https://racket.discourse.group/t/enumerated-stream/2790>\
**Category:** Questions & Answers\
**Created:** [March 14, 2024, 7:05am UTC](https://racket.discourse.group/t/enumerated-stream/2790 "2024-03-14T07:05:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyrn](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/tyrn/32/1731_2.png) [@Tyrn](https://racket.discourse.group/u/Tyrn)\
**Post date:** [March 14, 2024, 7:05am UTC](https://racket.discourse.group/t/enumerated-stream/2790/1 "2024-03-14T07:05:06Z")

</div>

Hi,

Suppose I have something like this:

```scheme
(define reds (stream-cons "red" reds))

(stream-for-each displayln reds))

```

The output will be

```scheme
red
red
red
...

```

What's the Racket way to make it

```scheme
1 red
2 red
3 red
...

```

?

---

<div class="post-metadata">

**Author:** ![usao](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/usao/32/1375_2.png) [@usao](https://racket.discourse.group/u/usao)\
**Post date:** [March 14, 2024, 8:38am UTC](https://racket.discourse.group/t/enumerated-stream/2790/2 "2024-03-14T08:38:13Z")

</div>

Use `for` and `in-stream`:

```scheme
#lang racket/base
(require racket/stream)

(define reds (stream-cons "red" reds))

(for ([red (in-stream reds)]
      [idx (in-naturals 1)])
  (printf "~a ~a\n" idx red))

```

---

<div class="post-metadata">

**Author:** ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)\
**Post date:** [March 14, 2024, 1:06pm UTC](https://racket.discourse.group/t/enumerated-stream/2790/3 "2024-03-14T13:06:51Z")

</div>

With different output formatting, something like (guessing)

```scheme
(stream-map cons (in-naturals) reds)

```

Except that `stream-map` doesn’t have arities like `map` when I checked, sadly. Is there a way to write this with stream combinators?

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [March 14, 2024, 6:04pm UTC](https://racket.discourse.group/t/enumerated-stream/2790/4 "2024-03-14T18:04:57Z")

</div>

A few ways. Using `for/stream`:

```scheme
(define numbered-reds
  (for/stream ([i (in-naturals)]
               [r (in-stream reds)])
  (cons i r))

```

or with [SRFI-41](https://srfi.schemers.org/srfi-41/srfi-41.html), which has a `stream-map` that accepts multiple streams:

```scheme
(require srfi/41)

(define nats (stream-cons 0 (stream-map add1 nats)))
(define reds (stream-constant "red"))

(define numbered-reds (stream-map cons nats reds))

```

---

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [March 14, 2024, 11:38pm UTC](https://racket.discourse.group/t/enumerated-stream/2790/5 "2024-03-14T23:38:31Z")

</div>

Although somewhat redundant by now:

```scheme
(define numbered-reds
  (for/stream ([(r i) (in-indexed (in-stream reds))])
    (cons i r)))

```
