# Progress indicator for \`for\` loops: \`in-heartbeat\`

**URL:** <https://racket.discourse.group/t/progress-indicator-for-for-loops-in-heartbeat/960>\
**Category:** Show & Tell\
**Tags:** tips\
**Created:** [May 3, 2022, 7:43am UTC](https://racket.discourse.group/t/progress-indicator-for-for-loops-in-heartbeat/960 "2022-05-03T07:43:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [May 3, 2022, 7:43am UTC](https://racket.discourse.group/t/progress-indicator-for-for-loops-in-heartbeat/960/1 "2022-05-03T07:43:02Z")

</div>

Waiting for the result of a large computation can be annoying.  
Is the loop stuck?

The silly sequence below makes it easy to do something every `nth` time in a loop.  
Such as displaying a progress indicator.

```scheme
#lang racket

(define (in-heartbeat n beat)
  (make-do-sequence
   (lambda ()
     (values (λ (pos) (when (= pos n) (beat)) #f) ; pos->element
             (λ (pos) (if (= pos n) 0 (+ pos 1))) ; next-pos
             0 ; initial-pos
             (λ (pos) #t) ; continue?
             #f
             #f))))

(for/sum ([i 100]
          [x (in-heartbeat 10 (λ () (display ".")))])
  i)

; .........4950

```

---

<div class="post-metadata">

**Author:** ![dstorrs](https://avatars.discourse-cdn.com/v4/letter/d/898d66/32.png) [@dstorrs](https://racket.discourse.group/u/dstorrs)\
**Post date:** [May 3, 2022, 5:53pm UTC](https://racket.discourse.group/t/progress-indicator-for-for-loops-in-heartbeat/960/2 "2022-05-03T17:53:44Z")

</div>

This is excellent. I've been doing something similar except not as neatly. Thanks for sharing.

Sidethought: This would be worth putting in a module if you wanted to.

---

<div class="post-metadata">

**Author:** ![not-just-yeti](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/not-just-yeti/32/139_2.png) [@not-just-yeti](https://racket.discourse.group/u/not-just-yeti)\
**Post date:** [May 5, 2022, 1:16pm UTC](https://racket.discourse.group/t/progress-indicator-for-for-loops-in-heartbeat/960/3 "2022-05-05T13:16:45Z")

</div>

The only thing I'd change is make `heartbeat` one word -- I'll be forever mis-naming it otherwise : -)

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [May 5, 2022, 1:51pm UTC](https://racket.discourse.group/t/progress-indicator-for-for-loops-in-heartbeat/960/4 "2022-05-05T13:51:43Z")

</div>

Done!

Need 20 characters.
