@@ -33,6 +33,36 @@ no-change-test: "named let loop over counter not refactorable when loop function
3333------------------------------------------------------------
3434
3535
36+ test: "named let loop can be refactored to for/first with in-naturals "
37+ ------------------------------------------------------------
38+ (define (f c g)
39+ (let loop ([i 0 ])
40+ (cond
41+ [(c i)
42+ (loop (+ i 1 ))]
43+ [else
44+ (g i)])))
45+ ============================================================
46+ (define (f c g)
47+ (for/first ([i (in-naturals 0 )]
48+ #:unless (c i))
49+ (g i)))
50+ ------------------------------------------------------------
51+
52+
53+ no-change-test: "named let loop not refactorable to for/first with in-naturals when loop referenced "
54+ ------------------------------------------------------------
55+ (define (f c g)
56+ (let loop ([i 0 ])
57+ (cond
58+ [(c i)
59+ (loop (+ i 1 ))]
60+ [else
61+ (displayln loop)
62+ (g i)])))
63+ ------------------------------------------------------------
64+
65+
3666test: "named let loop with conditional return over vector can be replaced by for/first "
3767------------------------------------------------------------
3868(define vec (vector 0 1 2 3 4 5 ))
@@ -68,8 +98,7 @@ test: "named let loop over list can be replaced by for/list"
6898 (displayln (car xs))
6999 (cons (* (car xs) 10 )
70100 (loop (cdr xs)))]))
71- ------------------------------------------------------------
72- ------------------------------------------------------------
101+ ============================================================
73102(require racket/list)
74103(let loop ([xs (list 1 2 3 )])
75104 (cond
@@ -78,8 +107,7 @@ test: "named let loop over list can be replaced by for/list"
78107 (displayln (first xs))
79108 (cons (* (first xs) 10 )
80109 (loop (rest xs)))]))
81- ------------------------------------------------------------
82- ------------------------------------------------------------
110+ ============================================================
83111(require racket/list)
84112(for/list ([x (in-list (list 1 2 3 ))])
85113 (displayln x)
@@ -97,8 +125,7 @@ test: "named let loop can be replaced with for/and when equivalent"
97125 [(and (big? (first xs)) (not (red? (car xs))))
98126 (loop (rest xs))]
99127 [else #false ])))
100- ------------------------------------------------------------
101- ------------------------------------------------------------
128+ ============================================================
102129(require racket/list)
103130(define (f xs big? red?)
104131 (for/and ([x (in-list xs)])
@@ -115,8 +142,7 @@ test: "named let loop can be replaced with for/or when equivalent"
115142 [(empty? xs) #false ]
116143 [(and (big? (first xs)) (not (red? (car xs)))) #true ]
117144 [else (loop (rest xs))])))
118- ------------------------------------------------------------
119- ------------------------------------------------------------
145+ ============================================================
120146(require racket/list)
121147(define (f xs big? red?)
122148 (for/or ([x (in-list xs)])
@@ -125,14 +151,14 @@ test: "named let loop can be replaced with for/or when equivalent"
125151
126152
127153test: "read-until-eof loop refactorable to for loop with in-port "
128- --------------------
154+ ------------------------------------------------------------
129155(define (print-reads)
130156 (let loop ([v (read)])
131157 (unless (eof-object? v)
132158 (displayln v)
133159 (loop (read)))))
134- ====================
160+ ============================================================
135161(define (print-reads)
136162 (for ([v (in-port)])
137163 (displayln v)))
138- --------------------
164+ ------------------------------------------------------------
0 commit comments