> On Oct 21, 2018, at 3:49 PM, Jay McCarthy <jay.mccar...@gmail.com> wrote:
> 
> I think the best strategy would be to convert the generator to a
> sequence and then the sequence to a stream, then write a stream
> cartesian product. The stream will cache the results of the generator.



Nice.



#lang racket 
(require racket/generator rackunit) 

(define (make-cartesian-generator gens)
  (generator ()
             (define gstreams (for/list ([gen (in-list gens)])
                                          (for/stream ([sol (in-producer gen 
(void))]) 
                                                      sol))) 
             (let loop ([gstreams gstreams][acc empty])
               (if (null? gstreams)
                   (yield (reverse acc))
                   (for ([arg (in-stream (car gstreams))])
                        (loop (cdr gstreams) (cons arg acc)))))))

(define g1 (generator () (yield 1) (yield 2) (void))) 
(define g2 (generator () (yield 'a) (println 'only-prints-once) (yield 'b) 
(void)))

(check-equal? 
 (for/list ([gs (in-producer (make-cartesian-generator (list g1 g2)) (void))]) 
           gs) 
 '((1 a) (1 b) (2 a) (2 b)))

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to