Add multi-chain.

This commit is contained in:
Sergiu Ivanov 2020-12-20 20:45:29 +01:00
parent 00b01b924d
commit 26d7620dc5

View file

@ -25,7 +25,7 @@
(provide compose-n
compose-3 compose-4 compose-5 compose-6 compose-7 compose-8 compose-9
multi-compose)
multi-compose multi-chain)
(: compose-n (All (a) (-> (-> a a) * (-> a a))))
(define (compose-n . funcs)
@ -141,3 +141,21 @@
(λ ([x : Number]) (+ x 2)))
3)
19))
(define-syntax (multi-chain stx)
(syntax-parse stx
[(_ funcs:expr ...)
(define rev-funcs (reverse (syntax->list #'(funcs ...))))
#`(multi-compose #,@rev-funcs)]))
(module+ test
(test-case "multi-chain and multi-compose"
(define f1 (λ ([x : Number]) (displayln "f1") (+ x 1)))
(define f2 (λ ([x : Number]) (displayln "f2") (+ x 1)))
(define f3 (λ ([x : Number]) (displayln "f3") (+ x 1)))
(check-equal?
(with-output-to-string (λ () (displayln ((multi-chain f1 f2 f3) 3))))
"f1\nf2\nf3\n6\n")
(check-equal?
(with-output-to-string (λ () (displayln ((multi-compose f1 f2 f3) 3))))
"f3\nf2\nf1\n6\n")))