Created
September 1, 2015 02:34
-
-
Save 97jaz/aefa9cb424dc8cd0b0c5 to your computer and use it in GitHub Desktop.
Limiting the size of the result of for*/list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 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