From 6577a1d3c3e250ea9cd204baac4633e5e68ceabb Mon Sep 17 00:00:00 2001 From: Leah Neukirchen Date: Thu, 5 Jan 2023 23:36:34 +0100 Subject: add len>= --- mew.scm | 15 ++++++++++++++- mew.svnwiki | 6 ++++++ tests/test.mew | 10 ++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/mew.scm b/mew.scm index 70bcff6..0e54d91 100644 --- a/mew.scm +++ b/mew.scm @@ -12,7 +12,7 @@ imp inc inc! inject inject-accumulator into juxt keys - len lines loc + len len>= lines loc mod negate nth-accumulator odometer one-of op op* @@ -399,6 +399,19 @@ ((procedure? o) (generator-count (op #t) o)) (#t (error "no len defined")))) + (define (len>= o n) + (cond ((list? o) + (let loop ((o o) (n n)) + (cond ((null? o) (not (positive? n))) + ((positive? n) (loop (cdr o) (- n 1))) + (else #t)))) + ((procedure? o) + (let loop ((v (o)) (n n)) + (cond ((eof-object? v) (not (positive? n))) + ((positive? n) (loop (o) (- n 1))) + (else #t)))) + (else (>= (len o) n)))) + (define dup (case-lambda ((o) (dup o #t)) diff --git a/mew.svnwiki b/mew.svnwiki index d3adc99..a7f55a0 100644 --- a/mew.svnwiki +++ b/mew.svnwiki @@ -438,6 +438,12 @@ Test if {{}} is an empty list/string/vector/hash-table. Return the length of the list/vector/string/hash-table/generator {{}}. +(len>= ) + +Return true if the list/vector/string/hash-table/generator {{}} +has at least {{}} elements. This is more efficient for lists and +generators than {{len}} and works on infinite structures. + (dup []) Return a duplicate of the nested datastructure {{}}, consisting diff --git a/tests/test.mew b/tests/test.mew index 13817b0..837edd8 100644 --- a/tests/test.mew +++ b/tests/test.mew @@ -394,6 +394,16 @@ (test 3 (len (generator 5 6 7))) (test-error (len #t))) +(test-group "len>=" + (test #t (len>= '() 0)) + (test #f (len>= '() 1)) + (test #t (len>= '(1) 0)) + (test #t (len>= '(1 2 3) 2)) + (test #f (len>= '(1 2 3) 4)) + (test #t (len>= (generator 5 6 7) 2)) + (test #f (len>= (generator 5 6 7) 4)) + (test-error (len>= #t 0))) + (test-group "dup" (let* ((x (list (list "foo") "bar")) (y1 (dup x)) -- cgit 1.4.1