Skip to content

Commit cb78d55

Browse files
fix: optimize sliding
1 parent 0f71d9f commit cb78d55

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

main.rkt

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@
102102

103103

104104
(define (sliding lst size [step 1])
105-
(define (tail-call lst)
106-
(if (>= size (length lst))
107-
(list lst)
108-
(cons (take lst size)
109-
(tail-call (drop lst step)))))
110-
(cond
111-
[(> step (length lst))
112-
(error "step has to be equal to or smaller than length of the list")]
113-
[(= step (length lst)) (list lst)]
114-
[else (tail-call lst)]))
105+
(cond [(> step (length lst))
106+
(error "step has to be equal to or smaller than length of the list")]
107+
[(= step (length lst))
108+
(list lst)]
109+
[(let recur ([lst lst]
110+
[len (length lst)])
111+
(if (>= size len)
112+
(if (empty? lst) empty (list lst))
113+
(cons (take lst size)
114+
(recur (drop lst step)
115+
(- len step)))))]))
115116

116117

117118
(define (scanl proc lst)

0 commit comments

Comments
 (0)