*** empty log message ***
[bpt/guile.git] / oop / goops / util.scm
1 ;;;; Copyright (C) 1999, 2000 Free Software Foundation, Inc.
2 ;;;;
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
7 ;;;;
8 ;;;; This program is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;;;; GNU General Public License for more details.
12 ;;;;
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
17 ;;;;
18 \f
19
20 (define-module (oop goops util)
21 :no-backtrace
22 )
23
24 (export any every filter
25 mapappend find-duplicate top-level-env top-level-env?
26 map* for-each* length* improper->proper
27 )
28
29 ;;;
30 ;;; {Utilities}
31 ;;;
32
33 (define (any pred lst . rest)
34 (if (null? rest) ;fast path
35 (and (not (null? lst))
36 (let loop ((head (car lst)) (tail (cdr lst)))
37 (if (null? tail)
38 (pred head)
39 (or (pred head)
40 (loop (car tail) (cdr tail))))))
41 (let ((lsts (cons lst rest)))
42 (and (not (any null? lsts))
43 (let loop ((heads (map car lsts)) (tails (map cdr lsts)))
44 (if (any null? tails)
45 (apply pred heads)
46 (or (apply pred heads)
47 (loop (map car tails) (map cdr tails)))))))))
48
49 (define (every pred lst . rest)
50 (if (null? rest) ;fast path
51 (or (null? lst)
52 (let loop ((head (car lst)) (tail (cdr lst)))
53 (if (null? tail)
54 (pred head)
55 (and (pred head)
56 (loop (car tail) (cdr tail))))))
57 (let ((lsts (cons lst rest)))
58 (or (any null? lsts)
59 (let loop ((heads (map car lsts)) (tails (map cdr lsts)))
60 (if (any null? tails)
61 (apply pred heads)
62 (and (apply pred heads)
63 (loop (map car tails) (map cdr tails)))))))))
64
65 (define (filter test? list)
66 (cond ((null? list) '())
67 ((test? (car list)) (cons (car list) (filter test? (cdr list))))
68 (else (filter test? (cdr list)))))
69
70 (define (mapappend func . args)
71 (if (memv '() args)
72 '()
73 (append (apply func (map car args))
74 (apply mapappend func (map cdr args)))))
75
76 (define (find-duplicate l) ; find a duplicate in a list; #f otherwise
77 (cond
78 ((null? l) #f)
79 ((memv (car l) (cdr l)) (car l))
80 (else (find-duplicate (cdr l)))))
81
82 (define (top-level-env)
83 (if *top-level-lookup-closure*
84 (list *top-level-lookup-closure*)
85 '()))
86
87 (define (top-level-env? env)
88 (or (null? env)
89 (procedure? (car env))))
90
91 (define (map* fn . l) ; A map which accepts dotted lists (arg lists
92 (cond ; must be "isomorph"
93 ((null? (car l)) '())
94 ((pair? (car l)) (cons (apply fn (map car l))
95 (apply map* fn (map cdr l))))
96 (else (apply fn l))))
97
98 (define (for-each* fn . l) ; A for-each which accepts dotted lists (arg lists
99 (cond ; must be "isomorph"
100 ((null? (car l)) '())
101 ((pair? (car l)) (apply fn (map car l)) (apply for-each* fn (map cdr l)))
102 (else (apply fn l))))
103
104 (define (length* ls)
105 (do ((n 0 (+ 1 n))
106 (ls ls (cdr ls)))
107 ((not (pair? ls)) n)))
108
109 (define (improper->proper ls)
110 (if (pair? ls)
111 (cons (car ls) (improper->proper (cdr ls)))
112 (list ls)))