blob: 8cc2741008efd820f078668c27e41be24f8d598d (
about) (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
(define-module (language python range)
#:use-module (oop pf-objects)
#:use-module (language python exceptions)
#:use-module (language python number)
#:use-module (language python list)
#:use-module (language python yield)
#:use-module (language python try)
#:use-module (language python persist)
#:export (range))
(define-python-class range ()
(define __init__
(case-lambda
((self n)
(let ((n (py-index n)))
(set self '_a 0)
(set self '_b (max 0 n))
(set self '_c 1)))
((self n m)
(let ((n (py-index n))
(m (py-index m)))
(set self '_a n)
(set self '_b (max m n))
(set self '_c 1)))
((self n m k)
(let ((n (py-index n))
(m (py-index m))
(k (py-index k)))
(cond
((= k 0)
(raise TypeError "range does not allow 0 as a step"))
((> k 0)
(set self '_a n)
(set self '_b (if (< m n) n m))
(set self '_c k))
((< k 0)
(set self '_a n)
(set self '_b (if (> m n) n m))
(set self '_c k)))))))
(define __iter__
(lambda (self)
((make-generator ()
(lambda (yield)
(let* ((a (ref self '_a))
(b (ref self '_b))
(c (ref self '_c))
(aa (if (> c 0) a a))
(op (if (> c 0) < >)))
(let lp ((i aa))
(if (op i b)
(begin
(yield i)
(lp (+ i c)))))))))))
(define __reversed__
(lambda (self)
(__getslice__ self None None -1)))
(define __repr__
(lambda (self)
(format #f "range(~a,~a,~a)"
(ref self '_a)
(ref self '_b)
(ref self '_c))))
(define __contains__
(lambda (self x)
(let ((x (py-index x ))
(a (ref self '_a))
(b (ref self '_b))
(c (ref self '_c)))
(if (> c 0)
(and
(>= x a)
(< x b)
(= (modulo (- x a) c) 0))
(and
(<= x a)
(> x b)
(= (modulo (- x a) c) 0))))))
(define __getitem__
(lambda (self x)
(let* ((x (py-index x))
(a (ref self '_a))
(b (ref self '_b))
(c (ref self '_c))
(m (+ a (* c x))))
(if (> c 0)
(if (and (>= x 0)
(< m b))
m
(raise IndexError "getitem out of range"))
(if (and (<= x 0)
(> m b))
m
(raise IndexError "getitem out of range"))))))
(define __min__
(lambda (self)
(let* ((a (ref self '_a))
(b (ref self '_b))
(c (ref self '_c))
(n (abs (py-floordiv (- a b) c))))
(if (> c 0)
a
(+ a (* c (- n 1)))))))
(define __max__
(lambda (self)
(let* ((a (ref self '_a))
(b (ref self '_b))
(c (ref self '_c))
(n (abs (py-floordiv (- a b) c))))
(if (> c 0)
(+ a (* c (- n 1)))
n))))
(define __len__
(lambda (self)
(let* ((a (ref self '_a))
(b (ref self '_b))
(c (ref self '_c)))
(abs (py-floordiv (- a b) c)))))
(define __getslice__
(lambda (self x y z)
(let* ((a (ref self '_a))
(b (ref self '_b))
(c (ref self '_c))
(x (if (eq? x None) None (py-index x)))
(y (if (eq? y None) None (py-index y)))
(z (if (eq? z None) None (py-index z)))
(n (abs (py-floordiv (- a b) c))))
(if (or (eq? z None) (> (* z c) 0))
(begin
(if (eq? z 'None) (set! z 1))
(if (eq? y 'None) (set! y n))
(if (eq? x 'None) (set! x 0))
(if (< x 0) (set! x 0))
(if (> x n) (set! x n))
(if (< y 0) (set! y 0))
(if (> y n) (set! y n))
(let* ((cc (* c z))
(xx (+ a (* c x)))
(yy (+ a (* c y)))
(aa (min xx yy))
(bb (max xx yy)))
(range aa bb cc)))
(begin
(if (eq? y 'None) (set! y 0))
(if (eq? x 'None) (set! x n))
(if (< x 0) (set! x 0))
(if (> x n) (set! x n))
(if (< y 0) (set! y 0))
(if (> y n) (set! y n))
(let* ((cc (* c z))
(xx (+ a (* c x)))
(yy (+ a (* c y)))
(aa (max xx yy))
(bb (min xx yy)))
(range aa bb cc)))))))
(define __index__
(case-lambda
((self x)
(let ((x (py-index x ))
(a (ref self '_a))
(b (ref self '_b))
(c (ref self '_c)))
(if (> c 0)
(if (and
(>= x a)
(< x b)
(= (modulo (- x a) c) 0))
(py-floordiv (- x a) c)
(raise IndexError))
(if (and
(<= x a)
(> x b)
(= (modulo (- x a) c) 0))
(py-floordiv (- x a) c)
(raise IndexError)))))
((self x i)
(py-index (pylist-slice None i 1) x))
((self x i j)
(py-index (pylist-slice i j 1) x))))
(define __count__
(lambda (self i)
(if (__contains__ self i)
1
0))))
(name-object range)
|