1 ;;;; Copyright (C) 2023 Vouivre Digital Corporation
3 ;;;; This file is part of Vouivre.
5 ;;;; Vouivre is free software: you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU General Public
7 ;;;; License as published by the Free Software Foundation, either
8 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;; Vouivre is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; General Public License for more details.
15 ;;;; You should have received a copy of the GNU General Public
16 ;;;; License along with Vouivre. If not, see <https://www.gnu.org/licenses/>.
18 (define-module (vouivre misc)
19 #:use-module (ice-9 arrays)
20 #:use-module (ice-9 match)
21 #:use-module (srfi srfi-1)
36 (define (but-last lst)
38 (error "List must contain at least one element.")
42 (but-last (cdr lst))))))
44 (define (alist<-plist plist)
45 "Convert a property list to an association list."
49 (match-let (((p . bindings) prev))
51 (cons (cons #f x) bindings)
54 (cons '() (cons p bindings))))))
59 "Returns a procedure behaving as `f', but with arguments taken in reverse
62 (apply f (reverse args))))
66 [(_ (x test) consequent alternate)
68 (if x consequent alternate))]
69 [(_ (x test) consequent)
75 [(_ test alternate consequent)
76 (if test consequent alternate)]
78 (if (not test) alternate)]))
80 (define (list-zeros n)
81 (list-tabulate n (lambda _ 0)))
83 (define (map-indexed f . lists)
84 "Like `map' but the last argument of `f' is passed the corresponding index."
85 (apply map f (append lists (list (list-tabulate (length (car lists))
88 (define (for-indices-in-range f starts ends)
89 (define (for-indices-in-range% f indices starts ends)
91 (apply f (reverse indices))
92 (do ((i (car starts) (1+ i)))
94 (for-indices-in-range%
99 (for-indices-in-range% f '() starts ends))
103 (define (produce-typed-array f type . dims)
104 (let ((a (apply make-typed-array type *unspecified* dims)))
105 (array-index-map! a f)
108 (define (produce-array f . dims)
109 (apply produce-typed-array f #t dims))
111 (define (array-map proc array . more)
112 (let ((x (array-copy array)))
113 (apply array-map! x proc array more)
116 (define (array-map-indexed proc array)
117 (let ((x (array-copy array)))
122 (apply array-ref array indices)