tuple
[software/python-on-guile.git] / modules / language / python / list.scm
1 (define-module (language python list)
2 #:use-module (ice-9 match)
3 #:use-module (ice-9 control)
4 #:use-module (oop pf-objects)
5 #:use-module (oop goops)
6 #:use-module (language python hash)
7 #:use-module (language python tuple)
8 #:use-module (language python exceptions)
9 #:use-module (language python yield)
10 #:use-module (language python for)
11 #:use-module (language python try)
12 #:use-module (language python exceptions)
13 #:export (to-list to-pylist <py-list>
14 pylist-ref pylist-set! pylist-append!
15 pylist-slice pylist-subset! pylist-reverse!
16 pylist-pop! pylist-count pylist-extend! len in
17 pylist-insert! pylist-remove! pylist-sort!
18 pylist-index pylist-null pylist-delete!
19 pylist pylist-listing
20 py-all py-any))
21
22 (define scm-list list)
23
24 (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
25
26 (define-class <py-list> () vec n)
27
28 (define-method (pylist-delete! (o <py-list>) k)
29 (let* ((n (slot-ref o 'n))
30 (k (if (< k 0) (+ k n) k)))
31 (pylist-subset! o k (+ k 1) None pylist-null)))
32
33 (define-method (pylist-delete! (o <p>) k)
34 (aif it (ref o '__delitem__)
35 (it k)
36 (next-method)))
37
38 (define pylist-null
39 (let ((o (make <py-list>)))
40 (slot-set! o 'vec (make-vector 0))
41 (slot-set! o 'n 0)
42 o))
43
44
45 (define-method (py-hash (o <py-list>))
46 (let ((n (min complexity (slot-ref o 'n)))
47 (v (slot-ref o 'vec)))
48 (let lp ((i 0) (s 0))
49 (if (< i n)
50 (lp (+ i 1)
51 (xy (py-hash (vector-ref v i)) s))
52 s))))
53
54 (define-method (to-list x)
55 (if (vector? x)
56 (vector->list x)
57 x))
58
59 (define-method (to-list (x <p>))
60 (aif it (ref x '__tolist__)
61 (it)
62 (next-method)))
63
64
65 (defpair (to-list x) x)
66
67 (define-method (to-list (x <yield>))
68 (define l '())
69 (catch StopIteration
70 (lambda ()
71 (let lp ()
72 (set! l (cons (next x) l))
73 (lp)))
74 (lambda x
75 (reverse l))))
76
77 (define-method (to-list (x <py-list>))
78 (let ((vec (slot-ref x 'vec))
79 (n (slot-ref x 'n)))
80 (let lp ((i 0))
81 (if (< i n)
82 (cons (vector-ref vec i) (lp (+ i 1)))
83 '()))))
84
85 (define-method (to-pylist (l <py-list>))
86 l)
87
88 (defpair (to-pylist l)
89 (let* ((n (length l))
90 (vec (make-vector (* 2 n)))
91 (o (make <py-list>)))
92
93 (let lp ((l l) (i 0))
94 (if (pair? l)
95 (begin
96 (vector-set! vec i (car l))
97 (lp (cdr l) (+ i 1)))))
98
99 (slot-set! o 'n n)
100 (slot-set! o 'vec vec)
101 o))
102
103 (define-method (to-pylist (l <vector>))
104 (to-pylist (vector->list l)))
105
106 (define-method (to-pylist (o <string>))
107 (to-pylist (string->list o)))
108
109 (define-method (to-pylist l)
110 (if (null? l)
111 (let ((o (make <py-list>)))
112 (slot-set! o 'vec (make-vector 4))
113 (slot-set! o 'n 0)
114 o)
115 (error "not able to make a pylist")))
116
117 ;;; REF
118 (define-method (pylist-ref (o <py-list>) nin)
119 (define N (slot-ref o 'n))
120 (define n (if (< nin 0) (+ N nin) nin))
121 (if (and (>= n 0) (< n (slot-ref o 'n)))
122 (vector-ref (slot-ref o 'vec) n)
123 (raise IndexError)))
124
125 (defpair (pylist-ref o n)
126 (list-ref o (if (< n 0) (+ (length o) n) n)))
127
128 (define-method (pylist-ref (o <vector>) n)
129 (vector-ref o n))
130
131 (define-method (pylist-ref (o <p>) n)
132 (aif it (ref o '__getitem__)
133 (it n)
134 (next-method)))
135
136 ;;; SET
137 (define-method (pylist-set! (o <py-list>) nin val)
138 (define N (slot-ref o 'n))
139 (define n (if (< nin 0) (+ N nin) nin))
140
141 (if (and (>= n 0) (< n (slot-ref o 'n)))
142 (vector-set! (slot-ref o 'vec) n val)
143 (raise IndexError)))
144
145 (defpair (pylist-set! o n val)
146 (list-set! o n val))
147
148 (defpair (pylist-set! o n val)
149 (vector-set! o n val))
150
151 (define-method (pylist-set! (o <p>) n val)
152 (aif it (ref o '__setitem__)
153 (it n val)
154 (next-method)))
155
156 ;;SLICE
157 (define-method (pylist-slice (o <p>) n1 n2 n3)
158 (aif it (ref o '__getslice__)
159 (it n1 n2 n3)
160 (next-method)))
161
162 (define-method (pylist-slice (o <py-list>) n1 n2 n3)
163 (define N (slot-ref o 'n))
164 (define (f n) (if (< n 0) (+ N n) n))
165
166 (let* ((n1 (f (if (eq? n1 None) 0 n1)))
167 (n2 (f (if (eq? n2 None) (slot-ref o 'n) n2)))
168 (n3 (f (if (eq? n3 None) 1 n3)))
169
170 (vec (slot-ref o 'vec))
171 (l (let lp ((i n1))
172 (if (< i n2)
173 (cons (vector-ref vec i) (lp (+ i n3)))
174 '()))))
175 (to-pylist l)))
176
177 (define-method (pylist-slice (o <string>) n1 n2 n3)
178 (list->string
179 (map (lambda (x) (string-ref x 0))
180 (to-list
181 (pylist-slice (to-pylist o) n1 n2 n3)))))
182
183 (defpair (pylist-slice o n1 n2 n3)
184 (to-list
185 (pylist-slice (to-pylist o) n1 n2 n3)))
186
187 (define-method (pylist-slice (o <vector>) n1 n2 n3)
188 (list->vector
189 (to-list
190 (pylist-slice (to-pylist o) n1 n2 n3))))
191
192 ;;SUBSET
193 (define-method (pylist-subset! (o <p>) n1 n2 n3 val)
194 (aif it (ref o '__setslice__)
195 (it n1 n2 n3 val)
196 (next-method)))
197
198 (define-method (pylist-subset! (o <py-list>) n1 n2 n3 val)
199 (define N (slot-ref o 'n))
200 (define (f n) (if (< n 0) (+ N n) n))
201
202 (let* ((n1 (f (if (eq? n1 None) 0 n1)))
203 (n2 (f (if (eq? n2 None) (slot-ref o 'n) n2)))
204 (n3 (f (if (eq? n3 None) 1 n3)))
205 (vec (slot-ref o 'vec))
206 (o2 (to-pylist val))
207 (N2 (slot-ref o2 'n))
208 (vec2 (slot-ref o2 'vec)))
209 (if (<= n2 N)
210 (let lp ((i 0) (j n1))
211 (if (< j n2)
212 (if (< i N2)
213 (begin
214 (vector-set! vec j (vector-ref vec2 i))
215 (lp (+ i 1) (+ j n3)))
216 (let lp ((j2 j))
217 (if (< j2 n2)
218 (lp (+ j2 n3))
219 (let lp ((k1 j) (k2 j2))
220 (if (< k2 N)
221 (begin
222 (vector-set! vec k1 (vector-ref vec k2))
223 (lp (+ k1 1) (+ k2 1)))
224 (begin
225 (let lp ((i k2))
226 (if (< i N)
227 (begin
228 (vector-set! vec i #f)
229 (lp (+ i 1)))
230 (slot-set! o 'n k1)))))))))))
231 (raise IndexError))
232 (values)))
233
234
235 ;;APPEND
236 (define-method (pylist-append! (o <py-list>) val)
237 (let* ((n (slot-ref o 'n))
238 (vec (slot-ref o 'vec))
239 (N (vector-length vec)))
240 (if (< n N)
241 (begin
242 (vector-set! vec n val)
243 (slot-set! o 'n (+ n 1)))
244 (let* ((N (* 2 N))
245 (vec2 (make-vector N)))
246 (let lp ((i 0))
247 (if (< i n)
248 (begin
249 (vector-set! vec2 i (vector-ref vec i))
250 (lp (+ i 1)))))
251 (vector-set! vec2 n val)
252 (slot-set! o 'vec vec2)))
253 (slot-set! o 'n (+ n 1))
254 (values)))
255
256 (define-method (pylist-append! o n)
257 (raise 'NotSupportedOP '__append__))
258
259 (define-method (pylist-append! (o <p>) n . l)
260 (aif it (ref o 'append)
261 (apply it n l)
262 (next-method)))
263
264
265
266 (define-method (write (o <py-list>) . l)
267 (define port (if (null? l) #t (car l)))
268
269 (let* ((l (to-list o)))
270 (if (null? l)
271 (format port "[]")
272 (format port "[~a~{, ~a~}]" (car l) (cdr l)))))
273
274 (define-method (display (o <py-list>) . l)
275 (define port (if (null? l) #t (car l)))
276
277 (let* ((l (to-list o)))
278 (if (null? l)
279 (format port "[]")
280 (format port "[~a~{, ~a~}]" (car l) (cdr l)))))
281
282
283 (define-method (+ (o1 <py-list>) (o2 <py-list>))
284 (let* ((vec1 (slot-ref o1 'vec))
285 (vec2 (slot-ref o2 'vec))
286 (n1 (slot-ref o1 'n))
287 (n2 (slot-ref o2 'n))
288 (n (+ n1 n2))
289 (vec (make-vector (* 2 n)))
290 (o (make <py-list>)))
291
292 (let lp ((i1 0))
293 (if (< i1 n1)
294 (begin
295 (vector-set! vec i1 (vector-ref vec1 i1))
296 (lp (+ i1 1)))
297 (let lp ((i2 0) (i i1))
298 (if (< i2 n2)
299 (begin
300 (vector-set! vec i (vector-ref vec2 i2))
301 (lp (+ i2 1) (+ i 1)))))))
302
303 (slot-set! o 'n n )
304 (slot-set! o 'vec vec)
305 o))
306
307
308 (define-method (+ (o1 <pair>) (o2 <pair>))
309 (append o1 o2))
310
311 (define-method (+ (o1 <py-tuple>) o2)
312 (+ (slot-ref o1 'l) o2))
313
314 (define-method (+ o2 (o1 <py-tuple>))
315 (+ o2 (slot-ref o1 'l)))
316
317 (define-method (+ (o1 <string>) (o2 <string>))
318 (string-append o1 o2))
319
320 (define-method (+ (o1 <symbol>) (o2 <symbol>))
321 (string->symbol
322 (string-append
323 (symbol->string o1)
324 (symbol->string o2))))
325
326 (define-method (* (x <integer>) (o1 <py-list>)) (* o1 x))
327 (define-method (* (o1 <py-list>) (x <integer>))
328 (let* ((vec (slot-ref o1 'vec))
329 (n (slot-ref o1 'n))
330 (n2 (* n x))
331 (vec2 (make-vector (* 2 n2)))
332 (o (make <py-list>)))
333
334 (let lp1 ((i 0) (j 0))
335 (if (< i x)
336 (let lp2 ((j j) (k 0))
337 (if (< k n)
338 (begin
339 (vector-set! vec2 j (vector-ref vec k))
340 (lp2 (+ j 1) (+ k 1)))
341 (lp1 (+ i 1) j)))))
342
343 (slot-set! o 'n n2 )
344 (slot-set! o 'vec vec2)
345 o))
346
347 (define-method (* (x <integer>) (vec <string>)) (* vec x))
348 (define-method (* (vec <string>) (x <integer>))
349 (let* ((n (string-length vec))
350 (n2 (* n x))
351 (vec2 (make-string n2)))
352
353 (let lp1 ((i 0) (j 0))
354 (if (< i x)
355 (let lp2 ((j j) (k 0))
356 (if (< k n)
357 (begin
358 (string-set! vec2 j (string-ref vec k))
359 (lp2 (+ j 1) (+ k 1)))
360 (lp1 (+ i 1) j)))))
361 vec2))
362
363 (define-method (* (x <integer> ) (l <pair>)) (* l x))
364 (define-method (* (x <py-tuple>) l) (* (slot-ref x 'l) l))
365 (define-method (* l (x <py-tuple>)) (* l (slot-ref x 'l)))
366 (define-method (* (l <pair>) (x <integer>))
367 (let lp1 ((i 0))
368 (if (< i x)
369 (let lp2 ((k l))
370 (if (pair? k)
371 (cons (car k) (lp2 (cdr k)))
372 (lp1 (+ i 1))))
373 '())))
374
375
376 ;;REVERSE
377 (define-method (pylist-reverse! (o <py-list>))
378 (let* ((N (slot-ref o 'n))
379 (M (- N 1))
380 (n (floor-quotient N 2))
381 (vec (slot-ref o 'vec)))
382 (let lp ((i 0))
383 (if (< i n)
384 (let ((swap (vector-ref vec i))
385 (k (- M i)))
386 (vector-set! vec i (vector-ref vec k))
387 (vector-set! vec k swap))))))
388
389
390 (define-method (pylist-reverse! (o <p>) . l)
391 (aif it (ref o 'reverse)
392 (apply it l)
393 (next-method)))
394
395 ;;POP!
396 (define-method (pylist-pop! (o <py-list>))
397 (let* ((n (slot-ref o 'n))
398 (m (- n 1))
399 (vec (slot-ref o 'vec)))
400 (if (> n 0)
401 (let ((ret (vector-ref vec m)))
402 (slot-set! o 'n m)
403 (vector-set! vec m #f)
404 ret)
405 (raise IndexError "pop from empty list"))))
406
407 (define-method (pylist-pop! (o <p>) . l)
408 (aif it (ref o 'pop)
409 (apply it l)
410 (next-method)))
411
412 ;;COUNT
413 (define-method (pylist-count (o <py-list>) q)
414 (let* ((n (slot-ref o 'n))
415 (vec (slot-ref o 'vec)))
416 (let lp ((i 0) (sum 0))
417 (if (< i n)
418 (if (equal? (vector-ref vec i) q)
419 (lp (+ i 1) (+ sum 1))
420 (lp (+ i 1) sum ))
421 sum))))
422
423 (define-method (pylist-count (s <string>) q)
424 (let* ((n (string-length s))
425 (q (if (and (string? q) (= (string-length q) 1))
426 (string-ref q 0))))
427 (let lp ((i 0) (sum 0))
428 (if (< i n)
429 (if (eq? (string-ref s i) q)
430 (lp (+ i 1) (+ sum 1))
431 (lp (+ i 1) sum ))
432 sum))))
433
434 (defpair (pylist-count l q)
435 (let lp ((l l) (sum 0))
436 (if (pair? l)
437 (if (eq? (car l) q)
438 (lp (cdr l) (+ sum 1))
439 (lp (cdr l) sum ))
440 sum)))
441
442 (define-method (pylist-count (o <p>) . l)
443 (aif it (ref o 'count)
444 (apply it l)
445 (next-method)))
446
447 ;; extend!
448 (define-method (pylist-extend! (o <py-list>) iter)
449 (for ((x : iter)) ()
450 (pylist-append! o x)))
451
452 (define-method (pylist-extend! (o <p>) . l)
453 (aif it (ref o 'extend)
454 (apply it l)
455 (next-method)))
456
457 ;; equal?
458 (define-method (equal? (o1 <py-list>) (o2 <py-list>))
459 (let ((n1 (slot-ref o1 'n))
460 (n2 (slot-ref o2 'n))
461 (vec1 (slot-ref o1 'vec))
462 (vec2 (slot-ref o2 'vec)))
463 (and
464 (equal? n1 n2)
465 (let lp ((i 0))
466 (if (< i n1)
467 (and (equal? (vector-ref vec1 i) (vector-ref vec2 i))
468 (lp (+ i 1)))
469 #t)))))
470
471 (define-method (equal? (o1 <py-list>) o2) #f)
472 (define-method (equal? o1 (o2 <py-list>)) #f)
473
474
475 (define-class <py-list-iter> (<py-list>) i)
476 (define-class <string-iter> () str i)
477
478 ;;WRAP-IN
479 (define-method (wrap-in (s <string>))
480 (let ((out (make <string-iter>)))
481 (slot-set! out 'str s)
482 (slot-set! out 'i 0)
483 out))
484
485 (define-method (wrap-in (o <py-list>))
486 (let ((out (make <py-list-iter>)))
487 (slot-set! out 'n (slot-ref o 'n ))
488 (slot-set! out 'vec (slot-ref o 'vec))
489 (slot-set! out 'i 0)
490 out))
491
492 (define-method (wrap-in (o <py-list-iter>)) o)
493 (define-method (wrap-in (o <string-iter> )) o)
494 ;;NEXT
495 (define-method (next (o <string-iter>))
496 (let ((i (slot-ref o 'i ))
497 (str (slot-ref o 'str)))
498 (if (< i (string-length str))
499 (let ((ret (string-ref str i)))
500 (slot-set! o 'i (+ i 1))
501 (list->string (scm-list ret)))
502 (throw StopIteration))))
503
504 (define-method (next (o <py-list-iter>))
505 (let ((i (slot-ref o 'i ))
506 (n (slot-ref o 'n ))
507 (vec (slot-ref o 'vec)))
508 (if (< i n)
509 (let ((ret (vector-ref vec i)))
510 (slot-set! o 'i (+ i 1))
511 ret)
512 (throw StopIteration))))
513
514 ;;INSERT
515 (define-method (pylist-insert! (o <py-list>) i val)
516 (let* ((vec (slot-ref o 'vec))
517 (n (slot-ref o 'n))
518 (i (if (< i 0) (+ n i) i)))
519 (if (and (>= i 0) (<= i n))
520 (let lp ((v val) (i i))
521 (if (< i n)
522 (let ((swap (vector-ref vec i)))
523 (vector-set! vec i v)
524 (lp swap (+ i 1)))
525 (pylist-append! o v)))
526 (raise IndexError "Wrong index in insert"))))
527
528 (define-method (pylist-insert! (o <p>) . l)
529 (aif it (ref o 'insert)
530 (apply it l)
531 (next-method)))
532
533
534 ;;REMOVE
535 (define-method (pylist-remove! (o <py-list>) val)
536 (let ((n (slot-ref o 'n ))
537 (vec (slot-ref o 'vec)))
538 (let lp ((i 0))
539 (if (< i n)
540 (let ((r (vector-ref vec i)))
541 (if (equal? r val)
542 (pylist-subset! o i (+ i 1) 1 '())
543 (lp (+ i 1))))
544 (raise ValueError "list removal has no element to remove")))))
545
546 (define-method (pylist-remove! (o <p>) . l)
547 (aif it (ref o 'remove)
548 (apply it l)
549 (next-method)))
550
551 ;; SORT!
552 (define (id x) id)
553 (define-method (pylist-sort! (o <py-list>) . l)
554 (apply
555 (lambda* (#:key (key id) (reverse #f))
556 (let lp ((l (sort (map key (to-list o)) (if reverse > <))) (i 0))
557 (if (pair? l)
558 (begin
559 (pylist-set! o i (car l))
560 (lp (cdr l) (+ i 1))))))
561 l))
562
563 (define-method (pylist-sort! (o <p>) . l)
564 (aif it (ref o 'sort)
565 (apply it l)
566 (next-method)))
567
568 ;; INDEX
569 (define-method (pylist-index (o <py-list>) val . l)
570 (let* ((n (slot-ref o 'n ))
571 (vec (slot-ref o 'vec))
572 (f (lambda (m) (if (< m 0) (+ m n) m))))
573 (call-with-values
574 (lambda ()
575 (match l
576 (()
577 (values 0 n))
578 ((x)
579 (values (f x) n))
580 ((x y)
581 (values (f x) (f y)))))
582 (lambda (n1 n2)
583 (if (and (>= n1 0) (>= n2 0) (< n1 n) (<= n2 n))
584 (let lp ((i n1))
585 (if (< i n2)
586 (let ((r (vector-ref vec i)))
587 (if (equal? r val)
588 i
589 (lp (+ i 1))))
590 (raise ValueError "could not find value in index fkn")))
591 (raise IndexError "index out of scop in index fkn"))))))
592
593 (define-method (pylist-index (o <string>) val . l)
594 (let* ((n (string-length o))
595 (f (lambda (m) (if (< m 0) (+ m n) m)))
596 (val (if (and (string? val) (> (string-length val) 0))
597 (string-ref val 0)
598 val)))
599 (call-with-values
600 (lambda ()
601 (match l
602 (()
603 (values 0 n))
604 ((x)
605 (values (f x) n))
606 ((x y)
607 (values (f x) (f y)))))
608 (lambda (n1 n2)
609 (if (and (>= n1 0) (>= n2 0) (< n1 n) (<= n2 n))
610 (let lp ((i n1))
611 (if (< i n2)
612 (let ((r (string-ref o i)))
613 (if (equal? r val)
614 i
615 (lp (+ i 1))))
616 (raise ValueError "could not find value in index fkn")))
617 (raise IndexError "index out of scop in index fkn"))))))
618
619 (defpair (pylist-index o val . l)
620 (let* ((n (length o))
621 (f (lambda (m) (if (< m 0) (+ m n) m))))
622 (call-with-values
623 (lambda ()
624 (match l
625 (()
626 (values 0 n))
627 ((x)
628 (values (f x) n))
629 ((x y)
630 (values (f x) (f y)))))
631 (lambda (n1 n2)
632 (if (and (>= n1 0) (>= n2 0) (< n1 n) (<= n2 n))
633 (let lp ((i o))
634 (if (pair? i)
635 (let ((r (car i)))
636 (if (equal? r val)
637 i
638 (lp (cdr i))))
639 (raise ValueError "could not find value in index fkn")))
640 (raise IndexError "index out of scop in index fkn"))))))
641
642 (define-method (pylist-index (o <p>) . l)
643 (aif it (ref o 'index)
644 (apply it l)
645 (next-method)))
646
647
648 #:len
649
650 (defpair (len l) (length l))
651 (define-method (len (v <vector>)) (vector-length v))
652 (define-method (len (s <string>)) (string-length s))
653 (define-method (len (o <py-list>)) (slot-ref o 'n))
654 (define-method (len (o <p>))
655 (aif it (ref o '__len__)
656 (it)
657 (next-method)))
658
659 (define-method (in x (l <py-tuple>)) (member x (slot-ref l 'l)))
660 (define-method (in x (l <pair>)) (member x l))
661 (define-method (in x (l <vector>))
662 (define n (vector-length l))
663 (let lp ((i 0))
664 (if (< i n)
665 (if (equal? x (vector-ref l i))
666 #t
667 (lp (+ i 1)))
668 #f)))
669
670 (define-method (in (x <string>) (s <string>))
671 (string-contains s x))
672
673 (define-method (in (x <char>) (s <string>))
674 (let/ec ret
675 (string-for-each
676 (lambda (ch)
677 (if (eq? ch x)
678 (ret #t)))
679 s))
680 #f)
681
682 (define-method (in x (o <py-list>))
683 (define l (slot-ref o 'vec))
684 (define n (slot-ref o 'n))
685 (let lp ((i 0))
686 (if (< i n)
687 (if (equal? x (vector-ref l i))
688 #t
689 (lp (+ i 1)))
690 #f)))
691
692 (define-method (in x (o <p>))
693 (aif it (ref o '__contains__)
694 (it x)
695 (next-method)))
696
697 (define-syntax-rule (defgen (op r s o1 o2) code ...)
698 (begin
699 (define-method (op (o1 <py-list>) (o2 <py-list>)) code ...)
700 (define-method (op (o1 <pair>) (o2 <pair> )) code ...)
701 (define-method (op (o1 <py-tuple>) o2)
702 (op (slot-ref o1 'l) o2))
703 (define-method (op o2 (o1 <py-tuple>))
704 (op o2 (slot-ref o1 'l)))
705 (define-method (op (o1 <vector>) (o2 <vector>)) code ...)
706 (define-method (op (o1 <p>) o2)
707 (aif it (ref o1 'r)
708 (it o2)
709 (next-method)))
710 (define-method (op o1 (o2 <p>))
711 (aif it (ref o2 's)
712 (it o1)
713 (next-method)))))
714
715 (defgen (< __le__ __gt__ o1 o2)
716 (let ((n1 (len o1))
717 (n2 (len o2)))
718 (for ((x1 : o1) (x2 : o2)) ()
719 (if (< x1 x2)
720 (break #t))
721 #:final
722 (< n1 n2))))
723
724 (defgen (<= __lt__ __ge__ o1 o2)
725 (let ((n1 (len o1))
726 (n2 (len o2)))
727 (for ((x1 : o1) (x2 : o2)) ()
728 (if (< x1 x2)
729 (break #t))
730 #:final
731 (<= n1 n2))))
732
733 (defgen (> __ge__ __lt__ o1 o2)
734 (let ((n1 (len o1))
735 (n2 (len o2)))
736 (for ((x1 : o1) (x2 : o2)) ()
737 (if (> x1 x2)
738 (break #t))
739 #:final
740 (> n1 n2))))
741
742 (defgen (>= __gt__ __le__ o1 o2)
743 (let ((n1 (len o1))
744 (n2 (len o2)))
745 (for ((x1 : o1) (x2 : o2)) ()
746 (if (> x1 x2)
747 (break #t))
748 #:final
749 (>= n1 n2))))
750
751 (define-python-class list (<py-list>)
752 (define __init__
753 (letrec ((__init__
754 (case-lambda
755 ((self)
756 (slot-set! self 'vec (make-vector 30))
757 (slot-set! self 'n 0))
758 ((self it)
759 (__init__ self)
760 (for ((i : it)) () (pylist-append! self i))))))
761 __init__)))
762
763 (define pylist list)
764
765
766 (define-method (py-class (o <py-list>) list))
767
768 (define (pylist-listing)
769 (let ((l
770 (to-pylist
771 (map symbol->string
772 '(append
773 count
774 extend
775 index
776 pop
777 insert
778 remove
779 reverse
780 sort
781 __init__
782 __le__
783 __lt__
784 __gt__
785 __ge__
786 __ne__
787 __eq__
788 __len__
789 __init__
790 __add__
791 __mul__
792 __rmul__
793 __radd__
794 __repr__
795 __contains__
796 __getattr__
797 __setattr__
798 __delattr__
799 __delitem__
800 __setitem__
801 __iter__
802 )))))
803
804 (pylist-sort! l)
805 l))
806
807 (define (py-all x)
808 (for ((i : x)) ()
809 (if (not i)
810 (break #f))
811 #:final
812 #t))
813
814 (define (py-any x)
815 (for ((i : x)) ()
816 (if i
817 (break #t))
818 #:final
819 #f))
820
821
822