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

Sequential- and random-access sequences as procedures

This is a variant of Mario's callable-datastructures. But contrary to that egg, I don't consider hash-tables, but only ordered sequences.

The most important procedure, make-callable, is a generic procedure with a local database supporting lists, pseudolists, vectors and strings. But this database can be enhanced, by giving make-callable two unary procedures as arguments, a type predicate and a maker, the latter being a spezialization of make-sas-callable or make-ras-callable, handling sequential-access or random-access sequences respectively.

Giving make-callable one argument, a sequence, a procedure of zero, one or two arguments is returned. The first call returns the encapsulated data and its length, the second the data's value at its index argument and the third the slice between its two index arguments, in either direction, the first included, the second excluded. For convenience, the argument #f is allowed in slices, representing the length.

So, for example, if vec is (make-callable #(0 1 2 3 4 5)), then (vec 1 4) or (vec 4 1) are callables comprising #(1 2 3) or #(4 3 2) respectively, and, using length, (vec 3 #f) or (vec #f 3) comprise #(3 4 5) or #(5 4) respectively.

API

callable-sequences

[procedure] (callable-sequences)
[procedure] (callable-sequences sym)

documentation procedure: returns the list of exported symbols if called without argument, or the documentation of sym otherwise.

make-callable

[procedure] (make-callable)
[procedure] (make-callable seq)
[procedure] (make-callable seq? seq-maker?)

generic procedure.

The first resets the local datbase to standard form, which can handle lists, vectors, strings and pseudolists.

The second transforms the sequence into a procedure of zero, one or two arguments, meaning

The third updates the local database inserting the new pair in next to last position. The seq-maker argument specializes one of the two constructors make-sas-callable and make-ras-callable respectively.

make-sas-callable

[procedure] (make-sas-callable seq seq-cons seq-car seq-cdr seq-null?)

constructor for sequential-access sequences: transforms the sequence, seq, into a procedure of zero, one or two arguments as above.

make-ras-callable

[procedure] (make-ras-callable seq make-seq seq-ref seq-set! seq-length)

constructor for random-access sequences: transforms the sequence, seq, into a procedure of zero, one or two arguments as above.

callable-sas?

[procedure] (callable-sas? xpr)

type predicate for callable sequential-access sequences

callable-ras?

[procedure] (callable-ras? xpr)

type predicate for callable random-access sequences

callable?

[procedure] (callable? xpr)

type predicate for arbitrary sequences

callable-null?

[procedure] (callable-null? xpr)

is xpr a callable-sequence with empty data?

callable-length

[procedure] (callable-length cs)

returns the length of the callable sequence cs

callable-data

[procedure] (callable-data cs)

returns the encapsulated data of the callable sequence cs

callable-reverse

[procedure] (callable-reverse cs)

returns a callable sequence, which is the reverse of the original one

Requirements

None

Examples


(import callable-sequences)

(define vec (make-callable #(0 1 2 3 4 5)))

(define lst (make-callable '(0 1 2 3 4 5)))

(define pair (make-callable '(0 1 2 3 4 5 . 6)))

(define str (make-callable "012345"))

(lst 2)
; -> 2

(vec 5)
; -> 5

(str 1)
; -> #\1

(callable-length pair)
; -> 6

(callable-length str)
; -> 6

(callable-sas? lst)
; -> #t

(callable-ras? lst)
; -> #f

(callable? lst)
; -> #t

(callable? (pair 2 4))
; -> #t

(callable-data (callable-reverse pair))
; ->  '(5 4 3 2 1 0 . 6))

(callable-data vec)
; -> #(0 1 2 3 4 5)

(callable-date (vec 2 4))
; -> #(2 3)

(callable-data (lst 2 4))
; -> '(2 3)

(callable-data (lst 2 #f))
; -> '(2 3 4 5)

(callable-data (vec #f 2)
; -> #(5 4 3)

(callable-data (pair 4 2))
; -> '(4 3 . 6)

(callable-data (pair 2 2))
; -> 6

(callable-null? (pair 2 2))
; -> #t

(callable-data (str 2 5))
; -> "234"

(callable-data (str 2 2))
; -> ""

(callable-data (str 5 2))
; -> "543"

(callable-data (callable-reverse str))
; -> "543210"

Last update

Aug 17, 2020

Author

Juergen Lorenz

License

Copyright (c) 2020, Juergen Lorenz
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the author nor the names of its contributors may be
used to endorse or promote products derived from this software without
specific prior written permission. 
  
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Version History

1.0.0 ; initial version