summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2014-07-29 22:01:51 -0700
committerKaz Kylheku <kaz@kylheku.com>2014-07-29 22:01:51 -0700
commit537155e326be00e079a6aadb6a894fef733a39a0 (patch)
tree58b22b512543c5477d178d7ff4b0b061b23c367f
parent187685ec560594fd5e1b26b76c6fea4ec92cc652 (diff)
downloadtxr-537155e326be00e079a6aadb6a894fef733a39a0.tar.gz
txr-537155e326be00e079a6aadb6a894fef733a39a0.tar.bz2
txr-537155e326be00e079a6aadb6a894fef733a39a0.zip
* eval.c (eval_init): Register uniq function.
* lib.c (uniq): New function. * lib.h (uniq): Declared. * txr.1: Documented uniq.
-rw-r--r--ChangeLog10
-rw-r--r--eval.c1
-rw-r--r--lib.c33
-rw-r--r--lib.h1
-rw-r--r--txr.116
5 files changed, 61 insertions, 0 deletions
diff --git a/ChangeLog b/ChangeLog
index 7d55f198..073faff1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,15 @@
2014-07-29 Kaz Kylheku <kaz@kylheku.com>
+ * eval.c (eval_init): Register uniq function.
+
+ * lib.c (uniq): New function.
+
+ * lib.h (uniq): Declared.
+
+ * txr.1: Documented uniq.
+
+2014-07-29 Kaz Kylheku <kaz@kylheku.com>
+
* eval.c (repeatv): Renamed to repeat. Turned into function
with one optional argument, reflecting existing behavior.
(eval_init): Registration of repeat updated.
diff --git a/eval.c b/eval.c
index 359c4988..9119b0f4 100644
--- a/eval.c
+++ b/eval.c
@@ -3772,6 +3772,7 @@ void eval_init(void)
reg_fun(intern(lit("hash-diff"), user_package), func_n2(hash_diff));
reg_fun(intern(lit("hash-isec"), user_package), func_n3o(hash_isec, 2));
reg_fun(intern(lit("group-by"), user_package), func_n2v(group_by));
+ reg_fun(intern(lit("uniq"), user_package), func_n1(uniq));
reg_fun(intern(lit("hash-update"), user_package), func_n2(hash_update));
reg_fun(intern(lit("hash-update-1"), user_package),
func_n4o(hash_update_1, 3));
diff --git a/lib.c b/lib.c
index afb134b8..e0e98782 100644
--- a/lib.c
+++ b/lib.c
@@ -5314,6 +5314,39 @@ val multi_sort(val lists, val funcs, val key_funcs)
return mapcarv(list_f, tuples);
}
+val uniq(val seq)
+{
+ val hash = make_hash(nil, nil, t);
+ list_collect_decl (out, ptail);
+
+ if (vectorp(seq) || stringp(seq)) {
+ cnum i, len;
+
+ for (i = 0, len = c_num(length(seq)); i < len; i++) {
+ val new_p;
+ val v = ref(seq, num_fast(i));
+
+ (void) gethash_c(hash, v, mkcloc(new_p));
+
+ if (new_p)
+ ptail = list_collect(ptail, v);
+ }
+ } else {
+ for (; seq; seq = cdr(seq)) {
+ val new_p;
+ val v = car(seq);
+
+ (void) gethash_c(hash, v, mkcloc(new_p));
+
+ if (new_p)
+ ptail = list_collect(ptail, v);
+ }
+ }
+
+ return make_like(out, seq);
+}
+
+
val find(val item, val list, val testfun, val keyfun)
{
testfun = default_arg(testfun, equal_f);
diff --git a/lib.h b/lib.h
index b6051100..63d3fbd5 100644
--- a/lib.h
+++ b/lib.h
@@ -751,6 +751,7 @@ val interpose(val sep, val seq);
val merge(val list1, val list2, val lessfun, val keyfun);
val sort(val seq, val lessfun, val keyfun);
val multi_sort(val lists, val funcs, val key_funcs);
+val uniq(val seq);
val find(val list, val key, val testfun, val keyfun);
val find_if(val pred, val list, val key);
val find_max(val seq, val testfun, val keyfun);
diff --git a/txr.1 b/txr.1
index 33c634d7..3f3879e8 100644
--- a/txr.1
+++ b/txr.1
@@ -10751,6 +10751,22 @@ The sort function is stable for sequences which are lists. This means that the
original order of items which are considered identical is preserved.
For strings and vectors, the sort is not stable.
+.SS Function uniq
+
+.TP
+Syntax:
+
+ (uniq <sequence>)
+
+.TP
+Description:
+
+The uniq function returns a sequence of the same kind as <sequence>, but with
+duplicates removed. Elements of <sequence> are considered equal under
+the equal function. The first occurrence of each element is retained,
+and the subsequent duplicates of that element, of any, are suppressed,
+such that the order of the elements is otherwise preserved.
+
.SS Function tuples
.TP