#lang scribble/doc @(require "mz.ss") @title[#:tag "procedures"]{Procedures} @defproc[(procedure? [v any/c]) boolean]{ Returns @scheme[#t] if @scheme[v] is a procedure, @scheme[#f] otherwise.} @defproc[(apply [proc procedure?] [v any/c] ... [lst list?] [#: kw-arg any/c] ...) any]{ @guideintro["apply"]{@scheme[apply]} Applies @scheme[proc] using the content of @scheme[(list* v ... lst)] as the (by-position) arguments. The @scheme[#: kw-arg] sequence is also supplied as keyword arguments to @scheme[proc], where @scheme[#:] stands for any keyword. The given @scheme[proc] must accept as many arguments as the number of @scheme[v]s plus length of @scheme[lst], it must accept the supplied keyword arguments, and it must not require any other keyword arguments; otherwise, the @exnraise[exn:fail:contract]. The given @scheme[proc] is called in tail position with respect to the @scheme[apply] call. @examples[ (apply + '(1 2 3)) (apply + 1 2 '(3)) (apply + '()) (apply sort (list (list '(2) '(1)) <) #:key car) ]} @defproc[(compose [proc procedure?] ...) procedure?]{ Returns a procedure that composes the given functions, applying the last @scheme[proc] first and the first @scheme[proc] last. The composed functions can consume and produce any number of values, as long as each function produces as many values as the preceding function consumes. @examples[ ((compose - sqrt) 10) ((compose sqrt -) 10) ((compose list split-path) (bytes->path #"/a" 'unix)) ]} @; ---------------------------------------- @section{Keywords and Arity} @defproc[(keyword-apply [proc procedure?] [kw-lst (listof keyword?)] [kw-val-lst list?] [v any/c] ... [lst list?] [#: kw-arg any/c] ...) any]{ @guideintro["apply"]{@scheme[keyword-apply]} Like @scheme[apply], but @scheme[kw-lst] and @scheme[kw-val-lst] supply by-keyword arguments in addition to the by-position arguments of the @scheme[v]s and @scheme[lst], and in addition to the directly supplied keyword arguments in the @scheme[#: kw-arg] sequence, where @scheme[#:] stands for any keyword. The given @scheme[kw-lst] must be sorted using @scheme[keyword], otherwise, the @exnraise[exn:fail:contract]. The given @scheme[kw-val-lst] must have the same length as @scheme[kw-lst], otherwise, the @exnraise[exn:fail:contract]. The given @scheme[proc] must accept all of the keywords in @scheme[kw-lst] plus the @scheme[#:]s, it must not require any other keywords, and it must accept as many by-position arguments as supplied via the @scheme[v]s and @scheme[lst]; otherwise, the @exnraise[exn:fail:contract]. @defexamples[ (define (f x #:y y #:z [z 10]) (list x y z)) (keyword-apply f '(#:y) '(2) '(1)) (keyword-apply f '(#:y #:z) '(2 3) '(1)) (keyword-apply f #:z 7 '(#:y) '(2) '(1)) ]} @defproc[(procedure-arity [proc procedure?]) procedure-arity?]{ Returns information about the number of by-position arguments accepted by @scheme[proc]. See also @scheme[procedure-arity?].} @defproc[(procedure-arity? [v any/c]) boolean?]{ A valid arity @scheme[_a] is one of the following: @itemize{ @item{An exact non-negative integer, which means that the procedure accepts @scheme[_a] arguments, only.} @item{A @scheme[arity-at-least] instance, which means that the procedure accepts @scheme[(arity-at-least-value _a)] or more arguments.} @item{A list containing integers and @scheme[arity-at-least] instances, which means that the procedure accepts any number of arguments that can match one of the elements of @scheme[_a].} } @examples[ (procedure-arity cons) (procedure-arity list) (arity-at-least? (procedure-arity list)) (arity-at-least-value (procedure-arity list)) (arity-at-least-value (procedure-arity (lambda (x . y) x))) (procedure-arity (case-lambda [(x) 0] [(x y) 1])) ]} @defproc[(procedure-arity-includes? [proc procedure?] [k exact-nonnegative-integer?]) boolean?]{ Returns @scheme[#t] if the procedure can accept @scheme[k] arguments when no keyword arguments are supplied, @scheme[#f] otherwise. @examples[ (procedure-arity-includes? cons 2) (procedure-arity-includes? display 3) ]} @defproc[(procedure-reduce-arity [proc procedure?] [arity procedure-arity?]) procedure?]{ Returns a procedure that is the same as @scheme[proc] (including the same name returned by @scheme[object-name]), but that accepts only arguments consistent with @scheme[arity]. In particular, when @scheme[procedure-arity] is applied to the generated procedure, it returns a value that is @scheme[equal?] to @scheme[arity]. If the @scheme[arity] specification allows arguments that are not in @scheme[(procedure-arity proc)], the @exnraise[exn:fail:contract]. @examples[ (define my+ (procedure-reduce-arity + 2)) (my+ 1 2) (my+ 1 2 3) ]} @defproc[(procedure-keywords [proc procedure?]) (values (listof keyword?) (or/c (listof keyword?) false/c))]{ Returns information about the keyword arguments required and accepted by a procedure. The first result is a list of keywords (sorted by @scheme[keyword* . any)] [plain-proc procedure? (lambda args (keyword-apply proc null null args))]) procedure?]{ Returns a procedure that accepts all keyword arguments (without requiring any keyword arguments). See also @scheme[procedure-reduce-keyword-arity]. When the result is called with keyword arguments, then @scheme[proc] is called; the first argument is a list of keywords sorted by @scheme[keyword