Skip to content

Instantly share code, notes, and snippets.

@97jaz
Created September 1, 2015 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 97jaz/aefa9cb424dc8cd0b0c5 to your computer and use it in GitHub Desktop.
Save 97jaz/aefa9cb424dc8cd0b0c5 to your computer and use it in GitHub Desktop.
Limiting the size of the result of for*/list
;; RFC 5545 example: "Every other week on Tuesday and Thursday, for 8 occurrences"
(define start (moment 1997 9 2 9 #:tz "America/New_York"))
;; using for*/fold with #:break and explicit reverse
(let-values ([(res _)
(for*/fold ([xs '()] [i 0]) ([w (in-naturals)]
[t (in-value (+weeks start (* w 2)))]
[d (list (on-wday t 2) (on-wday t 4))]
#:break (= i 8))
(values (cons d xs) (add1 i)))])
(reverse res))
;; using in-stream
(for/list ([i 8]
[t (in-stream
(let loop ([w 0])
(define t (+weeks start (* w 2)))
(stream-cons (on-wday t 2)
(stream-cons (on-wday t 4) (loop (add1 w))))))])
t)
;; using non-existent for*/stream
(for/list ([i 8]
[t (for*/stream ([w (in-naturals)]
[t (in-value (+weeks start (* w 2)))]
[d (list (on-wday t 2) (on-wday t 4))])
d)])
t)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment