[plt-scheme] From hash-table to generator

From: Danny Yoo (dyoo at cs.wpi.edu)
Date: Mon May 7 17:29:49 EDT 2007

> The problem is thus to write hash-table->generator efficiently. As it 
> turns out, the inversion via call/cc looses big time to the 
> convert-to-list approach.
>
> So the next question, is: Is there a better way to turn a hash-table 
> into a generator?


Hi Jens,

I'm interested: how does the following approach compare, performance-wise, 
to the version with continuations?

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(module hash-table-generator mzscheme
   (require (lib "etc.ss")
            (lib "contract.ss"))

   (provide/contract [hash-table->generator
                      (hash-table? . -> . (-> any/c))])
   (define (hash-table->generator ht)
     (local
         ((define ch (make-channel))
          (define sentinel (cons 'done 'done))
          (define all-done? #f))
       (thread (lambda ()
                 (hash-table-for-each
                  ht
                  (lambda (k v)
                    (channel-put ch k)))
                 (channel-put ch sentinel)))
       (lambda ()
         (cond
           [all-done? #f]
           [else
            (let ([result (channel-get ch)])
              (cond
                [(eq? result sentinel)
                 (set! all-done? #t)
                 #f]
                [else
                 result]))])))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Posted on the users mailing list.