1
0
Fork 0

Add for/max and for*/max stolen from Racket docs to library

This commit is contained in:
Jonathan Chan 2022-12-08 11:11:35 -05:00
parent 340a7a2b2e
commit 594e7061a4
2 changed files with 33 additions and 5 deletions

31
lib.rkt
View File

@ -7,12 +7,41 @@
enqueue!)
(only-in racket/set
list->set
set->list))
set->list)
(for-syntax syntax/for-body)
syntax/parse/define)
(provide (all-from-out threading)
(all-defined-out))
;; Macros ;;
(define-syntax-parse-rule (for/max clauses body ... tail-expr)
#:with original this-syntax
#:with ((pre-body ...) (post-body ...)) (split-for-body this-syntax #'(body ... tail-expr))
(for/fold/derived original
([current-max -inf.0])
clauses
pre-body ...
(define maybe-new-max (begin post-body ...))
(if (> maybe-new-max current-max)
maybe-new-max
current-max)))
(define-syntax-parse-rule (for*/max clauses body ... tail-expr)
#:with original this-syntax
#:with ((pre-body ...) (post-body ...)) (split-for-body this-syntax #'(body ... tail-expr))
(for*/fold/derived original
([current-max -inf.0])
clauses
pre-body ...
(define maybe-new-max (begin post-body ...))
(if (> maybe-new-max current-max)
maybe-new-max
current-max)))
;; Function helpers ;;
(define compose)

View File

@ -62,9 +62,8 @@
(- r r*))))
(define part2
(maximum
(for*/list ([r (in-range 1 (sub1 height))]
[c (in-range 1 (sub1 width))])
(score r c))))
(for*/max ([r (in-range 1 (sub1 height))]
[c (in-range 1 (sub1 width))])
(score r c)))
(show-solution (part1) part2)