hackerbase/src/util-proc.scm

87 lines
2.5 KiB
Scheme

;;
;; util-proc.scm
;;
;; Auxiliary procedure functions.
;;
;; ISC License
;;
;; Copyright 2023 Brmlab, z.s.
;; Dominik Pantůček <dominik.pantucek@trustica.cz>
;;
;; Permission to use, copy, modify, and/or distribute this software
;; for any purpose with or without fee is hereby granted, provided
;; that the above copyright notice and this permission notice appear
;; in all copies.
;;
;; THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
;; WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
;; WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
;; AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
;; CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
;; OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
;; NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
;; CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
;;
(declare (unit util-proc))
(module
util-proc
(
procedure-arity=?
procedure-arity>=?
procedure-arity>?
procedure-num-args
procedure-arg-names
)
(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)
(let loop ((lst lst)
(len 0))
(if (symbol? lst)
(values len #t)
(if (null? lst)
(values len #f)
(loop (cdr lst)
(add1 len))))))
;; Returns two values: the number of mandatory arguments and
;; information whether the procedure accepts optional arguments
(define (procedure-arity-info proc)
(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)
(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)
(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)
(let-values (((args rest?) (procedure-arity-info proc)))
(or rest?
(> args n))))
;; Returns the number of mandatory arguments
(define (procedure-num-args proc)
(let-values (((args rest?) (procedure-arity-info proc)))
args))
;; Returns the formal argument names for given procedure
(define (procedure-arg-names proc)
(cdr (procedure-information proc)))
)