diff options
author | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2017-09-21 20:22:56 +0200 |
---|---|---|
committer | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2017-09-21 20:22:56 +0200 |
commit | a0edd0af042bd9ea95bae419c2cb54a6d16a6270 (patch) | |
tree | bdef8da8f1abb1e5f28558bfed3d404b932d26cc /modules/language/python/list.scm | |
parent | 88c7715ee5041a3a1e16968c3b73d780e0e87c4f (diff) |
in now a proper goops method
Diffstat (limited to 'modules/language/python/list.scm')
-rw-r--r-- | modules/language/python/list.scm | 39 |
1 files changed, 38 insertions, 1 deletions
diff --git a/modules/language/python/list.scm b/modules/language/python/list.scm index 3b7fbd5..a88aa0a 100644 --- a/modules/language/python/list.scm +++ b/modules/language/python/list.scm @@ -9,7 +9,7 @@ #:use-module (language python exceptions) #:export (to-list pylist-ref pylist-set! pylist-append! pylist-slice pylist-subset! pylist-reverse! - pylist-pop! pylist-count pylist-extend! + pylist-pop! pylist-count pylist-extend! len in pylist-insert! pylist-remove! pylist-sort!)) (define-syntax-rule (aif it p x y) (let ((it p)) (if it x y))) @@ -557,3 +557,40 @@ (define-method (pylist-index (o <p>) . l) (apply (ref o 'index) l)) +#:len + +(define-method (len (l <pair> )) (length l)) +(define-method (len (v <vector>)) (vector-length v)) +(define-method (len (s <string>)) (string-length s)) +(define-method (len (o <py-list>)) (slot-ref i 'n)) +(define-method (len (o <p>)) ((ref o '__len__))) + +(define-method (in x (l <pair>)) (member x l)) +(define-method (in x (l <vector>)) + (define n (vector-length l)) + (let lp ((i 0)) + (if (< i n) + (if (equal? x (vector-ref l i)) + #t + (lp (+ i 1))) + #f))) + +(define-method (in x (s <string>)) + (let ((n (string-length l)) + (x (if (string? x) (string-ref x 0) x))) + (let lp ((i 0)) + (if (< i n) + (if (equal? x (string-ref s i)) + #t + (lp (+ i 1))) + #f)))) + +(define-method (in x (o <py-list>)) + (define l (slot-ref o 'vec)) + (define n (slot-ref o 'n)) + (let lp ((i 0)) + (if (< i n) + (if (equal? x (vector-ref l i)) + #t + (lp (+ i 1))) + #f))) |