Generalize period-based definitions lookup.

This commit is contained in:
Dominik Pantůček 2023-03-31 20:17:01 +02:00
parent 4a62102bc2
commit 51b3b6f5b7
2 changed files with 28 additions and 20 deletions

View file

@ -39,6 +39,8 @@
month-in-periods?
periods->string
periods-match
make-period-lookup-table
lookup-by-period
period-tests!
)
@ -171,6 +173,30 @@
(car ps)
(loop (cdr ps)))))))
;; Creates lookup table from definition source
(define (make-period-lookup-table source)
(map
(lambda (src)
(let* ((since0 (car src))
(since (apply make-month since0))
(before0 (cadr src))
(before (if before0
(apply make-month before0)
#f))
(definitions (cddr src)))
(cons (make-period since before)
definitions)))
source))
;; Looks up current month and returns associated definitions
(define (lookup-by-period table)
(let loop ((lst table))
(if (null? lst)
#f
(if (month-in-period? (caar lst))
(cdar lst)
(loop (cdr lst))))))
;; Performs self-tests of the period module.
(define (period-tests!)
(run-tests