8.18
top
← prev up next →

Utilities for composing functions in Typed RacketπŸ”— i

Sergiu Ivanov <sivanov@colimite.fr>

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.

1LicenseπŸ”— i

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/.

2Functions for composing functionsπŸ”— i

procedure

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

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

Example:

- : Integer [more precisely: Positive-Integer]

6

procedure

( compose-3 proc1proc2proc3)(-> ad)

proc1:(-> cd)
proc2:(-> bc)
proc3:(-> ab)
( compose-4 proc1proc2proc3proc4)(-> ae)
proc1:(-> de)
proc2:(-> cd)
proc3:(-> bc)
proc4:(-> ab)
( compose-5 proc1proc2proc3proc4proc5)(-> af)
proc1:(-> ef)
proc2:(-> de)
proc3:(-> cd)
proc4:(-> bc)
proc5:(-> ab)
( compose-6 proc1proc2proc3proc4proc5proc6)(-> ag)
proc1:(-> fg)
proc2:(-> ef)
proc3:(-> de)
proc4:(-> cd)
proc5:(-> bc)
proc6:(-> ab)
( compose-7 proc1
proc2
proc3
proc4
proc5
proc6
proc7)(-> ah)
proc1:(-> gh)
proc2:(-> fg)
proc3:(-> ef)
proc4:(-> de)
proc5:(-> cd)
proc6:(-> bc)
proc7:(-> ab)
( compose-8 proc1
proc2
proc3
proc4
proc5
proc6
proc7
proc8)(-> ai)
proc1:(-> hi)
proc2:(-> gh)
proc3:(-> fg)
proc4:(-> ef)
proc5:(-> de)
proc6:(-> cd)
proc7:(-> bc)
proc8:(-> ab)
( compose-9 proc1
proc2
proc3
proc4
proc5
proc6
proc7
proc8
proc9)(-> aj)
proc1:(-> ij)
proc2:(-> hi)
proc3:(-> gh)
proc4:(-> fg)
proc5:(-> ef)
proc6:(-> de)
proc7:(-> cd)
proc8:(-> bc)
proc9:(-> ab)
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

3Macros for composing functionsπŸ”— i

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 f1f2f3f4) expands to (compose f1(compose f2(compose f3f4))).

Example:
(λ ([x: Number ])(* x3))
(λ ([x: Number ])(+ x2)))
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 f1f2f3f4) expands to (compose f4(compose f3(compose f2f1))).

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

f1

f2

f3

- : Number

6

> ((multi-compose f1f2f3)3)

f3

f2

f1

- : Number

6

top
← prev up next →

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /