Add Guile as an extension language.
[deliverable/binutils-gdb.git] / gdb / guile / lib / gdb / iterator.scm
1 ;; Iteration utilities.
2 ;; Anything in this file can change or disappear.
3 ;;
4 ;; Copyright (C) 2014 Free Software Foundation, Inc.
5 ;;
6 ;; This file is part of GDB.
7 ;;
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3 of the License, or
11 ;; (at your option) any later version.
12 ;;
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 (define-module (gdb iterator)
22 #:use-module (gdb))
23
24 (define-public (make-list-iterator l)
25 "Return a <gdb:iterator> object for a list."
26 (%assert-type (list? l) l SCM_ARG1 'make-list-iterator)
27 (let ((next! (lambda (iter)
28 (let ((l (iterator-progress iter)))
29 (if (eq? l '())
30 (end-of-iteration)
31 (begin
32 (set-iterator-progress! iter (cdr l))
33 (car l)))))))
34 (make-iterator l l next!)))
35
36 (define-public (iterator->list iter)
37 "Return the elements of ITER as a list."
38 (let loop ((iter iter)
39 (result '()))
40 (let ((next (iterator-next! iter)))
41 (if (end-of-iteration? next)
42 (reverse! result)
43 (loop iter (cons next result))))))
44
45 (define-public (iterator-map proc iter)
46 "Return a list of PROC applied to each element."
47 (let loop ((proc proc)
48 (iter iter)
49 (result '()))
50 (let ((next (iterator-next! iter)))
51 (if (end-of-iteration? next)
52 (reverse! result)
53 (loop proc iter (cons (proc next) result))))))
54
55 (define-public (iterator-for-each proc iter)
56 "Apply PROC to each element. The result is unspecified."
57 (let ((next (iterator-next! iter)))
58 (if (not (end-of-iteration? next))
59 (begin
60 (proc next)
61 (iterator-for-each proc iter)))))
62
63 (define-public (iterator-filter pred iter)
64 "Return the elements that satify predicate PRED."
65 (let loop ((result '()))
66 (let ((next (iterator-next! iter)))
67 (cond ((end-of-iteration? next) (reverse! result))
68 ((pred next) (loop (cons next result)))
69 (else (loop result))))))
70
71 (define-public (iterator-until pred iter)
72 "Run the iterator until the result of (pred element) is true.
73
74 Returns:
75 The result of the first (pred element) call that returns true,
76 or #f if no element matches."
77 (let loop ((next (iterator-next! iter)))
78 (cond ((end-of-iteration? next) #f)
79 ((pred next) => identity)
80 (else (loop (iterator-next! iter))))))
This page took 0.031633 seconds and 4 git commands to generate.