Skip to content

Instantly share code, notes, and snippets.

@dyoo
Created September 24, 2012 03:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyoo/3774030 to your computer and use it in GitHub Desktop.
Save dyoo/3774030 to your computer and use it in GitHub Desktop.
for/seq for sequence generating through for loops
#lang racket
(require racket/generator)
(define-syntax (for/seq stx)
(syntax-case stx ()
[(_ clauses . body)
#`(in-generator
(for/fold/derived #,stx
()
clauses
(define d (let () . body))
(yield d)
(values)))]))
(define seq1 (for/seq ([i (in-range 3)]
[j (in-list '(a b c))])
(displayln "here")
(list j (* i i))))
(define-syntax (for*/seq stx)
(syntax-case stx ()
[(_ clauses . body)
#`(in-generator
(for*/fold/derived #,stx
()
clauses
(define d (let () . body))
(yield d)
(values)))]))
(define seq2 (for*/seq ([i (in-range 3)]
[j (in-list '(a b c))])
(displayln "here")
(list j (* i i))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment