chickadee » callable-data-structures

Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

callable-data-structures

Introduction

This egg provides "callable" data structures (lists, alists, vectors, hash tables and strings). "Callable" data structures are actually special procedures that internally hold a data structure.

Here's an example:

#;1> (use callable-alists)
#;2> (define alist (make-callable-alist))
#;3> alist
#<procedure callable-alist>
#;4> (alist 'foo)
#f
#;5> (alist 'foo 'not-set)
not-set
#;6> (set! (alist 'foo) 42)
#;7> (alist 'foo)
42

When called without arguments, "callable" data structures return their internal data structure, so you can apply any accessor compatible with them. Example:

#;1> (use callable-hash-tables)
#;2> (define hash-table (make-callable-hash-table))
#;3> (hash-table-exists? (hash-table) 'foo)
#f
#;4> (set! (hash-table 'foo) 42)
#;5> (hash-table-exists? (hash-table) 'foo)
#t
#;6> (hash-table 'foo)
42

Author

Mario Domenech Goulart

Repository

https://github.com/mario-goulart/callable-data-structures

Requirements

None

API

This extension provides a separate module for each supported data structure. So, the following modules are available:

It also provides a callable-data-structures which exports all symbols from all modules.

Module callable-alists

make-callable-alist
make-callable-alist #!optional (alist '()) #!key (test eqv?)procedure

Return a callable alist object. The initial alist is optional if the test keyword parameter is not given.

The test keyword parameter specifies the key comparison procedure (default: eqv?).

Besides the alist key, the callable alist object accepts the default keyword parameter, which specifies the default value for when the given key doesn't exist (default: #f).

Examples:

#;1> (use callable-alists)
#;2> (define symbols (make-callable-alist))
#;3> symbols
#<procedure callable-alist>
#;4> (symbols)
()
#;5> (set! (symbols 'foo) 42)
#;6> (symbols 'foo)
42
#;7> (symbols)
((foo . 42))
#;8> (define strings (make-callable-alist '() test: equal?))
#;9> strings
#<procedure callable-alist>
#;10> (strings)
()
#;11> (set! (strings "foo") 42)
#;12> (strings "foo")
42
#;13> (strings)
(("foo" . 42))
callable-alist?
callable-alist? objprocedure

Type predicate. Return #t if obj is a callable alist or #f if it is not.

Module callable-hash-tables

make-callable-hash-table
make-callable-hash-table #!rest argsprocedure

Return a callable hash table object. The initial value is an alist, and it is optional if no other keyword parameter is provided.

All the keyword parameters for make-hash-table are valid for make-callable-hash-table.

Examples:

#;1> (use callable-hash-tables)
#;2> (define hash-table (make-callable-hash-table))
#;3> hash-table
#<procedure callable-hash-table>
#;4> (hash-table)
#<hash-table (0)>
#;5> (set! (hash-table 'foo) 42)
#;6> (hash-table 'foo)
42
#;1> (use callable-hash-tables)
#;2> (define hash-table (make-callable-hash-table '((foo . 42) (bar . 0))))
#;3> (hash-table 'foo)
42
#;4> (hash-table 'bar)
0
#;1> (use callable-hash-tables)
#;2> (define hash-table (make-callable-hash-table '(("foo" . 42) ("bar" . 0)) test: equal?))
#;3> (hash-table "foo")
42
#;4> (hash-table "bar")
0
callable-hash-table?
callable-hash-table? objprocedure

Type predicate. Return #t if obj is a callable hash table or #f if it is not.

Module callable-lists

make-callable-list
make-callable-list #!rest itemsprocedure

Return a callable list object. It accepts an arbitrary number if items.

Example:

#;1> (use callable-lists)
#;2> (define l (make-callable-list "foo" "bar" "baz"))
#;3> l
#<procedure callable-list>
#;4> (l)
("foo" "bar" "baz")
#;5> (l 2)
"baz"
#;6> (set! (l 1) "quux")
#;7> (l)
("foo" "quux" "baz")
callable-list?
callable-list? objprocedure

Type predicate. Return #t if obj is a callable list or #f if it is not.

Module callable-strings

make-callable-string
make-callable-string #!rest charsprocedure

Return a callable string object. It accepts an arbitrary number of characters.

Example:

#;1> (use callable-strings)
#;2> (define s (make-callable-string #\f #\o #\o))
#;3> s
#<procedure callable-string>
#;4> (s)
"foo"
#;5> (s 1)
#\o
#;6> (set! (s 0) #\d)
#;7> (s)
"doo"
callable-string?
callable-string? objprocedure

Type predicate. Return #t if obj is a callable string or #f if it is not.

Module callable-vectors

make-callable-vector
make-callable-vector #!rest itemsprocedure

Return a callable vector object. It accepts an arbitrary number of items.

Example:

#;1> (use callable-vectors)
#;2> (define v (make-callable-vector 'foo "bar" 42))
#;3> v
#<procedure callable-vector>
#;4> (v)
#(foo "bar" 42)
#;5> (v 1)
"bar"
#;6> (set! (v 1) "quux")
#;7> (v)
#(foo "quux" 42)
callable-vector?
callable-vector? objprocedure

Type predicate. Return #t if obj is a callable vector or #f if it is not.

Limitations

Callable data strucutures are special procedures, but still procedures, which means the procedure predicate returns #t when given a callable data structure object:

#;1> (use callable-alists)
#;2> (define alist (make-callable-alist))
#;3> (procedure? alist)
#t
#;4> (callable-alist? alist)
#t

License

 Copyright (c) 2013-2021, Mario Domenech Goulart
 All rights reserved.
 
 Redistribution and use in source and binary forms, with or without
 modification, are permitted provided that the following conditions
 are met:
 1. Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.
 2. 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.
 3. The name of the authors may not be used to endorse or promote products
    derived from this software without specific prior written permission.
 
 THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 AUTHORS 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

Version 1.0.3 (2021-04-17)

Version 1.0.2

Version 1.0.1

Version 1.0

Contents »