You are looking at historical revision 19515 of this page. It may differ significantly from its current revision.

moremacros

Documentation

Various useful forms.

moremacros

type-case

[syntax] (type-case EXPRESSION [(TYPE-CASE BODY ...) ...]))

Expands into a form that selects a TYPE-CASE based on the type of the EXPRESSION.

A TYPE-CASE is:

symbol
the base name of a type predicate
(symbol symbol...)
a list of base names
else
an else clause

The actual name of the predicate is built from the base name and a ? suffix. So a base name number has the predicate number?.

(use moremacros)

(type-case 23
  ((symbol string char) 'symbolic)
  (number               'numeric)
  (else                 'otheric) )
;=> numeric

type-case*

[syntax] (type-case* EXPRESSION [(TYPE-TEST BODY ...) ...]))

Like type-case but binds local variable it to the value of EXPRESSION.

(use moremacros)

(type-case* 23
  ((symbol string char) (list it 'symbolic) )
  (number               (list it 'numeric) )
  (else                 (list it 'otheric) ) )
;=> (23 numeric)

whennot

[syntax] (whennot TEST [BODY ...]))

Synonym for miscmacros#unless.

swap-set!

[syntax] (swap-set! VAR1 VAR2))

Swap settings of VAR1 & VAR2.

Like (exchange! VAR1 VAR2) but lower overhead.

fluid-set!

[syntax] (fluid-set! VAR VAL ...))

Set each variable VAR to the value VAL in parallel.

stiff-set!

[syntax] (stiff-set! VAR VAL ...))

Set each variable VAR to the value VAL in series.

set!/op

[syntax] (set!/op VAR OP ARG...))

Sets VAR to the value of (OP ARG...), where an occurrence of <> in ARG... is replaced with VAR.

When there is no occurrence of <> in ARG... the template (OP <> ARG...) is used.

Similar to the C language family <l-value> <bin-op-assign> <r-value>.

assure

[syntax] (assure EXPRESSION [ERROR-ARGUMENT...]))

When EXPRESSION yields value #f} invoke {{(error ERROR-ARGUMENT...), otherwise return value.

make-reference-let

[syntax] (make-reference-let NAME REFERENCE-FUNCTION)

NAME is a symbol, the name of generated reference-let macro.

REFERENCE-FUNCTION is a (procedure (* * *) *) with arguments:

TABLE
some data-structure instance that reifies a set of key+value abstraction
KEY
identifier for a possible entry in the TABLE
DEFAULT
in case an entry for the KEY does not exist

The REFERENCE-FUNCTION is to return the value for KEY in the TABLE, otherwise the DEFAULT value.

The generated macro has the signature:

[syntax] (NAME ((((VAR | (VAR) | (VAR KEY [DEFAULT]))...) TABLE)...) BODY...))

Decompose TABLE entries into variable bindings. Should the KEY not be a symbol, or the desired variable name VAR, as 'VAR, is not the key, the (VAR KEY [DEFAULT]) form can be used.

The default for DEFAULT is #f.

The BODY... is evaluated with the specified bindings.

See hash-let for an example of use.

variable-item

Usage

(require-extension variable-item)

make-variable

[procedure] (make-variable INIT [GUARD [TYPE-NAME]]) => (procedure * *)

Returns a procedure whose behavior is that of make-parameter, except for the fact that it is not a parameter.

GUARD is a (procedure (*) boolean) testing the acceptability of the variable value. Default is no checking.

TYPE-NAME is a symbol or string describing the type tested by GUARD. Default is generic.

When the GUARD fails a warning is produced.

define-variable

[syntax] (define-variable NAME INIT [GUARD [TYPE-NAME]])

Assigns a make-variable to the variable NAME.

NAME is a symbol.

GUARD and TYPE-NAME are as for make-variable

(use moremacros)

(define-variable scale * procedure? 'procedure)
(scale)
; => #<procedure ...>

(scale 23)
 ;=> ;; Warning: "bad argument type - not a procedure" : 23

(scale /)

(scale)
; => #<procedure ...>

hash-let

Usage

(require-extension hash-let)

hash-let

[syntax] (hash-let ((((VAR | (VAR) | (VAR KEY [DEFAULT]))...) HASH-TABLE)...) BODY...))

Decompose HASH-TABLE entries into variable bindings. Should the KEY not be a symbol, or the desired variable name VAR is not the key, the (VAR KEY [DEFAULT]) form can be used.

The default value for a missing hash-table entry is #f but can be specified with the (VAR KEY DEFAULT) form.

The BODY... is evaluated with the specified bindings.

(use hash-let srfi-69)

(define tbl (make-hash-table))

(hash-table-set! tbl 'abc "commercial network")
(hash-table-set! tbl "abc" "commercial network")
(hash-table-set! tbl 'cbs "commercial network")
(hash-table-set! tbl "cbs" "commercial network")

(hash-let ((((abc) (cbs "cbs")) tbl )
           (((pbs (string-append "p" "bs") #t) tbs) tbl ) )
  (print 'abc " is a " abc) (print "cbs" " is a " cbs)
  (print (string-append "p" "bs") " is a " pbs)
  (print 'tbs " is a " tbs) )

This prints the following:

abc is a commercial network
cbs is a commercial network
pbs is a #f
tbs is a ?

hash-let is actually (generic-reference-let hash-let hash-table-ref/default).

Numeric Macros

Usage

(require-extension numeric-macros)

++

[syntax] (++ VAL)

Read-only increment.

When VAL is a numeric literal the strongest operation available is used in the generated expression, based on the type of the literal.

--

[syntax] (-- VAL)

Read-only decrement.

When VAL is a numeric literal the strongest operation available is used in the generated expression, based on the type of the literal.

fx++

[syntax] (fx++ VAL)

Read-only fixnum increment.

fx--

[syntax] (fx-- VAL)

Read-only fixnum decrement.

fp++

[syntax] (fp++ VAL)

Read-only flonum increment.

fp--

[syntax] (fp-- VAL)

Read-only flonum decrement.

fl++

[syntax] (fl++ VAL)

Read-only flonum increment.

R6RS nomenclature.

fl--

[syntax] (fl-- VAL)

Read-only flonum decrement.

R6RS nomenclature.

++!

[syntax] (++! VAR)

Mutable increment.

--!

[syntax] (--! VAR)

Mutable decrement.

fx++!

[syntax] (fx++! VAR)

Mutable fixnum increment.

fx--!

[syntax] (fx--! VAR)

Mutable fixnum decrement.

fp++!

[syntax] (fp++! VAR)

Mutable flonum increment.

fp--!

[syntax] (fp--! VAR)

Mutable flonum decrement.

fl++!

[syntax] (fl++! VAR)

Mutable flonum increment.

R6RS nomenclature.

fl--!

[syntax] (fl--! VAR)

Mutable flonum decrement.

R6RS nomenclature.

Author

kon lovett

Version history

1.1.0
Added make-reference-let. Split make-variable & define-variable into own module variable-item.
1.0.0
Hello

License

Copyright (C) 2010 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.