summaryrefslogtreecommitdiff
path: root/modules/language/python/list.scm
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-09 23:10:27 +0200
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2017-10-09 23:10:27 +0200
commitce108e22767232250682c69bf6275f50bffa8232 (patch)
treebe7fa8696524d23316df435ec4566d915fbff614 /modules/language/python/list.scm
parente4938284cef9e4048528839d29e5930b0e1928f4 (diff)
iterators refactorings
Diffstat (limited to 'modules/language/python/list.scm')
-rw-r--r--modules/language/python/list.scm81
1 files changed, 43 insertions, 38 deletions
diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm
index b0111c1..e9c301c 100644
--- a/modules/language/python/list.scm
+++ b/modules/language/python/list.scm
@@ -265,7 +265,6 @@
(define-method (write (o <py-list>) . l)
(define port (if (null? l) #t (car l)))
-
(let* ((l (to-list o)))
(if (null? l)
(format port "[]")
@@ -474,16 +473,29 @@
(define-class <py-seq-iter> () o i n d)
(define-class <py-list-iter> (<py-list>) i d)
-(define-class <string-iter> () str i d)
-;;WRAP-IN
-(define-method (wrap-in (s <string>))
- (let ((out (make <string-iter>)))
- (slot-set! out 'str s)
- (slot-set! out 'i 0)
- (slot-set! out 'd 1)
- out))
+(define-method (write (o <py-list-iter>) . l)
+ (define port (if (null? l) #t (car l)))
+ (for ((x : o)) ((l '()))
+ (cons x l)
+ #:final
+ (let ((l (reverse l)))
+ (if (null? l)
+ (format port "iter[]")
+ (format port "iter[~a~{, ~a~}]" (car l) (cdr l))))))
+(define-method (write (o <py-seq-iter>) . l)
+ (define port (if (null? l) #t (car l)))
+ (for ((x : o)) ((l '()))
+ (cons x l)
+ #:final
+ (let ((l (reverse l)))
+ (if (null? l)
+ (format port "iter[]")
+ (format port "iter[~a~{, ~a~}]" (car l) (cdr l))))))
+
+
+;;WRAP-IN
(define-method (wrap-in (o <py-list>))
(let ((out (make <py-list-iter>)))
(slot-set! out 'n (slot-ref o 'n ))
@@ -496,20 +508,13 @@
(let ((out (make <py-list-iter>)))
(slot-set! out 'i (- (slot-ref o 'n) 1))
(slot-set! out 'vec (slot-ref o 'vec))
- (slot-set! out 'n 0)
- (slot-set! out 'd -1)
- out))
-
-(define-method (py-reversed (s <string>))
- (let ((out (make <string-iter>)))
- (slot-set! out 'str s)
- (slot-set! out 'i 0)
+ (slot-set! out 'n (slot-ref o 'n))
(slot-set! out 'd -1)
out))
(define-method (py-reversed (o <p>))
(aif it (ref o '__reversed__)
- it
+ (it)
(let ((a (ref o '__getitem__))
(n (ref o '__len__)))
(if (and a n)
@@ -522,7 +527,7 @@
(define-method (wrap-in (o <p>))
(aif it (ref o '__iter__)
- it
+ (it)
(let ((a (ref o '__getitem__)))
(if a
(let ((ret (make <py-seq-iter>)))
@@ -533,8 +538,24 @@
(next-method)))))
-(define-method (wrap-in (o <py-list-iter>)) o)
-(define-method (wrap-in (o <string-iter> )) o)
+(define-method (wrap-in (o <py-list-iter>))
+ (let ((out (make <py-list-iter>)))
+ (slot-set! out 'vec (slot-ref o 'vec))
+ (slot-set! out 'i (slot-ref o 'i))
+ (slot-set! out 'n (slot-ref o 'n))
+ (slot-set! out 'd (slot-ref o 'd))
+ out))
+
+(define-method (wrap-in (o <py-seq-iter>))
+ (let ((out (make <py-seq-iter>)))
+ (slot-set! out 'o (slot-ref o 'o))
+ (slot-set! out 'i (slot-ref o 'i))
+ (slot-set! out 'n (slot-ref o 'n))
+ (slot-set! out 'd (slot-ref o 'd))
+ out))
+
+
+
(define-method (wrap-in (o <py-seq-iter> )) o)
;;NEXT
@@ -546,22 +567,6 @@
(slot-set! o 'i (+ i d))
r)))
-(define-method (next (o <string-iter>))
- (let ((i (slot-ref o 'i ))
- (d (slot-ref o 'd))
- (str (slot-ref o 'str)))
- (if (> d 0)
- (if (< i (string-length str))
- (let ((ret (string-ref str i)))
- (slot-set! o 'i (+ i d))
- (list->string (scm-list ret)))
- (throw StopIteration))
- (if (>= i 0)
- (let ((ret (string-ref str i)))
- (slot-set! o 'i (+ i d))
- (list->string (scm-list ret)))
- (throw StopIteration)))))
-
(define-method (next (o <py-list-iter>))
(let ((i (slot-ref o 'i ))
(d (slot-ref o 'd))
@@ -573,7 +578,7 @@
(slot-set! o 'i (+ i 1))
ret)
(throw StopIteration))
- (if (>= i n)
+ (if (>= i 0)
(let ((ret (vector-ref vec i)))
(slot-set! o 'i (- i 1))
ret)