diff options
-rw-r--r-- | ChangeLog | 10 | ||||
-rw-r--r-- | eval.c | 1 | ||||
-rw-r--r-- | lib.c | 33 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | txr.1 | 16 |
5 files changed, 61 insertions, 0 deletions
@@ -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. @@ -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)); @@ -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); @@ -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); @@ -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 |