Duck util-proc.

This commit is contained in:
Dominik Pantůček 2023-07-05 22:09:07 +02:00
parent 2f1589579e
commit a2c312741b
5 changed files with 109 additions and 64 deletions

View file

@ -25,8 +25,12 @@
(declare (unit util-proc))
(module
(import duck)
(module*
util-proc
#:doc ("This module provides a few simple procedures for querying properties
of other procedures.")
(
improper-list-info
@ -41,9 +45,9 @@
(import scheme
(chicken base))
;; Returns two values: the proper part of the list length and #t if
;; there is an improper list end
(define (improper-list-info lst)
(define/doc (improper-list-info lst)
("Returns two values: the proper part of the list length and #t if
there is an improper list end")
(let loop ((lst lst)
(len 0))
(if (symbol? lst)
@ -59,31 +63,50 @@
(let-values (((len rest?) (improper-list-info (procedure-information proc))))
(values (sub1 len) rest?)))
;; Returns true if given procedure arity is exactly n
(define ((procedure-arity=? n) proc)
(define/doc ((procedure-arity=? n) proc)
("* ```n``` - integer representing the number of arguments
* ```proc``` - procedure to query
Returns true if the procedure ```proc``` accepts exactly ```n```
arguments.")
(let-values (((args rest?) (procedure-arity-info proc)))
(and (not rest?)
(= args n))))
;; Returns true if given procedure arity is greater than or equal to n
(define ((procedure-arity>=? n) proc)
(define/doc ((procedure-arity>=? n) proc)
("* ```n``` - integer representing the number of arguments
* ```proc``` - procedure to query
Returns true if the procedure ```proc``` accepts at least ```n```
arguments.")
(let-values (((args rest?) (procedure-arity-info proc)))
(or rest?
(>= args n))))
;; Returns true if given procedure arity is greater than n
(define ((procedure-arity>? n) proc)
(define/doc ((procedure-arity>? n) proc)
("* ```n``` - integer representing the number of arguments
* ```proc``` - procedure to query
Returns true if the procedure ```proc``` accepts more than ```n```
arguments.")
(let-values (((args rest?) (procedure-arity-info proc)))
(or rest?
(> args n))))
;; Returns the number of mandatory arguments
(define (procedure-num-args proc)
(define/doc (procedure-num-args proc)
("* ```proc``` - procedure to check
Returns the number of mandatory arguments.")
(let-values (((args rest?) (procedure-arity-info proc)))
args))
;; Returns the formal argument names for given procedure
(define (procedure-arg-names proc)
(define/doc (procedure-arg-names proc)
("* ```proc``` - procedure to check
Returns the (possibly improper) list of arguments the procedure
```proc``` accepts. If it accepts arbitrary number of arguments, it is
signalled by simple symbol instead of pair at the last position. If it
accepts an exact number of arguments, it returns a proper list.")
(cdr (procedure-information proc)))
)