From 537155e326be00e079a6aadb6a894fef733a39a0 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Tue, 29 Jul 2014 22:01:51 -0700 Subject: * eval.c (eval_init): Register uniq function. * lib.c (uniq): New function. * lib.h (uniq): Declared. * txr.1: Documented uniq. --- ChangeLog | 10 ++++++++++ eval.c | 1 + lib.c | 33 +++++++++++++++++++++++++++++++++ lib.h | 1 + txr.1 | 16 ++++++++++++++++ 5 files changed, 61 insertions(+) diff --git a/ChangeLog b/ChangeLog index 7d55f198..073faff1 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +2014-07-29 Kaz Kylheku + + * 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 * eval.c (repeatv): Renamed to repeat. Turned into function 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 ) + +.TP +Description: + +The uniq function returns a sequence of the same kind as , but with +duplicates removed. Elements of 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 -- cgit v1.2.3