summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2018-11-27 23:19:49 -0800
committerKaz Kylheku <kaz@kylheku.com>2018-11-27 23:19:49 -0800
commit71f4c886ece16c2f8f1c52b86298cd348db37b8f (patch)
tree27ea1eb988b83b800e5f88a88efe96532425f18c
parent38d30d7ce812bcccc5de475dd272eda9fb87e579 (diff)
downloadtxr-71f4c886ece16c2f8f1c52b86298cd348db37b8f.tar.gz
txr-71f4c886ece16c2f8f1c52b86298cd348db37b8f.tar.bz2
txr-71f4c886ece16c2f8f1c52b86298cd348db37b8f.zip
New range testing functions.
* eval.c (eval_init): Register in-range and in-range* intrinsics. * lib.c (in_range, in_range_star): New functions. * lib.h (in_range, in_range_star): Declared. * txr.1: Documented.
-rw-r--r--eval.c2
-rw-r--r--lib.c20
-rw-r--r--lib.h2
-rw-r--r--txr.152
4 files changed, 76 insertions, 0 deletions
diff --git a/eval.c b/eval.c
index 9219c8d4..8e9fb067 100644
--- a/eval.c
+++ b/eval.c
@@ -6771,6 +6771,8 @@ void eval_init(void)
reg_fun(intern(lit("rangep"), user_package), func_n1(rangep));
reg_fun(intern(lit("from"), user_package), func_n1(from));
reg_fun(intern(lit("to"), user_package), func_n1(to));
+ reg_fun(intern(lit("in-range"), user_package), func_n2(in_range));
+ reg_fun(intern(lit("in-range*"), user_package), func_n2(in_range_star));
reg_fun(intern(lit("make-like"), user_package), func_n2(make_like));
reg_fun(intern(lit("nullify"), user_package), func_n1(nullify));
diff --git a/lib.c b/lib.c
index 5a4eb3ad..d06a71b1 100644
--- a/lib.c
+++ b/lib.c
@@ -10639,6 +10639,26 @@ val set_to(val range, val to)
return range;
}
+val in_range(val range, val num)
+{
+ type_check(lit("in-range"), range, RNG);
+ {
+ val from = range->rn.from;
+ val to = range->rn.to;
+ return and2(lequal(from, num), lequal(num, to));
+ }
+}
+
+val in_range_star(val range, val num)
+{
+ type_check(lit("in-range*"), range, RNG);
+ {
+ val from = range->rn.from;
+ val to = range->rn.to;
+ return and2(lequal(from, num), less(num, to));
+ }
+}
+
val env(void)
{
if (env_list) {
diff --git a/lib.h b/lib.h
index 14882390..d74304db 100644
--- a/lib.h
+++ b/lib.h
@@ -1080,6 +1080,8 @@ val from(val range);
val to(val range);
val set_from(val range, val from);
val set_to(val range, val to);
+val in_range(val range, val num);
+val in_range_star(val range, val num);
val env(void);
void out_str_char(wchar_t ch, val out, int *semi_flag, int regex);
val obj_print_impl(val obj, val out, val pretty, struct strm_ctx *);
diff --git a/txr.1 b/txr.1
index 76e4f629..c2636c81 100644
--- a/txr.1
+++ b/txr.1
@@ -21384,6 +21384,58 @@ of a range.
Note that these functions are not accessors, which is because
ranges are immutable.
+.coNP Functions @ in-range and @ in-range*
+.synb
+.mets (in-range < range << value )
+.mets (in-range* < range << value )
+.syne
+.desc
+The
+.code in-range
+and
+.code in-range*
+functions test whether the
+.meta value
+argument lies in the range represented by the
+.meta range
+argument, indicating the Boolean result using one of the values
+.code t
+or
+.codn nil .
+
+The
+.meta range
+argument must be a range object.
+
+It is expected that the range object's
+.code from
+value does not exceed the
+.code to
+value; a reversed range is considered empty.
+
+The
+.code in-range*
+function differs from
+.code in-range
+in that it excludes the
+upper endpoint.
+
+The implicit comparison against the range endpoints is performed
+using the
+.code less
+and
+.code lequal
+functions, as appropriate.
+
+The following equivalences hold:
+
+.cblk
+ (in-range r x) <--> (and (lequal (from r) x)
+ (lequal (to r) x))
+
+ (in-range* r x) <--> (and (lequal (from r) x)
+ (less (to r) x))
+.cble
.SS* Characters and Strings
.coNP Function @ mkstring
.synb