]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/guile/lib/gdb/iterator.scm
ee9e26ae00b89c85f1dd708b037ffad9d9b0c309
[thirdparty/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-2016 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 #:use-module (gdb support))
24
25 (define-public (make-list-iterator l)
26 "Return a <gdb:iterator> object for a list."
27 (assert-type (list? l) l SCM_ARG1 'make-list-iterator "list")
28 (let ((next! (lambda (iter)
29 (let ((l (iterator-progress iter)))
30 (if (eq? l '())
31 (end-of-iteration)
32 (begin
33 (set-iterator-progress! iter (cdr l))
34 (car l)))))))
35 (make-iterator l l next!)))
36
37 (define-public (iterator->list iter)
38 "Return the elements of ITER as a list."
39 (let loop ((iter iter)
40 (result '()))
41 (let ((next (iterator-next! iter)))
42 (if (end-of-iteration? next)
43 (reverse! result)
44 (loop iter (cons next result))))))
45
46 (define-public (iterator-map proc iter)
47 "Return a list of PROC applied to each element."
48 (let loop ((proc proc)
49 (iter iter)
50 (result '()))
51 (let ((next (iterator-next! iter)))
52 (if (end-of-iteration? next)
53 (reverse! result)
54 (loop proc iter (cons (proc next) result))))))
55
56 (define-public (iterator-for-each proc iter)
57 "Apply PROC to each element. The result is unspecified."
58 (let ((next (iterator-next! iter)))
59 (if (not (end-of-iteration? next))
60 (begin
61 (proc next)
62 (iterator-for-each proc iter)))))
63
64 (define-public (iterator-filter pred iter)
65 "Return the elements that satify predicate PRED."
66 (let loop ((result '()))
67 (let ((next (iterator-next! iter)))
68 (cond ((end-of-iteration? next) (reverse! result))
69 ((pred next) (loop (cons next result)))
70 (else (loop result))))))
71
72 (define-public (iterator-until pred iter)
73 "Run the iterator until the result of (pred element) is true.
74
75 Returns:
76 The result of the first (pred element) call that returns true,
77 or #f if no element matches."
78 (let loop ((next (iterator-next! iter)))
79 (cond ((end-of-iteration? next) #f)
80 ((pred next) => identity)
81 (else (loop (iterator-next! iter))))))