Skip to content

perf: optimize scanl O(n²) --> O(n) #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions main.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,21 @@
[else (tail-call lst)]))


(define (scanl proc lst)
(foldl
(λ (val acc)
(append acc (list (proc val (last acc)))))
(list (first lst))
(rest lst)))

(define scanl
(case-lambda
[(proc lst)
(scanl proc (car lst) (cdr lst))]
[(proc seed lst)
(let loop ([acc (list seed)]
[val seed]
[rest lst])
(if (empty? rest)
(reverse acc)
(let ([next (proc val (car rest))])
(loop (cons next acc)
next
(cdr rest)))))]))


(define (scanr proc lst)
(foldl
Expand Down
Loading