Scheme Utilities in (planet cce/scheme:1:0)
This is my personal library of PLT Scheme programming utilities. Feel free to use it, copy my code, or ask me questions.
1 Main Module
This module provides all the bindings described below under the individual modules.
2 Classes and Objects
This module provides tools for classes, objects, and mixins.
class-or-interface/c : flat-contract? |
Recognizes classes and interfaces.
(object/c spec ) → flat-contract? |
spec : class-or-interface/c |
Recognizes objects which are instances of all the given classes and interfaces.
(class/c spec ) → flat-contract? |
spec : class-or-interface/c |
Recognizes classes which are subclasses (not strictly) and implementations, respectively, of all the given classes and interfaces.
Function contract for a mixin whose first argument is the parent class c% matching (class/c super-expr ), whose remaining arguments match arg-expr , and whose result matches (class/c c% sub-expr ).
i<%> : interface? |
mx : (mixin/c [] [] i<%>) |
c% : class? |
Returns c% if it implements i<%>; otherwise, returns (mx c%).
Sends each message (with arguments) to obj, then returns obj.
Examples: | |||||
| |||||
> (send+ (new c%) [say 'Hello] [say 'Good-bye]) | |||||
| |||||
#(struct:object:c%) |
Sends the message to each object in the list objs, returning (void).
Examples: | ||||||
| ||||||
| ||||||
|
3 Functions
This module provides tools for higher-order programming and creating functions.
(identity v) → (one-of/c v) |
v : any/c |
This function returns its argument.
(constant v) → (unconstrained-> (one-of/c v)) |
v : any/c |
Produces a function that returns v regardless of input.
Examples: | ||
| ||
> (f) | ||
g242 | ||
> (f '(4 5 6)) | ||
g242 | ||
> (f #:x 7 #:y 8 #:z 9) | ||
g242 |
Creates a function that ignores its inputs and evaluates the given body. Useful for creating event handlers with no (or irrelevant) arguments.
Examples: | ||
| ||
> (f) | ||
1 | ||
> (f 'x) | ||
1 | ||
> (f #:y 'z) | ||
1 |
arg : A |
Calls its first argument, passing on any remaining arguments. Useful for invoking thunks in a higher-order context (for instance, as an argument to map or for-each).
Examples: | |||
| |||
> (call say) | |||
Hello, World! | |||
> (call say "Good night" #:to "Moon") | |||
Good night, Moon! |
((conjoin f ) arg ) → boolean? |
arg : A |
Combines calls to each function with and.
Examples: | ||
| ||
> (f 1) | ||
#t | ||
> (f 1.0) | ||
#f | ||
> (f 1/2) | ||
#f | ||
> (f 0.5) | ||
#f |
((disjoin f ) arg ) → boolean? |
arg : A |
Combines calls to each function with or.
Examples: | ||
| ||
> (f 1) | ||
#t | ||
> (f 1.0) | ||
#t | ||
> (f 1/2) | ||
#t | ||
> (f 0.5) | ||
#f |
| ||||||||||||||||||||||||||||||
|
Constructs a function much like lambda, except that some optional arguments correspond to the value of a parameter. For each clause [id #:param param-expr] in which param-expr evalutes to a value param satisfying parameter?, the default value of id is (param); conversely, when id is provided, param is bound to id via parameterize during the function call.
Examples: | |||||
| |||||
| |||||
> (hello-world p) | |||||
> (get-output-string p) | |||||
"Hello, World!\n" |
4 Syntax Objects
This module provides tools for macro transformers.
(syntax-datum/c datum/c) → flat-contract? |
datum/c : flat-contract/predicate? |
Recognizes syntax objects stx such that (syntax->datum stx) satisfies datum/c.
(syntax-listof/c elem/c) → flat-contract? |
elem/c : flat-contract/predicate? |
Recognizes syntax objects stx such that (syntax->list stx) satisfies (listof elem/c).
(syntax-list/c elem/c ) → flat-contract? |
elem/c : flat-contract/predicate? |
Recognizes syntax objects stx such that (syntax->list stx) satisfies (list/c elem/c ...).
(syntax-map f stx) → (listof A) |
stx : syntax? |
Performs (map f (syntax->list stx)).
Examples: |
> (syntax-map syntax-e #'(a (b c) d)) |
(a (#<syntax:1:0> #<syntax:1:0>) d) |
| ||||||||||||||||||||||||||||||||||||||||||
datum : any/c | ||||||||||||||||||||||||||||||||||||||||||
A wrapper for datum->syntax with keyword arguments for the syntax properties. Users may leave them all off for unadorned syntax objects, or use the "master" keyword #:stx to set all properties from one syntax object, or use the other keywords for individual kinds of properties, or some combination thereof.
Examples: | ||
| ||
> blank-stx | ||
#<syntax> | ||
> (syntax-e blank-stx) | ||
car | ||
> (free-identifier=? blank-stx #'car) | ||
#f | ||
| ||
> full-stx | ||
#<syntax:13:0> | ||
> (syntax-e full-stx) | ||
car | ||
> (free-identifier=? full-stx #'car) | ||
#t | ||
| ||
> partial-stx | ||
#<syntax> | ||
> (syntax-e partial-stx) | ||
car | ||
> (free-identifier=? partial-stx #'car) | ||
#t |
v : any/c |
A deeply-traversing version of syntax->datum; this copies v, descending into pairs, vectors, and prefab structures, converting syntax objects to the data they contain along the way.
Examples: |
> (to-datum '(a b c d)) |
(a b c d) |
> (to-datum #'(a b c d)) |
(a b c d) |
> (to-datum (list 'a #'(b c) 'd)) |
(a (b c) d) |
Like with-syntax, but with nested scope.
Examples: |
> (with-syntax* ([a #'id] [b #'a]) (syntax-e #'b)) |
id |
5 Text Representations
This module provides tools for manipulating and converting textual data.
| ||
|
This contract and predicate recognize text values: strings, byte strings, symbols, and keywords, as well as syntax objects containing them.
Examples: |
> (text? "text") |
#t |
> (text? #"text") |
#t |
> (text? 'text) |
#t |
> (text? '#:text) |
#t |
> (text? #'"text") |
#t |
> (text? #'#"text") |
#t |
> (text? #'text) |
#t |
> (text? #'#:text) |
#t |
> (text? '(not text)) |
#f |
| ||
| ||
|
These predicates recognize specific text types stored in syntax objects.
Examples: |
> (string-literal? #'"literal") |
#t |
> (string-literal? "not literal") |
#f |
> (bytes-literal? #'#"literal") |
#t |
> (bytes-literal? #"not literal") |
#f |
> (keyword-literal? #'#:literal) |
#t |
> (keyword-literal? '#:not-literal) |
#f |
| ||
| ||
| ||
|
These functions convert text values to specific types, retaining their textual content and concatenating text when necessary.
Examples: |
> (text->string #"concat" #'enate) |
"concatenate" |
> (text->bytes 'concat #'#:enate) |
#"concatenate" |
> (text->symbol '#:concat #'"enate") |
concatenate |
> (text->keyword "concat" #'#"enate") |
#:concatenate |
| |||
| |||
| |||
|
These functions convert text values to specific syntax object types, retaining their textual value, concatenating text when necessary, and deriving syntax object properties from the stx argument.
Examples: | ||
| ||
> (show (text->string-literal #"concat" #'enate)) | ||
#<syntax> | ||
"concatenate" | ||
> (show (text->bytes-literal 'concat #'#:enate)) | ||
#<syntax> | ||
#"concatenate" | ||
> (show (text->identifier '#:concatenate #:stx #'props)) | ||
#<syntax:10:0> | ||
concatenate | ||
> (show (text->keyword-literal "concatenate" #:stx #'props)) | ||
#<syntax:13:0> | ||
#:concatenate |
text : text/c |
This function appends multiple text values, producing a text value of arbitrary type with the concatenated content.
Examples: |
> (text-append "a" #'"b" #"c" #'#"d" 'e #'f '#:g #'#:h) |
"abcdefgh" |
(text=? one two) → boolean? |
one : text/c |
two : text/c |
Compares the character content of two text values.
Examples: |
> (text=? 'a "b") |
#f |
> (text=? #'x (datum->syntax #f 'x)) |
#t |
6 Multiple Values
This module provides tools for functions producing multiple values.
Produces a list of the values returned by expr.
Examples: |
> (values->list (values 1 2 3)) |
(1 2 3) |
| ||||||||
lst : (listof A) |
Produces a pair of lists of the respective values of f applied to the elements in lst ... sequentially.
Examples: |
(2 3 4) |
(0 1 2) |
| ||||||||||
n : natural-number/c | ||||||||||
lst : (listof A) |
Produces lists of the respective values of f applied to the elements in lst ... sequentially.
Examples: | |||||
| |||||
(2 3 4) | |||||
(1 2 3) | |||||
(0 1 2) |
| ||||||||||||
|
These functions combine the values in the lists lst ... using the multiple-valued function f; foldr/values traverses the lists right to left and foldl/values traverses left to right.
Examples: | |||
| |||
10 | |||
(5 6 7 8) | |||
10 | |||
(8 7 6 5) |