alist-lib

SRFI-69-like library for alists

  1. alist-lib
    1. alist-values
    2. alist-keys
    3. alist-map
    4. alist-set!
    5. alist-update!
    6. alist-update!/default
    7. alist-ref
    8. alist-ref/default
    9. alist-size
    10. alist-fold
    11. alist-set
    12. About this egg
      1. Author
      2. Repository
      3. License
      4. Dependencies
      5. Versions
      6. Colophon

alist-values

[procedure] (alist-values alist) → list

Extract the associations from an alist.

alist
The alist from which to extract
(define (alist-values alist) (map cdr alist))

alist-keys

[procedure] (alist-keys alist) → list

Extract the keys from an alist.

alist
The alist from which to extract
(define (alist-keys alist) (map car alist))

alist-map

[procedure] (alist-map f alist) → list

Map across an alist; f takes two parameters: key and values.

f
The function to apply to each key-value association
alist
The alist to apply to
(define (alist-map f alist)
  (map (match-lambda ((key . values) (f key values))) alist))

alist-set!

[syntax] (alist-set! alist key value) → unspecified

Destructively set a key-value association.

alist
The alist in which to set
key
The key to set
value
The value to associate with the key
(define-syntax
  alist-set!
  (lambda (expression rename compare)
    (match expression
           ((_ variable key value)
            (let ((%if (rename 'if))
                  (%null? (rename 'null?))
                  (%set! (rename 'set!))
                  (%list (rename 'list))
                  (%cons (rename 'cons))
                  (%alist-prepend! (rename 'alist-prepend!)))
              `(,%if
                (,%null? ,variable)
                (,%set! ,variable (,%list (,%cons ,key ,value)))
                (,%alist-prepend! ,variable ,key ,value)))))))

alist-update!

[procedure] (alist-update! alist key function) → unspecified
[procedure] (alist-update! alist key function thunk) → unspecified
[procedure] (alist-update! alist key function thunk =) → unspecified

On analogy with hash-table-update!, descructively update an association.

alist
The alist to update
key
The key associated with the update
f
A monadic function taking the preëxisting key
thunk
The thunk to apply if no association exists
=
The equality predicate for keys
(define alist-update!
  (case-lambda
    ((alist key function)
     (alist-update!
       alist
       key
       function
       (lambda () (error "Key not found -- ALIST-UPDATE!" key))))
    ((alist key function thunk) (alist-update! alist key function thunk eqv?))
    ((alist key function thunk =)
     (let ((pair (assoc key alist =)))
       (if pair
         (set-cdr! pair (function (cdr pair)))
         (alist-set! alist key (function (thunk))))))))

alist-update!/default

[procedure] (alist-update!/default alist key function default) → unspecified
[procedure] (alist-update!/default alist key function default =) → unspecified

On analogy with hash-table-update!, descructively update an association.

alist
The alist to update
key
The key associated with the update
f
A monadic function taking the preëxisting key
default
The default value if no association exists
=
The equality predicate for keys
(define alist-update!/default
  (case-lambda
    ((alist key function default)
     (alist-update!/default alist key function default eqv?))
    ((alist key function default =)
     (alist-update! alist key function (lambda () default)))))

alist-ref

[procedure] (alist-ref alist key) → object
[procedure] (alist-ref alist key thunk) → object
[procedure] (alist-ref alist key thunk =) → object

Return a value associated with its key or apply thunk.

alist
The alist to search in
key
The key whose value to return
thunk
The thunk to apply when association doesn't exist (default is to err)
=
The equality predicate to apply to keys
(define alist-ref
  (case-lambda
    ((alist key)
     (alist-ref
       alist
       key
       (lambda () (error "Key not found -- ALIST-REF" key))))
    ((alist key thunk) (alist-ref alist key thunk eqv?))
    ((alist key thunk =)
     (let ((value (assoc key alist =))) (if value (cdr value) (thunk))))))

alist-ref/default

[procedure] (alist-ref/default alist key default) → object
[procedure] (alist-ref/default alist key default =) → object

Return a value associated with its key or default.

alist
The alist to search in
key
The key whose value to return
default
The default to return when association doesn't exist
=
The equality predicate to apply to keys
(define alist-ref/default
  (case-lambda
    ((alist key default) (alist-ref alist key (lambda () default)))
    ((alist key default =) (alist-ref alist key (lambda () default) =))))

alist-size

[procedure] (alist-size alist) → integer

Calculate size of alist.

alist
The alist whose size to calculate
(define alist-size length)

alist-fold

[procedure] (alist-fold alist f init) → object

Fold an alist; whose f takes key, accumulatum, value.

alist
The alist to fold
f
The function to apply to key, accumulatum, value
init
The seed of the fold
(define (alist-fold alist f init)
  (fold (lambda (association accumulatum)
          (match association ((key . value) (f key accumulatum value))))
        init
        alist))

alist-set

[procedure] (alist-set alist key value) → alist

Non-destructively associate a key and value in the alist.

alist
Alist in which to set
key
The key to set
value
The value to associate with key
(define (alist-set alist key value) (alist-cons key value alist))

About this egg

Author

Peter Danenberg

Repository

https://github.com/klutometis/alist-lib

License

BSD

Dependencies

Versions

0.1
Version 0.1
0.1.1
Version 0.1.1
0.1.2
Meta fixes
0.1.3
More meta
0.1.4
Housekeeping
0.2.1
Change to BSD.
0.2.2
Remove debug
0.2.3
Add docs.
0.2.4
Failure condition
0.2.5
With a note about cock-utils
0.2.6
Add test-exit.
0.2.7
alist-set! should work on empty lists.

Colophon

Documented by cock.