diff options
Diffstat (limited to 'modules/language/python/list.scm')
-rw-r--r-- | modules/language/python/list.scm | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm index 8f734d8..0b98b61 100644 --- a/modules/language/python/list.scm +++ b/modules/language/python/list.scm @@ -6,7 +6,8 @@ #:use-module (language python for) #:use-module (language python try) #:use-module (language python exceptions) - #:export (to-list pylist-ref pylist-set! pylist-append!)) + #:export (to-list pylist-ref pylist-set! pylist-append! + pylist-slice)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) @@ -46,7 +47,17 @@ (slot-set! o 'vec vec) o)) +(define-method (to-pylist (l <vector>)) + (to-pylist (vector->list l))) +(define-method (to-pylist l) + (if (null? l) + (let ((o (make <py-list>))) + (slot-set! o 'vec (make-vector 4)) + (slot-set! o 'n 0) + o) + (error "not able to make a pylist"))) + ;;; REF (define-method (pylist-ref (o <py-list>) n) (if (< n (slot-ref o 'n)) @@ -77,6 +88,22 @@ (define-method (pylist-set! (o <p>) n val) ((ref o '__listset__) n val)) +;;SLICE +(define-method (pylist-slice (o <py-list>) n1 n2 n3) + (let* ((n1 (if (eq? n1 'None) 0 n1)) + (n2 (if (eq? n2 'None) (slot-ref o 'n) n2)) + (n3 (if (eq? n3 'None) 1 n3)) + + (vec (slot-ref o 'vec)) + (l (let lp ((i n1)) + (if (< i n2) + (cons (vector-ref vec i) (lp (+ i n3))) + '())))) + (to-pylist l))) + +(define-method (pylist-slice o n1 n2 n3) + (pylist-slice (to-pylist o) n1 n2 n3)) + ;;APPEND (define-method (pylist-append! (o <py-list>) val) (let* ((n (slot-ref o 'n)) |