1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
4 ;;; This program is free software: you can redistribute it and/or modify
5 ;;; it under the terms of the GNU General Public License as published by
6 ;;; the Free Software Foundation, either version 3 of the License, or
7 ;;; (at your option) any later version.
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;; GNU General Public License for more details.
14 ;;; You should have received a copy of the GNU General Public License
15 ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 (define-module (gacela utils)
19 #:export (use-cache-with
25 ;;; Cache for procedures
27 (define (use-cache-with proc)
28 "Cache for procedures"
29 (let ((cache (make-weak-value-hash-table)))
32 (res (hash-ref cache key)))
35 (set! res (apply proc param))
36 (hash-set! cache key res)
40 ;;; Playing with procedures arguments
43 (define (bound? var) (not (eq? var undefined)))
45 (define (required-arguments args values)
46 "Return an alist with required arguments and their values"
47 (define (f vars values)
48 (cond ((null? vars) '())
49 ((null? values) (assoc-set! (f (cdr vars) '())
52 (else (assoc-set! (f (cdr vars) (cdr values))
55 (f (assoc-ref args 'required) values))
57 (define (optional-arguments args values)
58 "Return an alist with optional arguments and their values"
59 (define (f vars values)
60 (cond ((null? vars) '())
61 ((null? values) (assoc-set! (f (cdr vars) '())
64 (else (assoc-set! (f (cdr vars) (cdr values))
67 (f (assoc-ref args 'optional)
69 (min (length (assoc-ref args 'required))
72 (define (keyword-arguments args values)
73 "Return an alist with keyword arguments and their values"
74 (define (f vars values)
75 (cond ((null? vars) '())
77 (let ((val (member (car vars) values)))
78 (assoc-set! (f (cdr vars) values)
79 (keyword->symbol (car vars))
80 (if val (cadr val) undefined))))))
81 (f (map (lambda (x) (car x)) (assoc-ref args 'keyword)) values))
83 (define (rest-arguments args values)
84 "Return an alist with rest arguments"
85 (let ((rest (assoc-ref args 'rest)))
86 (cond (rest (assoc-set! '()
89 (min (+ (length (assoc-ref args 'required))
90 (length (assoc-ref args 'optional)))
94 (define (arguments-calling proc values)
95 "Return an alist with procedure arguments and their values"
96 (let ((args ((@ (ice-9 session) procedure-arguments) proc)))
97 (append (required-arguments args values)
98 (optional-arguments args values)
99 (keyword-arguments args values)
100 (rest-arguments args values))))
102 (define (required-arguments-apply args values)
103 "Return a list with required arguments for use with apply"
104 (define (f vars values)
105 (cond ((null? vars) '())
107 (cons (assoc-ref values (car vars))
108 (f (cdr vars) values)))))
109 (f (assoc-ref args 'required) values))
111 (define (optional-arguments-apply args values)
112 "Return a list with optional arguments for use with apply"
113 (define (f vars values)
114 (cond ((null? vars) '())
115 (else (let ((a (f (cdr vars) values))
116 (val (assoc (car vars) values)))
117 (cond ((and val (bound? (cdr val)))
120 (f (assoc-ref args 'optional) values))
122 (define (keyword-arguments-apply args values)
123 "Return a list with keyword arguments for use with apply"
124 (define (f vars values)
125 (cond ((null? vars) '())
126 (else (let ((a (f (cdr vars) values))
127 (val (assoc (keyword->symbol (car vars)) values)))
128 (cond ((and val (bound? (cdr val)))
129 (cons (car vars) (cons (cdr val) a)))
131 (f (map (lambda (x) (car x)) (assoc-ref args 'keyword)) values))
133 (define (rest-arguments-apply args values)
134 "Return a list with rest arguments for use with apply"
135 (let ((rest (assoc-ref args 'rest)))
136 (cond (rest (assoc-ref values rest))
139 (define (arguments-apply proc values)
140 "Return a list for use with apply"
141 (let ((args ((@ (ice-9 session) procedure-arguments) proc)))
142 (append (required-arguments-apply args values)
143 (optional-arguments-apply args values)
144 (keyword-arguments-apply args values)
145 (rest-arguments-apply args values))))