summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-10-04 06:16:32 -0700
committerKaz Kylheku <kaz@kylheku.com>2016-10-04 06:16:32 -0700
commit755f99d4f397cc2357468e3c2dc19d3ea451dedb (patch)
tree02358f6b761edff37462d732a97bea7033195842
parent20758a37854c852c5a6574ced48bb0aca9417132 (diff)
downloadtxr-755f99d4f397cc2357468e3c2dc19d3ea451dedb.tar.gz
txr-755f99d4f397cc2357468e3c2dc19d3ea451dedb.tar.bz2
txr-755f99d4f397cc2357468e3c2dc19d3ea451dedb.zip
length and empty functions support ranges.
* lib.c (length, empty): Handle RNG in switch. * txr.1: Documented.
-rw-r--r--lib.c4
-rw-r--r--txr.171
2 files changed, 64 insertions, 11 deletions
diff --git a/lib.c b/lib.c
index 062f35c6..8676f9e2 100644
--- a/lib.c
+++ b/lib.c
@@ -8150,6 +8150,8 @@ val length(val seq)
return length_str(seq);
case VEC:
return length_vec(seq);
+ case RNG:
+ return minus(to(seq), from(seq));
case COBJ:
if (seq->co.cls == hash_s)
return hash_count(seq);
@@ -8176,6 +8178,8 @@ val empty(val seq)
return length_str_le(seq, zero);
case VEC:
return eq(length_vec(seq), zero);
+ case RNG:
+ return eql(from(seq), to(seq));
case COBJ:
if (seq->co.cls == hash_s)
return eq(hash_count(seq), zero);
diff --git a/txr.1 b/txr.1
index e215ee16..69691c23 100644
--- a/txr.1
+++ b/txr.1
@@ -18304,6 +18304,20 @@ and
.code pred
family of functions also operate on ranges.
+The length of a range may be obtained with the
+.code length
+function;
+
+The length of the range
+.code a..b
+is defined as
+.codn "(- b a)" ,
+and may be obtained using the
+.code length
+function. The
+.code empty
+function accepts ranges and tests them for zero length.
+
.coNP Function @ rcons
.synb
.mets (rcons < from << to )
@@ -22121,36 +22135,71 @@ the empty list and so is a sequence.
.coNP Function @ length
.synb
-.mets (length << sequence )
+.mets (length << object )
.syne
.desc
-The
+If
+.meta object
+is a sequence, the
.code length
-function returns the number of items in
-.metn sequence ,
-and
-returns it.
-.meta sequence
-may be a hash, in which case
+function returns the number of items it
+contains.
+
+If
+.meta object
+is a hash then the value of
.cblk
-.meti (hash-count << sequence )
+.meti (hash-count << object )
.cble
is returned.
+If
+.meta object
+is a range, then the length of the interval
+represented by the range is returned.
+The length of a range
+.code r
+is defined as
+.codn "(- (to r) (from r))" ,
+and thus may be negative.
+The length of
+.code "#R(1 -10)"
+is -11 and of
+.codn "#R(0.5 3)" ,
+2.5.
+
+For other types,
+.code length
+throws an error exception.
+
.coNP Function @ empty
.synb
-.mets (empty << sequence )
+.mets (empty << object )
.syne
.desc
+If
+.meta object
+is a suitable argument for the
+.code length
+function, then the
+.code empty
Returns
.code t
if
.cblk
-.meti (length << sequence )
+.meti (length << object )
.cble
is zero, otherwise
.codn nil .
+If
+.meta object
+is not a suitable argument for
+.codn length ,
+then
+.code empty
+throws an error exception.
+
.coNP Function @ copy
.synb
.mets (copy << object )