]> git.jsancho.org Git - gacela.git/blob - src/utils.scm
Functions for playing with arguments when apply to procedures.
[gacela.git] / src / utils.scm
1 ;;; Gacela, a GNU Guile extension for fast games development
2 ;;; Copyright (C) 2009 by Javier Sancho Fernandez <jsf at jsancho dot org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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/>.
16
17
18 (define-module (gacela utils)
19   #:export (use-cache-with
20             arguments-calling
21             arguments-apply))
22
23
24 ;;; Cache for procedures
25
26 (define (use-cache-with proc)
27   "Cache for procedures"
28   (let ((cache (make-weak-value-hash-table)))
29     (lambda (. param)
30       (let* ((key param)
31              (res (hash-ref cache key)))
32         (cond (res res)
33               (else
34                (set! res (apply proc param))
35                (hash-set! cache key res)
36                res))))))
37
38
39 ;;; Playing with procedures arguments
40
41 (define undefined)
42 (define (bound? var) (not (eq? var undefined)))
43
44 (define (required-arguments args values)
45   "Return an alist with required arguments and their values"
46   (define (f vars values)
47     (cond ((or (null? vars) (null? values)) '())
48           (else (assoc-set! (f (cdr vars) (cdr values))
49                             (car vars)
50                             (car values)))))
51   (f (assoc-ref args 'required) values))
52
53 (define (optional-arguments args values)
54   "Return an alist with optional arguments and their values"
55   (define (f vars values)
56     (cond ((null? vars) '())
57           ((null? values) (assoc-set! (f (cdr vars) '())
58                                       (car vars)
59                                       undefined))
60           (else (assoc-set! (f (cdr vars) (cdr values))
61                             (car vars)
62                             (car values)))))
63   (f (assoc-ref args 'optional)
64      (list-tail values
65                 (min (length (assoc-ref args 'required))
66                      (length values)))))
67
68 (define (keyword-arguments args values)
69   "Return an alist with keyword arguments and their values"
70   (define (f vars values)
71     (cond ((null? vars) '())
72           (else
73            (let ((val (member (car vars) values)))
74              (assoc-set! (f (cdr vars) values)
75                             (keyword->symbol (car vars))
76                             (if val (cadr val) undefined))))))
77   (f (map (lambda (x) (car x)) (assoc-ref args 'keyword)) values))
78
79 (define (rest-arguments args values)
80   "Return an alist with rest arguments"
81   (let ((rest (assoc-ref args 'rest)))
82     (cond (rest (assoc-set! '()
83                             rest
84                             (list-tail values
85                                        (min (+ (length (assoc-ref args 'required))
86                                                (length (assoc-ref args 'optional)))
87                                             (length values)))))
88           (else '()))))
89
90 (define (arguments-calling proc values)
91   "Return an alist with procedure arguments and their values"
92   (let ((args ((@ (ice-9 session) procedure-arguments) proc)))
93     (append (required-arguments args values)
94             (optional-arguments args values)
95             (keyword-arguments args values)
96             (rest-arguments args values))))
97
98 (define (required-arguments-apply args values)
99   "Return a list with required arguments for use with apply"
100   (define (f vars values)
101     (cond ((null? vars) '())
102           (else 
103            (cons (assoc-ref values (car vars))
104                  (f (cdr vars) values)))))
105   (f (assoc-ref args 'required) values))
106
107 (define (optional-arguments-apply args values)
108   "Return a list with optional arguments for use with apply"
109   (define (f vars values)
110     (cond ((null? vars) '())
111           (else (let ((a (f (cdr vars) values))
112                       (val (assoc (car vars) values)))
113                   (cond ((and val (bound? (cdr val)))
114                          (cons (cdr val) a))
115                         (else a))))))
116   (f (assoc-ref args 'optional) values))
117
118 (define (keyword-arguments-apply args values)
119   "Return a list with keyword arguments for use with apply"
120   (define (f vars values)
121     (cond ((null? vars) '())
122           (else (let ((a (f (cdr vars) values))
123                       (val (assoc (keyword->symbol (car vars)) values)))
124                   (cond ((and val (bound? (cdr val)))
125                          (cons (car vars) (cons (cdr val) a)))
126                         (else a))))))
127   (f (map (lambda (x) (car x)) (assoc-ref args 'keyword)) values))
128
129 (define (rest-arguments-apply args values)
130   "Return a list with rest arguments for use with apply"
131   (let ((rest (assoc-ref args 'rest)))
132     (cond (rest (assoc-ref values rest))
133           (else '()))))
134   
135 (define (arguments-apply proc values)
136   "Return a list for use with apply"
137   (let ((args ((@ (ice-9 session) procedure-arguments) proc)))
138     (append (required-arguments-apply args values)
139             (optional-arguments-apply args values)
140             (keyword-arguments-apply args values)
141             (rest-arguments-apply args values))))