Outdated egg!

This is an egg for CHICKEN 3, the unsupported old release. You're almost certainly looking for the CHICKEN 4 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.

  1. Outdated egg!
  2. K/V-lists
    1. Examples
      1. Using key-value lists
      2. A let/kv convenience macro
    2. Author
    3. Constructors
      1. kvlist-cons*
    4. Predicates
      1. kvlist?
      2. alist?
      3. plist?
    5. Selectors
      1. kvlist-key?
      2. kvlist-ref
      3. kvlist-assoc
    6. Iteration
      1. kvlist-map
      2. kvlist-for-each
    7. Filtering
      1. kvlist-delete
    8. Mutators
      1. kvlist-set!
    9. Conversions
      1. kvlist->alist
      2. kvlist->plist
      3. alist->kvlist
      4. alist->plist
      5. plist->kvlist
    10. plist->alist
    11. Miscellaneous
      1. keyword->symbol
      2. symbol->keyword
    12. License
    13. Version history

K/V-lists

This egg provides keyword/value list (or key-value list, for short) operations. Key-value lists are a restricted subset of property lists wherein the key is always a self-evaluating keyword symbol.

In addition to key-value list manipulators, the egg also provides facilities for conversions between kvlists (key-value lists) and both plists (property lists) and alists (association lists).

Here's a run-down of the different ways to use Scheme lists for directly storing mappings/bindings:

Association lists
((k1 . v1) (k2 . v2) (k3 . v3) ...)
Property lists
(k1 v1 k2 v2 k3 v3 ...)
Key-value lists
(k1: v1 k2: v2 k3: v3 ...)

(Self-evaluating keyword symbols are defined in SRFI-88: Keyword objects).

Examples

Using key-value lists

These examples assume Chicken is being run with -keyword-style suffix, which is the default mode. See the manual section Using the interpreter in case you wish to use prefix-style keywords as in Common Lisp.


#;1> (use kvlists)

#;2> (define lst '(foo: 1 bar: 2 baz: 3))

#;3> (kvlist? lst)
#t

#;4> (kvlist-ref lst baz:)
3

#;5> (kvlist-ref lst abc:)
#f

#;6> (kvlist-ref lst abc: 123)
123

#;7> (kvlist->alist lst)
((foo . 1) (bar . 2) (baz . 3))

#;8> (kvlist->plist lst)
(foo 1 bar 2 baz 3)

A let/kv convenience macro

There are many opportunities to use key-value lists to cut down on the proliferation of parentheses in syntactic forms. While this may arguably be against the Lisp ethos, here's a straightforward let/kv macro which uses let to introduce lexically scoped variable bindings but without one layer of enclosing parentheses in the defining form:


(use kvlists)

(define-macro (let/kv head . body)
  `(let ,(kvlist-map (lambda (k v) (list (keyword->symbol k) v))
                     head)
     ,@body))

(let/kv (x: 123 y: 456 z: 789)
  (printf "x + y + z = ~A\n" (+ x y z)))

Author

Arto Bendiken

Constructors

kvlist-cons*

[procedure] (kvlist-cons* KEY VALUE KVLIST)

Predicates

kvlist?

[procedure] (kvlist? X)

alist?

[procedure] (alist? X)

plist?

[procedure] (plist? X)

Selectors

kvlist-key?

[procedure] (kvlist-key? KVLIST KEY)

kvlist-ref

[procedure] (kvlist-ref KVLIST KEY)

kvlist-assoc

[procedure] (kvlist-assoc KEY KVLIST)

Iteration

kvlist-map

[procedure] (kvlist-map PROC KVLIST)

kvlist-for-each

[procedure] (kvlist-for-each PROC KVLIST)

Filtering

kvlist-delete

[procedure] (kvlist-delete KEY KVLIST)

Mutators

kvlist-set!

[procedure] (kvlist-set! KVLIST KEY VALUE)

Conversions

kvlist->alist

[procedure] (kvlist->alist KVLIST)

kvlist->plist

[procedure] (kvlist->plist KVLIST)

alist->kvlist

[procedure] (alist->kvlist ALIST)

alist->plist

[procedure] (alist->plist ALIST)

plist->kvlist

[procedure] (plist->kvlist PLIST)

plist->alist

[procedure] (plist->alist PLIST)

Miscellaneous

keyword->symbol

[procedure] (keyword->symbol KEYWORD)

symbol->keyword

[procedure] (symbol->keyword SYMBOL)

License

 Copyright (c) 2006-2007 Arto Bendiken.
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to
 deal in the Software without restriction, including without limitation the
 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 sell copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 IN THE SOFTWARE.

Version history

1.0.0
Initial release of the kvlists egg.