Skip to content

Commit 51b1e63

Browse files
authored
Merge pull request #31 from walking-octopus/master
Optimize `sliding`
2 parents 198512b + bb63a77 commit 51b1e63

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

main.rkt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,16 @@
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+
(when (or (<= step 0) (> step (length lst)))
106+
(error "step has to be equal to or smaller than the length of the list"))
107+
108+
(let recur [(lst lst)
109+
(len (length lst))]
110+
(if (>= len size)
111+
(cons (take lst size)
112+
(recur (drop lst step)
113+
(- len step)))
114+
empty)))
115115

116116

117117
(define (scanl proc lst)

0 commit comments

Comments
 (0)