Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
33
Task/Nested-templated-data/Racket/nested-templated-data.rkt
Normal file
33
Task/Nested-templated-data/Racket/nested-templated-data.rkt
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#lang racket
|
||||
|
||||
(define current-not-found-handler
|
||||
(make-parameter (λ (idx max) (raise-range-error 'substitute-template "integer?" "" idx 0 max))))
|
||||
|
||||
(define ((substitute-template template) payloads)
|
||||
(define match-function
|
||||
(match-lambda
|
||||
[(? nonnegative-integer? idx) #:when (< idx (length payloads)) (list-ref payloads idx)]
|
||||
[(? nonnegative-integer? idx) ((current-not-found-handler) idx (sub1 (length payloads)))]
|
||||
[(list (app match-function substitutions) ...) substitutions]))
|
||||
(match-function template))
|
||||
|
||||
(module+ test
|
||||
(require rackunit)
|
||||
|
||||
(define substitute-in-t (substitute-template '(((1 2)
|
||||
(3 4 1)
|
||||
5))))
|
||||
|
||||
(define p '(Payload#0 Payload#1 Payload#2 Payload#3 Payload#4 Payload#5 Payload#6))
|
||||
|
||||
(check-equal? (substitute-in-t p)
|
||||
'(((Payload#1 Payload#2)
|
||||
(Payload#3 Payload#4 Payload#1)
|
||||
Payload#5)))
|
||||
|
||||
(define out-of-bounds-generating-template-substitution (substitute-template '(7)))
|
||||
|
||||
(check-exn exn:fail:contract? (λ () (out-of-bounds-generating-template-substitution p)))
|
||||
|
||||
(parameterize ((current-not-found-handler (λ (idx max) (format "?~a" idx))))
|
||||
(check-equal? (out-of-bounds-generating-template-substitution p) '("?7"))))
|
||||
Loading…
Add table
Add a link
Reference in a new issue