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