Debian Codegraph @

lib/scheme/lseq.scm

raw
1 ;;;
2 ;;; scheme.lseq - lazy sequences (R7RS Red)
3 ;;;
4 ;;; Copyright (c) 2016-2024 Shiro Kawai <[email protected]>
5 ;;;
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
8 ;;; are met:
9 ;;;
10 ;;; 1. Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
12 ;;;
13 ;;; 2. Redistributions in binary form must reproduce the above copyright
14 ;;; notice, this list of conditions and the following disclaimer in the
15 ;;; documentation and/or other materials provided with the distribution.
16 ;;;
17 ;;; 3. Neither the name of the authors nor the names of its contributors
18 ;;; may be used to endorse or promote products derived from this
19 ;;; software without specific prior written permission.
20 ;;;
21 ;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 ;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 ;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 ;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 ;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 ;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27 ;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 ;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 ;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 ;;;
33
34 ;; This is a compability facade of Gauche's native lseq.
35 ;; Srfi-127's lseq is conceptually a list terminated by a generator:
36 ;;
37 ;; Lseq :: () | (x . Generator) | (x . Lseq)
38 ;;
39 ;; However, generally there's no guarantee that how many elements are
40 ;; realized in a given lseq, so no portable code should count on
41 ;; (cdr lseq) => <generator>.
42 ;;
43 ;; One deviation of SRFI-127 is the constructor generator->lseq: It
44 ;; explicitly specifies to return (x . Generator) of the given generator
45 ;; yields non-EOF object. We return Gauche's built-in lseq, which is
46 ;; operationally equivalent to SRFI-127's lseq but you cannot get
47 ;; the generator itself in the cdr.
48
49 ;; Originall SRFI-127
50 (define-module scheme.lseq
51 (use scheme.list)
52 (use gauche.lazy)
53 (use gauche.generator)
54 (export generator->lseq ; built-in
55 lseq? lseq=?
56 lseq-car lseq-cdr
57 lseq-first lseq-rest lseq-ref
58 lseq-take lseq-drop
59 lseq-realize lseq->generator
60 lseq-length lseq-append lseq-zip
61 lseq-map lseq-for-each
62 lseq-filter lseq-remove
63 lseq-find lseq-find-tail
64 lseq-any lseq-every
65 lseq-index
66 lseq-take-while lseq-drop-while
67 lseq-member lseq-memq lseq-memv))
68 (select-module scheme.lseq)
69
70 (define (lseq? x) (or (null? x) (pair? x)))
71
72 (define (lseq=? elt=? lseq1 lseq2)
73 (list= elt=? lseq1 lseq2))
74
75 (define lseq-car car)
76 (define lseq-first car)
77 (define lseq-cdr cdr)
78 (define lseq-rest cdr)
79 (define lseq-ref list-ref)
80
81 (define (lseq-take lseq k)
82 (if (and (null? lseq) (= k 0))
83 '() ;shortcut
84 (generator->lseq
85 (^[] (if (<= k 0)
86 (eof-object)
87 (rlet1 e (car lseq)
88 (dec! k)
89 (set! lseq (cdr lseq))))))))
90 (define lseq-drop drop)
91 (define (lseq-realize lseq) (and (length lseq) lseq))
92
93 (define lseq->generator list->generator)
94
95 (define lseq-length length)
96 (define lseq-append lappend)
97
98 (define (lseq-zip lseq . lseqs) (apply lmap list lseq lseqs))
99 (define lseq-map lmap)
100 (define lseq-for-each for-each)
101
102 (define lseq-filter lfilter)
103 (define (lseq-remove pred lseq) (lfilter (complement pred) lseq))
104 (define lseq-find find)
105 (define lseq-find-tail find-tail)
106 (define lseq-take-while ltake-while)
107 (define lseq-drop-while drop-while)
108
109 (define lseq-any any)
110 (define lseq-every every)
111 (define lseq-index list-index)
112 (define lseq-member member)
113 (define lseq-memq memq)
114 (define lseq-memv memv)