utils: Add compose-n, compose-3, and compose-4.
This commit is contained in:
parent
f9de7b1027
commit
a13b30d876
2 changed files with 34 additions and 1 deletions
|
@ -199,6 +199,26 @@ element with @racket[string->any] or keeps it as is if it is not a string.
|
||||||
(unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))")))
|
(unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))")))
|
||||||
]}
|
]}
|
||||||
|
|
||||||
|
@defproc[(compose-n [proc (-> a a)] ...) (-> a a)]{
|
||||||
|
|
||||||
|
Compose an arbitrary number of functions of type @racket[(-> a a)].
|
||||||
|
|
||||||
|
@examples[#:eval utils-evaluator
|
||||||
|
((compose-n add1 add1 add1) 3)
|
||||||
|
]}
|
||||||
|
|
||||||
|
@defproc*[([(compose-3 [proc1 (-> c d)] [proc2 (-> b c)] [proc3 (-> a b)]) (-> a d)]
|
||||||
|
[(compose-4 [proc1 (-> d e)] [proc2 (-> c d)] [proc3 (-> b c)] [proc4 (-> a b)]) (-> a e)])]{
|
||||||
|
|
||||||
|
@racket[compose-i] composes @racket[i] functions. The rightmost function is
|
||||||
|
applied first.
|
||||||
|
|
||||||
|
@examples[#:eval utils-evaluator
|
||||||
|
(define (s->n [x : String]) (cast (string->number x) Number))
|
||||||
|
(define fancy-add1 (compose-3 print add1 s->n))
|
||||||
|
fancy-add1
|
||||||
|
(fancy-add1 "1")
|
||||||
|
]}
|
||||||
@section{Additional graph utilities}
|
@section{Additional graph utilities}
|
||||||
|
|
||||||
@section{Pretty printing}
|
@section{Pretty printing}
|
||||||
|
|
15
utils.rkt
15
utils.rkt
|
@ -6,7 +6,7 @@
|
||||||
eval-with eval1-with
|
eval-with eval1-with
|
||||||
extract-symbols
|
extract-symbols
|
||||||
any->string stringify-variable-mapping string->any map-sexp
|
any->string stringify-variable-mapping string->any map-sexp
|
||||||
read-org-sexp unorg unstringify-pairs
|
read-org-sexp unorg unstringify-pairs compose-n compose-3 compose-4
|
||||||
;; Syntax
|
;; Syntax
|
||||||
auto-hash-ref/explicit auto-hash-ref/:)
|
auto-hash-ref/explicit auto-hash-ref/:)
|
||||||
|
|
||||||
|
@ -224,3 +224,16 @@
|
||||||
'((a . 1) (b . (and a (not b)))))
|
'((a . 1) (b . (and a (not b)))))
|
||||||
(check-equal? (unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))")))
|
(check-equal? (unstringify-pairs '(("a" . 1) ("b" . "(and a (not b))")))
|
||||||
'((a . 1) (b . (and a (not b)))))))
|
'((a . 1) (b . (and a (not b)))))))
|
||||||
|
|
||||||
|
(: compose-n (All (a) (-> (-> a a) * (-> a a))))
|
||||||
|
(define (compose-n . funcs)
|
||||||
|
(λ (x)
|
||||||
|
(for/foldr ([x x]) ([f funcs])
|
||||||
|
(f x))))
|
||||||
|
|
||||||
|
(: compose-3 (All (a b c d) (-> (-> c d) (-> b c) (-> a b) (-> a d))))
|
||||||
|
(define (compose-3 f1 f2 f3)
|
||||||
|
(λ (x) (f1 (f2 (f3 x)))))
|
||||||
|
(: compose-4 (All (a b c d e) (-> (-> d e) (-> c d) (-> b c) (-> a b) (-> a e))))
|
||||||
|
(define (compose-4 f1 f2 f3 f4)
|
||||||
|
(λ (x) (f1 (f2 (f3 (f4 x))))))
|
||||||
|
|
Loading…
Reference in a new issue