chickadee » jso

JSOs

Introduction

This library allows the creation and use of JavaScript-style objects (JSOs), which are chained association lists that map keys (which must be symbols) to arbitrary values.

When you create a JSO, you can specify a prototype JSO. The recursive sequence of a JSO's prototype, the prototype of its prototype, and so on, is called the prototype chain. If a JSO doesn't contain a particular key, the prototype chain will be searched for it. Modifications, however, are only applied to the referenced JSO, never to anything along the prototype chain. An attempt to look up an unknown key returns a unique value known as the undefined value, which can be tested with jso-undef?. Note that this value is not itself a JSO.

A JSO is an ordinary Scheme association list with a special key in the first key-value pair. As long as this pair is left undisturbed, they can be searched with assq and the resulting pairs mutated with set-car! and set-cdr!. However, it is safer to use the jso-ref and jso-set! procedures instead.

Author

John Cowan

License

3-clause BSD

Requirements

None

Usage

(import jso)

Reference

Basics

make-jso
make-jsoprocedure
make-jso PROTOprocedure

Constructs a new JSO. If PROTO is supplied, it must be a JSO and is used as the prototype for the new object.

jso?
jso? OBJprocedure

Returns true if OBJ is a JSO.

jso-undef?
jso-undef? OBJprocedure

Returns true if OBJ is the undefined value.

jso-ref
jso-ref JSO KEYprocedure

Retrieves the value for KEY in JSO, or the undefined value if KEY is not found.

jso-set!
jso-set! JSO KEY VALUEprocedure

Sets VALUE to be the value of KEY in JSO; the prototype chain is not searched.

jso-remove!
jso-remove! JSO KEYprocedure

Removes KEY and the associated value from JSO. The prototype chain is not searched. Therefore, if the same key exists in the prototype chain, it will now be exposed. Attempts to remove nonexistent keys are ignored.

jso-proto
jso-proto jsoprocedure

Returns the JSO's prototype, or the undefined value if it has none.

Special values

In addition to key-value pairs, a JSO also has an associated special value, which is the undefined value by default but can be changed to be any Scheme object.

jso-value
jso-value JSOprocedure

Returns the special value of JSO.

jso-set-value!
jso-set-value! JSO VALUEprocedure

Sets the special value of JSO to VALUE.

Copiers

jso-copy
jso-copy JSOprocedure

Returns a shallow copy of JSO that shares the same prototype chain.

jso-full-copy
jso-full-copy JSOprocedure

Returns a shallow copy of JSO including the whole prototype chain. The old and the new JSOs share nothing.

Mappers

These all accept procedures which take two arguments, the key and the value respectively, and return the new value.

jso-map
jso-map PROC JSOprocedure

Returns a new JSO after applying PROC to each key-value pair of JSO. The new and old JSOs share prototypes.

jso-map!
jso-map! PROC JSOprocedure

Modifies JSO by applying PROC to each key-value pair of JSO. The prototype chain is left undisturbed.

jso-for-each
jso-for-each proc jsoprocedure

Executes PROC over all the key-value pairs of JSO. The returned values are ignored. The prototype chain is not processed.

jso-full-map
jso-full-map PROC JSOprocedure

Returns a new JSO after applying PROC to each key-value pair of JSO including the whole prototype chain. The new and old JSOs share nothing.

Note that there is no jso-full-map!, on the principle that no standard JSO procedure makes a destructive change to the prototype chain.

jso-full-for-each
jso-full-for-each PROC JSOprocedure

Executes PROC over all the key-value pairs of JSO, including the whole prototype chain. The returned values are ignored.

Method application

jso-apply
jso-apply JSO KEY #!rest ARGSprocedure

Applies the procedure (method) which is the value of KEY in JSO to JSO. Any trailing arguments are also passed.

jso-apply/fallback
jso-apply/fallback JSO KEY FALLBACK #!rest ARGSprocedure

Applies the procedure (method) which is the value of KEY in JSO to JSO. Any trailing arguments are also passed. If there is no such key, the fallback procedure is invoked instead.

Contents »