For Development HEAD DRAFTSearch (procedure/syntax/module):

11.57 srfi.222 - Compound objects

Module: srfi.222

Compound objects are encapsulation of an immutable sequence of objects. Any objects other than a compound object can be included in a compound object; if you try to create a compound object including compound objects, it is ’flattened’—that is, the source’s subobjects are retrieved and each one is put into the new compound object.

This is an abstraction of the compound condition (see Conditions), although Gauche’s compound conditions are not built on this.

This module is mainly provided for the compatibility; used with R7RS records (see gauche.record - Record types), you can have a portable multiple inheritance of records.

Function: make-compound obj …

[SRFI-222]{srfi.222} Returns a compound objects having obj … as its subobjects. Each obj can any Scheme object, but if obj is a compound object itself, it is expanded into its subobjects:

(make-compound 1 2 3)
 ⇒ #<compound-object (1 2 3)>
(make-compund 0 (make-compound 1 2 3) 4 5)
 ⇒ #<compound-object (0 1 2 3 4 5)>
Function: compound? obj

[SRFI-222]{srfi.222} Returns #t iff obj is a compound object.

Function: compound-subobjects obj

[SRFI-222]{srfi.222} If obj is a compound, returns a list of its subobjects. Otherwise, returns a 1-element list of obj.

That is, it always returns a list of non-compound objects.

Function: compound-length obj

[SRFI-222]{srfi.222} If obj is a compound, returns the number of its subobjects. Otherwise, returns 1.

Function: compound-ref obj k

[SRFI-222]{srfi.222} Returns k-th subobject of obj if it is a compound. Othrewise, k must be zero and obj itself is returned.

An error is thrown if k is not a nonnegative exact integer or out of range.

Function: compound-map mapper obj

[SRFI-222]{srfi.222} Returns a compound object whose subobjects are result of applying mapper on each subobject of obj. If obj is not a compound, it is treated as if a compound with obj as a sole subobject. Note that if mapper returns a compound, its subobjects are spliced in place. Thus the number of subobjects of the result may differ from the number of subobjects of obj.

(compound-map (cut + 1 <>) (make-compound 1 2 3))
 ⇒ #<compound (2 3 4)>
(compound-map (cut make-compound <> 'x) (make-compound 1 2 3))
 ⇒ #<compound (1 x 2 x 3 x)>
(compound-map (cut + 1 <>) 3)
 ⇒ #<compound (4)>
Function: compound-map->list mapper obj

[SRFI-222]{srfi.222} Returns a list of results of applying mapper on each subobjects of obj. If obj is not a compound, mapper is directly applied on obj.

Unlike compound-map, the result of mapper appears as is in the result list, even when it is a compound object. Hence the length of the result list always matches (compound-length obj).

(compound-map->list (cut + 1 <>) (make-compound 1 2 3))
 ⇒ (2 3 4)
(compound-map->list (cut make-compound <> 'x) (make-compound 1 2 3))
 ⇒ (#<compound (1 x)> #<compound (2 x)> #<compound (3 x)>)
(compound-map->list (cut + 1 <>) 3)
 ⇒ (4)
Function: compound-filter pred obj

[SRFI-222]{srfi.222} Collect subobjects of obj that satisfies pred, and returns a compound that consists of them. If obj is not a compound, it is treated as if it is a compound with obj as a sole subobject.

(compound-filter odd? (make-compound 1 2 3))
 ⇒ #<compound (1 3)>
(compound-filter odd? 1)
 ⇒ #<compound (1)>
(compound-filter odd? 2)
 ⇒ #<compound ()>
Function: compound-predicate pred obj

[SRFI-222]{srfi.222} Returns #t if obj satisfies pred, or any of obj’s subobjects satisfies pred when obj is a compound. Returns #f otherwise.

Function: compound-access pred acc default obj

[SRFI-222]{srfi.222} If obj satisfies pred, acc is applied ot obj and the result is returned. Otherwise, if obj is a compound, pred is applied on each subobject, and acc is applied on the first subobject that satisfies pred, and the result is returned. If none of the case is satisfied, default is returned.

(define-record-type <foo> make-foo foo?
  (f foo-f))

(define-record-type <bar> make-bar bar?
  (b bar-b))

(define c (make-compound (make-foo "foo!") (make-bar "bar!") '(ca . cd)))

(compound-access foo? foo-f 'none c) ⇒ "foo!"
(compound-access bar? bar-b 'none c) ⇒ "bar!"
(compound-access pair? car  'none c) ⇒ ca
(compound-access char? char->integer 'none c) ⇒ none


For Development HEAD DRAFTSearch (procedure/syntax/module):
DRAFT