Scheme Utilities in (planet cce/scheme:1:2)
This is my personal library of PLT Scheme programming utilities. Feel free to use it, copy my code, or ask me questions.
The default module provides all the bindings described below under the individual modules.
1 Classes and Objects
This module provides tools for classes, objects, and mixins.
Recognizes classes and interfaces.
spec : class-or-interface/c |
Recognizes objects which are instances of all the given classes and interfaces.
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: | ||||||
| ||||||
| ||||||
|
2 Functions
This module provides tools for higher-order programming and creating functions.
v : any/c |
This function returns its argument.
v : any/c |
Produces a function that returns v regardless of input.
Examples: | ||
| ||
> (f) | ||
g368 | ||
> (f '(4 5 6)) | ||
g368 | ||
> (f #:x 7 #:y 8 #:z 9) | ||
g368 |
| ||||||
|
Partial applications of a function; curry* provides the left arguments first, while curryr* provides the right arguments first.
((curry* f x ...) y ...) = (f x ... y ...)
((curryr* f x ...) y ...) = (f y ... x ...)
Examples: | ||
| ||
> ((curry* f 1) #:x 2) | ||
(1 0 2 0) | ||
> ((curry* f 1 #:x 3) 2 #:y 4) | ||
(1 2 3 4) | ||
> ((curryr* f 1) #:x 2) | ||
(1 0 2 0) | ||
> ((curryr* f 1 #:x 3) 2 #:y 4) | ||
(2 1 3 4) |
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 |
f : (-> A ... B) |
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" |
3 Imperative Queues
This module provides a mutable queue representation.
Produces an empty queue.
(enqueue! q v) → void? |
q : queue/c |
v : any/c |
Adds an element to the back of a queue.
q : nonempty-queue/c |
Removes an element from the front of a nonempty queue, and returns that element.
Examples: | ||
| ||
> (enqueue! q 1) | ||
> (dequeue! q) | ||
1 | ||
> (dequeue! q) | ||
top-level broke the contract (-> "nonempty-queue" any/c) on | ||
dequeue!; expected <nonempty-queue>, given: #<queue> | ||
> (enqueue! q 2) | ||
> (enqueue! q 3) | ||
> (dequeue! q) | ||
2 | ||
> (dequeue! q) | ||
3 | ||
> (dequeue! q) | ||
top-level broke the contract (-> "nonempty-queue" any/c) on | ||
dequeue!; expected <nonempty-queue>, given: #<queue> |
(queue-empty? q) → boolean? |
q : queue/c |
Recognizes whether a queue is empty or not.
Examples: | ||
| ||
> (queue-empty? q) | ||
#t | ||
> (enqueue! q 1) | ||
> (queue-empty? q) | ||
#f | ||
> (dequeue! q) | ||
1 | ||
> (queue-empty? q) | ||
#t |
(queue? v) → boolean? |
v : any/c |
This predicate recognizes queues.
Examples: |
> (queue? (make-queue)) |
#t |
> (queue? 'not-a-queue) |
#f |
These contracts recognize queues; the latter requires the queue to contain at least one value.
4 Syntax Objects
This module provides tools for macro transformers.
datum/c : flat-contract/predicate? |
Recognizes syntax objects stx such that (syntax->datum stx) satisfies datum/c.
elem/c : flat-contract/predicate? |
Recognizes syntax objects stx such that (syntax->list stx) satisfies (listof elem/c).
elem/c : flat-contract/predicate? |
Recognizes syntax objects stx such that (syntax->list stx) satisfies (list/c elem/c ...).
f : (-> syntax? 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 | ||||||||||||||||||||||||||||||||||||||||||
stx : (or/c false/c syntax?) = #f | ||||||||||||||||||||||||||||||||||||||||||
src : (or/c false/c syntax?) = #f | ||||||||||||||||||||||||||||||||||||||||||
ctxt : (or/c false/c syntax?) = #f | ||||||||||||||||||||||||||||||||||||||||||
prop : (or/c false/c syntax?) = #f | ||||||||||||||||||||||||||||||||||||||||||
cert : (or/c false/c syntax?) = #f |
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 |
(to-datum v) → (not/c syntax?) |
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) |
current-syntax : (parameter/c (or/c syntax? false/c)) |
A parameter that may be used to store the current syntax object being transformed. It is not used by the expander; you have to assign to it yourself. This parameter is used by syntax-error, below. It defaults to #f.
stx : syntax? |
fmt : string? |
arg : any/c |
Raises a syntax error based on the locations of (current-syntax) and stx, with (format fmt arg ...) as its message.
Examples: | ||
| ||
| ||
eval:1:0: a: general location in: (a b c) | ||
| ||
eval:1:0: a: specific location at: a in: (a b c) |
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 |
> (text=? '#:z #"y") |
#f |
(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)) |
#f |
> (text>? '#:z #"y") |
#t |
(text<? one two) → boolean? |
one : text/c |
two : text/c |
Compares the character content of two text values.
Examples: |
> (text<? 'a "b") |
#t |
> (text<? #'x (datum->syntax #f 'x)) |
#f |
> (text<? '#:z #"y") |
#f |
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) |