| 1 |
;;; |
| 2 |
;;; SRFI-222 - Compound objects |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2022-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module srfi.222 |
| 35 |
(use gauche.record) |
| 36 |
(use data.queue) |
| 37 |
(export make-compound compound? |
| 38 |
compound-subobjects compound-length compound-ref |
| 39 |
compound-map compound-map->list compound-filter |
| 40 |
compound-predicate compound-access |
| 41 |
)) |
| 42 |
(select-module srfi.222) |
| 43 |
|
| 44 |
(define-class <compound> () |
| 45 |
((subobjects :init-keyword :subobjects) ; list of objects |
| 46 |
)) |
| 47 |
|
| 48 |
(define-method write-object ((c <compound>) port) |
| 49 |
(format port "#<compound ~s>" (~ c'subobjects))) |
| 50 |
|
| 51 |
(define-method object-hash ((c <compound>) rec-hash) |
| 52 |
(rec-hash (~ c'subobjects))) |
| 53 |
|
| 54 |
(define-method object-compare ((a <compound>) (b <compound>)) |
| 55 |
(compare (~ a'subobjects) (~ b'subobjects))) |
| 56 |
|
| 57 |
(define (%list->compound objs) |
| 58 |
(let1 q (make-queue) |
| 59 |
(dolist [o objs] |
| 60 |
(if (compound? o) |
| 61 |
(apply enqueue! q (~ o'subobjects)) |
| 62 |
(enqueue! q o))) |
| 63 |
(make <compound> :subobjects (dequeue-all! q)))) |
| 64 |
|
| 65 |
(define (make-compound . objs) (%list->compound objs)) |
| 66 |
|
| 67 |
(define (compound? obj) (is-a? obj <compound>)) |
| 68 |
|
| 69 |
(define (compound-subobjects obj) |
| 70 |
(if (compound? obj) |
| 71 |
(~ obj'subobjects) |
| 72 |
(list obj))) |
| 73 |
|
| 74 |
(define (compound-length obj) |
| 75 |
(if (compound? obj) |
| 76 |
(length (~ obj'subobjects)) |
| 77 |
1)) |
| 78 |
|
| 79 |
(define (compound-ref obj k) |
| 80 |
(assume (and (exact-integer? k) (not (negative? k))) |
| 81 |
"Index must be nonnegative exact integer, but got:" k) |
| 82 |
(if (compound? obj) |
| 83 |
(~ obj'subobjects k) |
| 84 |
(if (= k 0) obj (error "Index out of range:" k)))) |
| 85 |
|
| 86 |
(define (compound-map mapper obj) |
| 87 |
(%list->compound (compound-map->list mapper obj))) |
| 88 |
|
| 89 |
(define (compound-map->list mapper obj) |
| 90 |
(map mapper (compound-subobjects obj))) |
| 91 |
|
| 92 |
(define (compound-filter pred obj) |
| 93 |
(%list->compound (filter pred (compound-subobjects obj)))) |
| 94 |
|
| 95 |
(define (compound-predicate pred obj) |
| 96 |
(or (pred obj) |
| 97 |
(and (compound? obj) |
| 98 |
(boolean (any pred (~ obj'subobjects)))))) |
| 99 |
|
| 100 |
(define (compound-access pred acc default obj) |
| 101 |
(cond [(pred obj) (acc obj)] |
| 102 |
[(and (compound? obj) |
| 103 |
(find pred (~ obj'subobjects))) |
| 104 |
=> acc] |
| 105 |
[else default])) |