diff options
author | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2018-03-30 21:09:59 +0200 |
---|---|---|
committer | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2018-03-30 21:09:59 +0200 |
commit | 045653a5d82987865ecd304fb2777fda0edb88a0 (patch) | |
tree | 572a2fe6e7969773342f411a7bedf2b16220e5ab /modules/language/python/module | |
parent | b5f2f47ddaf43eff0ce328db4e2ec3bfc276bf86 (diff) |
itertools fully debugged
Diffstat (limited to 'modules/language/python/module')
-rw-r--r-- | modules/language/python/module/itertools.scm | 174 |
1 files changed, 88 insertions, 86 deletions
diff --git a/modules/language/python/module/itertools.scm b/modules/language/python/module/itertools.scm index c32f99c..7679459 100644 --- a/modules/language/python/module/itertools.scm +++ b/modules/language/python/module/itertools.scm @@ -14,11 +14,12 @@ combination_with_replacement)) (define count - (make-generator ll - (lambda* (yield start #:optional (step 1)) - (let lp ((i start)) - (yield i) - (lp (+ i step)))))) + (lambda* (start #:optional (step 1)) + ((make-generator () + (lambda (yield) + (let lp ((i start)) + (yield i) + (lp (+ i step)))))))) (define cycle (make-generator (p) @@ -28,36 +29,39 @@ (lp))))) (define repeat - (make-generator - (lambda* (yield e #:optional (n -1)) - (let lp ((i 0)) - (if (not (= i n)) - (begin - (yield e) - (lp (+ i 1)))))))) + (lambda* (e #:optional (n -1)) + ((make-generator () + (lambda (yield) + (let lp ((i 0)) + (if (not (= i n)) + (begin + (yield e) + (lp (+ i 1)))))))))) (define accumulate - (make-generator ll - (lambda* (yield p #:optional (f +)) - (for ((x : p)) ((s 0) (first? #t)) - (if first? - (begin - (yield x) - (values x #f)) - (let ((s (f x s))) - (yield s) - (values s #f))))))) - + (lambda* (p #:optional (f +)) + ((make-generator () + (lambda (yield) + (for ((x : p)) ((s 0) (first? #t)) + (if first? + (begin + (yield x) + (values x #f)) + (let ((s (f x s))) + (yield s) + (values s #f))))))))) + (define-python-class chain () (define __call__ - (make-generator ll - (lambda (yield . l) - (let lp ((l l)) - (if (pair? l) - (begin - (for ((x : (car l))) () - (yield x)) - (lp (cdr l)))))))) + (lambda (self . l) + ((make-generator () + (lambda (yield) + (let lp ((l l)) + (if (pair? l) + (begin + (for ((x : (car l))) () + (yield x)) + (lp (cdr l)))))))))) (define from_iterable (make-generator (i) @@ -94,33 +98,35 @@ (define none (list 'none)) (define groupby - (make-generator l - (lambda* (yield seq #:optional (key (lambda (x) x))) - (for ((x : seq)) ((k none) (l '())) - (if (eq? k none) - (values (key x) (list x)) - (let ((kk (key x))) - (if (equal? k kk) - (values k (cons x l)) - (begin - (yield k (reverse l)) - (values kk (list x)))))) - #:final - (if (not (eq? k none)) - (yield l (reverse l))))))) - + (lambda* (seq #:optional (key (lambda (x) x))) + ((make-generator () + (lambda (yield) + (for ((x : seq)) ((k none) (l '())) + (if (eq? k none) + (values (key x) (list x)) + (let ((kk (key x))) + (if (equal? k kk) + (values k (cons x l)) + (begin + (yield k (reverse l)) + (values kk (list x)))))) + #:final + (if (not (eq? k none)) + (yield k (reverse l))))))))) + (define isslice - (make-generator l - (lambda* (yield seq #:optional (start 0) (stop -1) (step 1)) - (for ((x : seq) (i : (count 0))) () - (if (= i stop) (break)) - (if (and (>= i start) - (= (modulo (- i start) step) 0)) - (yield x)))))) + (lambda* (seq #:optional (start 0) (stop -1) (step 1)) + ((make-generator () + (lambda (yield) + (for ((x : seq) (i : (count 0))) () + (if (= i stop) (break)) + (if (and (>= i start) + (= (modulo (- i start) step) 0)) + (yield x)))))))) (define starmap - (make-generator + (make-generator (f seq) (lambda (yield f seq) (for ((x : seq)) () (yield (f x)))))) @@ -173,48 +179,44 @@ (list it)))))) (define zip_longest - (make-generator - (lam (yield (* l) (= fillvalue None)) + (lam ((* l) (= fillvalue None)) + ((make-generator + (lambda (yield) (define mkit - (make-generator (it) - (lambda (yield it) - (for ((x : it)) () - (yield (cons 1 x)) - (let lp () - (yield (cons 0 0)) - (lp)))))) + (lambda (it) + ((make-generator () + (lambda (yield) + (for ((x : it)) () + (yield (cons 1 x))) + (let lp () + (yield (cons 0 0)) + (lp))))))) (for ((x : (apply zip (map mkit l)))) () (if (= (apply + (map car x)) 0) (break) - (yield (map (lambda (x) (if (= (car x) 0) fillvalue (cdr x))) - x))))))) + (yield (map (lambda (y) (if (= (car y) 0) fillvalue (cdr y))) + x))))))))) (def (product (* iterables) (= repeat 1)) ((make-generator () (lambda (yield) - (let* ((iterables (map iter iterables)) - (it0 (car iterables))) - (try - (lambda () - (let lp ((it0 it0) (l (cdr iterables)) (rl (list it0)) (res '())) - (let ((x (next it0))) - (if (pair? l) - (let ((it1.it2 (tee (car l)))) - (try - (lambda () - (lp (cdr it1.it2) (cdr l) - (cons (car it1.it2) rl) - (cons x res))) - (#:except StopIteration => - (lambda x - (lp it0 l rl res))))) - (begin - (yield (reverse (cons x res))) - (raise StopIteration)))))) - (#:except StopIteration => values))))))) - + (let* ((iterables (let lp ((i 1) (l (map iter iterables))) + (if (= i repeat) + l + (let* ((t (map tee l)) + (h (map car t)) + (r (map cadr t))) + (append h (lp (+ i 1) r))))))) + (let lp ((l iterables) (ret '())) + (if (pair? l) + (let ((x.y (tee (car l)))) + (for ((x : (car x.y))) ((l (cdr l))) + (lp l (cons x ret)) + #:final (cons (cadr x.y) l))) + (yield (reverse ret))))))))) + (def (permutation it (= r None)) ((make-generator () (lambda (yield) |