1;;;
2;;; srfi-127 - lazy sequences
3;;;
4;;;   Copyright (c) 2016-2020  Shiro Kawai  <shiro@acm.org>
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(define-module srfi-127
50  (use srfi-1)
51  (use gauche.lazy)
52  (use gauche.generator)
53  (export generator->lseq  ; built-in
54          lseq?        lseq=?
55          lseq-car     lseq-cdr
56          lseq-first   lseq-rest lseq-ref
57          lseq-take    lseq-drop
58          lseq-realize lseq->generator
59          lseq-length  lseq-append  lseq-zip
60          lseq-map     lseq-for-each
61          lseq-filter  lseq-remove
62          lseq-find    lseq-find-tail
63          lseq-any     lseq-every
64          lseq-index
65          lseq-take-while lseq-drop-while
66          lseq-member  lseq-memq     lseq-memv))
67(select-module srfi-127)
68
69(define (lseq? x) (or (null? x) (pair? x)))
70
71(define (lseq=? elt=? lseq1 lseq2)
72  (list= elt=? lseq1 lseq2))
73
74(define lseq-car car)
75(define lseq-first car)
76(define lseq-cdr cdr)
77(define lseq-rest cdr)
78(define lseq-ref list-ref)
79
80(define (lseq-take lseq k)
81  (if (and (null? lseq) (= k 0))
82    '()                                 ;shortcut
83    (generator->lseq
84     (^[] (if (<= k 0)
85            (eof-object)
86            (rlet1 e (car lseq)
87              (dec! k)
88              (set! lseq (cdr lseq))))))))
89(define lseq-drop drop)
90(define (lseq-realize lseq) (and (length lseq) lseq))
91
92(define lseq->generator list->generator)
93
94(define lseq-length length)
95(define lseq-append lappend)
96
97(define (lseq-zip lseq . lseqs) (apply lmap list lseq lseqs))
98(define lseq-map lmap)
99(define lseq-for-each for-each)
100
101(define lseq-filter lfilter)
102(define (lseq-remove pred lseq) (lfilter (complement pred) lseq))
103(define lseq-find find)
104(define lseq-find-tail find-tail)
105(define lseq-take-while ltake-while)
106(define lseq-drop-while drop-while)
107
108(define lseq-any any)
109(define lseq-every every)
110(define lseq-index list-index)
111(define lseq-member member)
112(define lseq-memq memq)
113(define lseq-memv memv)
114
115
116
117
118