summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog14
-rw-r--r--eval.c1
-rw-r--r--lib.c34
-rw-r--r--lib.h1
-rw-r--r--txr.120
5 files changed, 67 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index 1f72f094..693c5d27 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
2014-03-23 Kaz Kylheku <kaz@kylheku.com>
+ * 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 <kaz@kylheku.com>
+
* eval.c (not_s): New symbol var.
(me_unless, me_while, m_until): New static functions.
(eval_init): Register macros unless, while and until.
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 <sequence>, and
-returns it.
+returns it. <sequence> may be a hash, in which case (hash-count <sequence>) is
+returned.
+
+.SS Function empty
+
+.TP
+Syntax:
+
+ (empty <sequence>)
+
+.TP
+Description:
+
+Returns t if (length <sequence>) is zero, otherwise nil.
.SS Function copy
@@ -9741,8 +9754,9 @@ Description:
The copy function duplicates a sequence. If <sequence> is nil, it
returns nil. If <sequence> is a list, it returns (copy-list <sequence>);
-if <sequence> is a string, it returns (copy-str <sequence>); and
-if <sequence> is a vector, it returns (copy-vec <sequence>).
+if <sequence> is a string, it returns (copy-str <sequence>);
+if <sequence> is a vector, it returns (copy-vec <sequence>); and
+if <sequence> is a hash, it return (copy-hash <sequence>).
Except in the case when <sequence> is nil, copy returns a value that
is distinct from (not eq to) <sequence>. This is different from