Skip to content

Instantly share code, notes, and snippets.

@mrfabbri
Created November 3, 2014 21:30
Show Gist options
  • Save mrfabbri/c075a7233a83fc0c41cb to your computer and use it in GitHub Desktop.
Save mrfabbri/c075a7233a83fc0c41cb to your computer and use it in GitHub Desktop.
Shadow stream in Racket
;; Returns a stream that shadows side-effects of the original stream
;; when accessed after the first time
(define (shadow-stream s)
(let ([shadow-next #f])
(thunk (begin
(when (not shadow-next)
(let ([next (s)])
(set! shadow-next (cons (car next) (shadow-stream (cdr next))))))
(cons (car shadow-next) (cdr shadow-next))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment