lazy-seq leak? pasted by Kooda on Fri Feb 20 15:51:13 2015

; Start this script with `csi -:D` to observe heap resizing

(use lazy-seq)

; Remove this when in lazy-seq {{{

(define (lazy-reductions kons knil . streams)
  (letrec ((s (lazy-seq
                (cons knil
                      (apply lazy-map kons s streams)))))
    s))

; (define (lazy-reductions kons init seq)
;     (if (lazy-null? seq)
;             lazy-null
;                   (lazy-seq
;                             (let ((next (kons (lazy-head seq) init)))
;                                         (cons next (lazy-reductions kons next (lazy-tail seq)))))))

; }}}

(define (time-stream)
  (define last (current-milliseconds))
  (lazy-seq (cons 0
                  (lazy-repeatedly
                    (lambda ()
                      (let* ((now (current-milliseconds))
                             (dt (- now last)))
                        (set! last now)
                        dt))))))

(define (make-clock time)
  (lazy-reductions + 0 time))

(define (render coords)
  (void coords))

(define (complex-stream time)
  (lazy-map (cut * 2 <>) (make-clock time)))


; This seems to leak:

(lazy-each
  render
  (complex-stream
      (time-stream)))


; This doesn't:

#;(lazy-each
  render
  (lazy-map
    (cut * 2 <>)
    (make-clock
      (time-stream))))

even simpler test pasted by Kooda on Fri Feb 20 16:08:37 2015

; Start this script with `csi -:D` to observe heap resizing

(use lazy-seq)

(define (complex-stream time)
  (lazy-map identity time))


; This seems to leak:

(lazy-each
  void
  (complex-stream
      (lazy-numbers)))


; This doesn't:

#;(lazy-each
  void
  (lazy-map
    identity
    (lazy-numbers)))

Larger program showing the behaviour, either interpreted or compiled added by Kooda on Mon Feb 23 16:34:08 2015

(use
  lazy-seq)

; from doodle

(define doodle-width 800)
(define doodle-height 480)


; from fps

(define (translate x y pict)
  pict)

(define (pcompose . rest)
  rest)

(define (pt x y) (cons x y))
(define (add-pts x y) (cons (+ (car x) (car y))
                            (+ (cdr x) (cdr y))))
(define pi (* 2 (asin 1)))
(define 2pi (* 2 pi))


; from frp.scm

(define (translate-pt p pict)
  (translate (car p) (cdr p) pict))

(define (time-stream) (constantly 0.01))
(define (event-stream) lazy-null)

; Remove this when included in lazy-seq {{{
(define (lazy-reductions kons knil . streams)
  (letrec ((s (lazy-seq
                (cons knil
                      (apply lazy-map kons s streams)))))
    s))
; }}}

(define (make-clock time)
  (lazy-reductions + 0 time))

(define (constantly x)
  (letrec ((s (lazy-seq (cons x s))))
    s))


; from test.scm

(define (stranslate pts picture)
  (lazy-map translate-pt pts picture))

(define (scompose . seqs)
  (apply lazy-map pcompose seqs))

(define (red-square)
  (constantly (list 'square)))

(define (delayed n seq)
  (lazy-append (lazy-take n (constantly (lazy-head seq)))
               seq))

(define (make-squares n pos)
  (let loop ((p 0))
    (if (= p n)
      '()
      (cons (stranslate (delayed p pos) (red-square))
            (loop (add1 p))))))

(define (oscillator f amp freq clock)
  (lazy-map
    (lambda (t)
      (* amp (f (* t freq 2pi))))
    clock))


(define (scene time events)
  (print "building scene")
  (let* ((clock (make-clock time))
         (x-pos (oscillator sin 345 (/ 1 6) clock))
         (y-pos (oscillator sin 185 (/ 1 3) clock))
         (pos (lazy-map add-pts center (lazy-map pt x-pos y-pos)))
         (the-scene (apply scompose (make-squares 100 pos))))
    (print "done building scene")
    the-scene))


; Rendering

(define center
  (constantly (pt (/ doodle-width 2) (/ doodle-height 2))))

(define (render pict)
  (void pict)
  )

(lazy-each render
           (scene (time-stream) (event-stream)))