Utilities for composing functions in Typed Racket
1 License
2 Functions for composing functions
compose-n
compose-3
compose-4
compose-5
compose-6
compose-7
compose-8
compose-9
3 Macros for composing functions
make-compose
multi-compose
multi-chain
8.12

Utilities for composing functions in Typed Racket🔗ℹ

Sergiu Ivanov <sivanov@colimite.fr>

 (require typed-compose) package: typed-compose

Typed Racket’s compose only takes two arguments, because in general it is difficult to specify that the return types and the argument types should be the same for two successive functions in the argument list. This package defines some further utilities to allow compose-ing more than two functions more comfortable in Typed Racket.

1 License🔗ℹ

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see https://www.gnu.org/licenses/.

2 Functions for composing functions🔗ℹ

procedure

(compose-n proc ...)  (-> a a)

  proc : (-> a a)
Compose an arbitrary number of functions of type (-> a a).

Example:
> ((compose-n add1 add1 add1) 3)

- : Integer [more precisely: Positive-Integer]

6

procedure

(compose-3 proc1 proc2 proc3)  (-> a d)

  proc1 : (-> c d)
  proc2 : (-> b c)
  proc3 : (-> a b)
(compose-4 proc1 proc2 proc3 proc4)  (-> a e)
  proc1 : (-> d e)
  proc2 : (-> c d)
  proc3 : (-> b c)
  proc4 : (-> a b)
(compose-5 proc1 proc2 proc3 proc4 proc5)  (-> a f)
  proc1 : (-> e f)
  proc2 : (-> d e)
  proc3 : (-> c d)
  proc4 : (-> b c)
  proc5 : (-> a b)
(compose-6 proc1 proc2 proc3 proc4 proc5 proc6)  (-> a g)
  proc1 : (-> f g)
  proc2 : (-> e f)
  proc3 : (-> d e)
  proc4 : (-> c d)
  proc5 : (-> b c)
  proc6 : (-> a b)
(compose-7 proc1    
  proc2    
  proc3    
  proc4    
  proc5    
  proc6    
  proc7)  (-> a h)
  proc1 : (-> g h)
  proc2 : (-> f g)
  proc3 : (-> e f)
  proc4 : (-> d e)
  proc5 : (-> c d)
  proc6 : (-> b c)
  proc7 : (-> a b)
(compose-8 proc1    
  proc2    
  proc3    
  proc4    
  proc5    
  proc6    
  proc7    
  proc8)  (-> a i)
  proc1 : (-> h i)
  proc2 : (-> g h)
  proc3 : (-> f g)
  proc4 : (-> e f)
  proc5 : (-> d e)
  proc6 : (-> c d)
  proc7 : (-> b c)
  proc8 : (-> a b)
(compose-9 proc1    
  proc2    
  proc3    
  proc4    
  proc5    
  proc6    
  proc7    
  proc8    
  proc9)  (-> a j)
  proc1 : (-> i j)
  proc2 : (-> h i)
  proc3 : (-> g h)
  proc4 : (-> f g)
  proc5 : (-> e f)
  proc6 : (-> d e)
  proc7 : (-> c d)
  proc8 : (-> b c)
  proc9 : (-> a b)
compose-i composes i functions. The rightmost function is applied first.

Examples:
> (define (s->n [x : String]) (cast (string->number x) Number))
> (define fancy-add1 (compose-3 print add1 s->n))
> fancy-add1

- : (-> String Void)

#<procedure:...ed-compose/main.rkt:61:25>

> (fancy-add1 "1")

2

3 Macros for composing functions🔗ℹ

syntax

(make-compose n)

 
  n : exact-nonnegative-integer
Expants to a typed lambda form composing exactly n one-argument functions. For example, compose-3 is defined as: (define compose-3 (make-compose 3)). The rest of the functions of the compose-i family are defined using this macro as well.

syntax

(multi-compose func ...)

 
  func : expression
Expands to a code applying compose in a pairwise manner to the given expressions. For example, (multi-compose f1 f2 f3 f4) expands to (compose f1 (compose f2 (compose f3 f4))).

Example:
> ((multi-compose add1
                  (λ ([x : Number]) (* x 3))
                  add1
                  (λ ([x : Number]) (+ x 2)))
          3)

- : Number

19

syntax

(multi-chain func ...)

 
  func : expression
Like multi-compose, but the first function in the argument list is applied first instead of last. For example, (multi-chain f1 f2 f3 f4) expands to (compose f4 (compose f3 (compose f2 f1))).

Examples:
> (define f1 (λ ([x : Number]) (displayln "f1") (+ x 1)))
> (define f2 (λ ([x : Number]) (displayln "f2") (+ x 1)))
> (define f3 (λ ([x : Number]) (displayln "f3") (+ x 1)))
> ((multi-chain f1 f2 f3) 3)

f1

f2

f3

- : Number

6

> ((multi-compose f1 f2 f3) 3)

f3

f2

f1

- : Number

6