callable-data-structures

  1. callable-data-structures
    1. Introduction
    2. Author
    3. Repository
    4. Requirements
    5. API
      1. Module callable-alists
        1. make-callable-alist
        2. callable-alist?
      2. Module callable-hash-tables
        1. make-callable-hash-table
        2. callable-hash-table?
      3. Module callable-lists
        1. make-callable-list
        2. callable-list?
      4. Module callable-strings
        1. make-callable-string
        2. callable-string?
      5. Module callable-vectors
        1. make-callable-vector
        2. callable-vector?
    6. Limitations
    7. License
    8. Version history
      1. Version 1.0.3 (2021-04-17)
      2. Version 1.0.2
      3. Version 1.0.1
      4. Version 1.0

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
[procedure] (make-callable-alist #!optional (alist '()) #!key (test eqv?))

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?
[procedure] (callable-alist? obj)

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

Module callable-hash-tables

make-callable-hash-table
[procedure] (make-callable-hash-table . args)

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?
[procedure] (callable-hash-table? obj)

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

Module callable-lists

make-callable-list
[procedure] (make-callable-list . items)

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?
[procedure] (callable-list? obj)

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

Module callable-strings

make-callable-string
[procedure] (make-callable-string . chars)

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?
[procedure] (callable-string? obj)

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

Module callable-vectors

make-callable-vector
[procedure] (make-callable-vector . items)

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?
[procedure] (callable-vector? obj)

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