1. procedure-decoration
    1. Introduction
    2. Documentation
    3. Usage
    4. Procedure Decorator
      1. make-procedure-decorator
      2. procedure-decorator?
      3. decorate-procedure
      4. decorated-procedure?
      5. procedure-decoration
      6. procedure-decorator-getter-and-setter
    5. Procedure Extender
      1. make-procedure-extender
      2. define-procedure-extender
    6. Lambda Decoration Interface
      1. decorated-lambda?
      2. lambda-decoration
      3. decorate-lambda
    7. Examples
    8. Notes
    9. Author
    10. Requirements
    11. Version history
    12. License

procedure-decoration

Introduction

Chicken Scheme procedures have attributes, aspects of the procedure object that can be operated upon. Such an attribute is known as a "procedure decoration".

This extension provides a facility to perform procedure decoration operations.

Documentation

An interface for procedure decoration. Similar to the CHICKEN library extend-procedure interface but allowing many decorations, not just one.

A PREDICATE is a procedure of one argument, a procedure decoration. The predicate must return a truth value indicating whether the argument is a decoration of the proper kind.

A DECORATOR is a procedure of at least one argument, the current procedure decoration, which is (void) initially. The decorator must return an object to replace the current decoration.

Usage

(import procedure-decoration)

Procedure Decorator

A more sophisticated interface for procedure decoration.

make-procedure-decorator

[procedure] (make-procedure-decorator PREDICATE DECORATOR RETRIEVER [#:initializer INITIALIZER] [#:replace? REPLACE?]) -> procedure-decorator

Returns a new procedure-decorator.

The RETRIEVER is a procedure of at least one argument, the current procedure decoration, returning the procedure decoration object. Any following arguments are from the procedure-decoration call that will invoke the RETRIEVER.

The INITIALIZER is a procedure of varying arity, returning the procedure decoration initial object. The arguments are from the decorate-procedure call that invokes the INITIALIZER. The INITIALIZER is invoked only once per procedure to decorate.

The default INITIALIZER is built from the DECORATOR. The DECORATOR will be called with a first argument of (void), and any other arguments from the decorate-procedure call.

The REPLACE? flag determines whether the procedure object of a decorated procedure will be replaced (become). The default is #f.

procedure-decorator?

[procedure] (procedure-decorator? OBJECT) -> boolean

Is the OBJECT a procedure-decorator?

decorate-procedure

[procedure] (decorate-procedure PROCEDURE PROCEDURE-DECORATOR [ARGUMENTS ...]) -> procedure

Decorate the PROCEDURE with the PROCEDURE-DECORATOR.

The PROCEDURE-DECORATOR INITIALIZER is called on the first invocation for PROCEDURE and the DECORATOR on all subsequent invocations.

The optional ARGUMENTS are passed to the invoked PROCEDURE-DECORATOR constructor, as above.

On the first invocation a GC will be performed.

Returns the decorated procedure.

decorated-procedure?

[procedure] (decorated-procedure? PROCEDURE PROCEDURE-DECORATOR) -> boolean

Is the PROCEDURE a decorated by PROCEDURE-DECORATOR?

procedure-decoration

[procedure] (procedure-decoration PROCEDURE PROCEDURE-DECORATOR [ARGUMENTS ...]) -> *

Returns the procedure decoration of PROCEDURE-DECORATOR for the PROCEDURE.

The optional ARGUMENTS are passed to the PROCEDURE-DECORATOR RETRIEVER.

procedure-decorator-getter-and-setter

[procedure] (procedure-decorator-getter-and-setter PROCEDURE-DECORATOR) -> procedure

Returns a single argument getter, and creates a two argument setter, for the PROCEDURE-DECORATOR.

The getter takes a PROCEDURE argument. The setter takes PROCEDURE and OBJECT arguments.

Procedure Extender

Not to be confused with the extend-procedure interface.

make-procedure-extender

[procedure] (make-procedure-extender TAG) -> procedure-decorator

Returns a simple PROCEDURE-DECORATOR that recognizes its' decorations by the TAG and accepts any object as a decoration.

The TAG is any object suitable for testing with eq?.

define-procedure-extender

[syntax] (define-procedure-extender TAG [GETTER-NAME [PREDICATE-NAME]])

Creates a functional interface for a PROCEDURE-DECORATOR that uses the {{TAG} symbol.

Defines the PROCEDURE-DECORATOR as TAG-decorator.

Defines a procedure named GETTER-NAME that takes one argument, a decorated procedure, and returns the procedure decoration. Creates a corresponding setter. The default GETTER-NAME is TAG-decoration.

Defines a procedure named PREDICATE-NAME that takes one argument, a procedure, and returns whether the procedure is decorated. The default PREDICATE-NAME is TAG-decorated?.

Lambda Decoration Interface

A lower level interface for procedure decoration.

decorated-lambda?

[procedure] (decorated-lambda? PROCEDURE PREDICATE) -> boolean

Is the PROCEDURE decorated per the PREDICATE?

lambda-decoration

[procedure] (lambda-decoration PROCEDURE PREDICATE) -> *

Returns an object per the PREDICATE or #f.

decorate-lambda

[procedure] (decorate-lambda PROCEDURE PREDICATE DECORATOR)

Decorate the PROCEDURE per the PREDICATE with the result of the DECORATOR.

Returns the decorated procedure.

Examples

;create a 'docstring' concept
(define-procedure-extender docstring procedure-documentation documented-procedure?)

;document something
(define (foo x) x)
(documented-procedure? test-proc) ;=> #f
(set! (procedure-documentation foo) "foo is foo")
(documented-procedure? test-proc) ;=> #t
(procedure-documentation test-proc) ;=> "foo is foo"

Notes

Author

Kon Lovett

Requirements

check-errors

test test-utils

Version history

3.0.0
CHICKEN 5 release.

License

Copyright (c) 2009-2024 Kon Lovett. All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the Software), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.