From 9ec101dc65dd7676b5544b752c1af879d011a376 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Sun, 23 Mar 2014 17:19:26 -0700 Subject: * eval.c (eval_init): Register empty as intrinsic. * lib.c (copy): Bugfix: handle lazy strings. Also, handle hash tables via copy_hash. (length): Bugifx: handle lazy strings. Also, handle hash tables via hash_count. (empty): New function. * lib.h (empty): Declared. * txr.1: Documented. --- ChangeLog | 14 ++++++++++++++ eval.c | 1 + lib.c | 34 ++++++++++++++++++++++++++++++++++ lib.h | 1 + txr.1 | 20 +++++++++++++++++--- 5 files changed, 67 insertions(+), 3 deletions(-) diff --git a/ChangeLog b/ChangeLog index 1f72f094..693c5d27 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2014-03-23 Kaz Kylheku + + * eval.c (eval_init): Register empty as intrinsic. + + * lib.c (copy): Bugfix: handle lazy strings. Also, handle hash + tables via copy_hash. + (length): Bugifx: handle lazy strings. Also, handle hash tables + via hash_count. + (empty): New function. + + * lib.h (empty): Declared. + + * txr.1: Documented. + 2014-03-23 Kaz Kylheku * eval.c (not_s): New symbol var. diff --git a/eval.c b/eval.c index 3d4ca301..8e70ec29 100644 --- a/eval.c +++ b/eval.c @@ -3527,6 +3527,7 @@ void eval_init(void) reg_fun(intern(lit("set-diff"), user_package), func_n4o(set_diff, 2)); reg_fun(intern(lit("length"), user_package), func_n1(length)); + reg_fun(intern(lit("empty"), user_package), func_n1(empty)); reg_fun(intern(lit("copy"), user_package), func_n1(copy)); reg_fun(intern(lit("sub"), user_package), func_n3o(sub, 1)); reg_fun(intern(lit("ref"), user_package), func_n2(ref)); diff --git a/lib.c b/lib.c index 885455ed..505ae3e6 100644 --- a/lib.c +++ b/lib.c @@ -4932,9 +4932,14 @@ val copy(val seq) return copy_list(seq); case LIT: case STR: + case LSTR: return copy_str(seq); case VEC: return copy_vec(seq); + case COBJ: + if (seq->co.cls == hash_s) + return copy_hash(seq); + /* fallthrough */ default: type_mismatch(lit("copy: ~s is not a sequence"), seq, nao); } @@ -4950,14 +4955,43 @@ val length(val seq) return length_list(seq); case LIT: case STR: + case LSTR: return length_str(seq); case VEC: return length_vec(seq); + case COBJ: + if (seq->co.cls == hash_s) + return hash_count(seq); + /* fallthrough */ default: type_mismatch(lit("length: ~s is not a sequence"), seq, nao); } } +val empty(val seq) +{ + switch (type(seq)) { + case NIL: + return t; + case CONS: + case LCONS: + return nil; + case LIT: + case STR: + return if2(c_str(seq)[0] == 0, t); + case LSTR: + return length_str_le(seq, zero); + case VEC: + return eq(length_vec(seq), zero); + case COBJ: + if (seq->co.cls == hash_s) + return eq(hash_count(seq), zero); + /* fallthrough */ + default: + type_mismatch(lit("empty: ~s is not a sequence"), seq, nao); + } +} + val sub(val seq, val from, val to) { switch (type(seq)) { diff --git a/lib.h b/lib.h index 5d7c694c..b48bf39f 100644 --- a/lib.h +++ b/lib.h @@ -712,6 +712,7 @@ val pos_if(val pred, val list, val key); val set_diff(val list1, val list2, val testfun, val keyfun); val copy(val seq); val length(val seq); +val empty(val seq); val sub(val seq, val from, val to); val ref(val seq, val ind); val refset(val seq, val ind, val newval); diff --git a/txr.1 b/txr.1 index f0fd80b4..60906447 100644 --- a/txr.1 +++ b/txr.1 @@ -9727,7 +9727,20 @@ Syntax: Description: The length function returns the number of items in , and -returns it. +returns it. may be a hash, in which case (hash-count ) is +returned. + +.SS Function empty + +.TP +Syntax: + + (empty ) + +.TP +Description: + +Returns t if (length ) is zero, otherwise nil. .SS Function copy @@ -9741,8 +9754,9 @@ Description: The copy function duplicates a sequence. If is nil, it returns nil. If is a list, it returns (copy-list ); -if is a string, it returns (copy-str ); and -if is a vector, it returns (copy-vec ). +if is a string, it returns (copy-str ); +if is a vector, it returns (copy-vec ); and +if is a hash, it return (copy-hash ). Except in the case when is nil, copy returns a value that is distinct from (not eq to) . This is different from -- cgit v1.2.3