summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2023-08-17 07:15:55 -0700
committerKaz Kylheku <kaz@kylheku.com>2023-08-17 07:15:55 -0700
commit1a4bdbc566683f0e70412fd75af233526803d0d2 (patch)
tree2810e787cba69552aeca6ed73ccfcdfed291ebb6
parent927ce14064e931edcf9f55582fd6a4f4b7c0567c (diff)
downloadtxr-1a4bdbc566683f0e70412fd75af233526803d0d2.tar.gz
txr-1a4bdbc566683f0e70412fd75af233526803d0d2.tar.bz2
txr-1a4bdbc566683f0e70412fd75af233526803d0d2.zip
New function: csort-group.
* autoload.c (csort_set_entries): Register csort-group as autoload trigger for stdlib/csort.tl. * stdlib/csort.tl (csort-group): New function. * tests/012/sort.tl: Tests for sort-group and csort-group. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
-rw-r--r--autoload.c1
-rw-r--r--stdlib/csort.tl3
-rw-r--r--stdlib/doc-syms.tl3
-rw-r--r--tests/012/sort.tl10
-rw-r--r--txr.111
5 files changed, 26 insertions, 2 deletions
diff --git a/autoload.c b/autoload.c
index 417f6fb0..b251ae9e 100644
--- a/autoload.c
+++ b/autoload.c
@@ -960,6 +960,7 @@ static val csort_set_entries(val fun)
{
val name[] = {
lit("csort"), lit("cnsort"), lit("cssort"), lit("csnsort"),
+ lit("csort-group"),
nil
};
autoload_set(al_fun, name, fun);
diff --git a/stdlib/csort.tl b/stdlib/csort.tl
index 44715236..345b4ce8 100644
--- a/stdlib/csort.tl
+++ b/stdlib/csort.tl
@@ -41,3 +41,6 @@
(defun csnsort (seq : (less-fun :) (key-fun :))
(cached-sort-body snsort))
+
+(defun csort-group (seq : (key-fun :) (less-fun :))
+ (partition-by key-fun (csort seq less-fun key-fun)))
diff --git a/stdlib/doc-syms.tl b/stdlib/doc-syms.tl
index f3d793d2..edde5200 100644
--- a/stdlib/doc-syms.tl
+++ b/stdlib/doc-syms.tl
@@ -453,6 +453,7 @@
("csize" "N-01B1B5DF")
("csnsort" "N-02102493")
("csort" "N-02102493")
+ ("csort-group" "N-00CDF784")
("cssort" "N-02102493")
("cstopb" "N-01B1B5DF")
("cum-norm-dist" "N-03AB449B")
@@ -1920,7 +1921,7 @@
("some-false" "N-016BDF48")
("some-true" "N-016BDF48")
("sort" "N-03923640")
- ("sort-group" "N-01E65DDC")
+ ("sort-group" "N-00CDF784")
("source-loc" "N-0370CD69")
("source-loc-str" "N-0370CD69")
("span-str" "N-0394CA3A")
diff --git a/tests/012/sort.tl b/tests/012/sort.tl
index 4989a74b..03c122d3 100644
--- a/tests/012/sort.tl
+++ b/tests/012/sort.tl
@@ -78,3 +78,13 @@
(cssort svec) vec
(cssort vec (fun greater)) (reverse vec)
(cssort svec (fun greater)) (reverse vec)))
+
+(mtest
+ [sort-group '((a 1) (b 1) (a 2) (b 2) (a 3) (c 2) (c 1) (a 4)) car]
+ (((a 1) (a 2) (a 3) (a 4))
+ ((b 1) (b 2))
+ ((c 2) (c 1)))
+ [csort-group '((a 1) (b 1) (a 2) (b 2) (a 3) (c 2) (c 1) (a 4)) car]
+ (((a 1) (a 2) (a 3) (a 4))
+ ((b 1) (b 2))
+ ((c 2) (c 1))))
diff --git a/txr.1 b/txr.1
index f41337c0..6e61c412 100644
--- a/txr.1
+++ b/txr.1
@@ -38372,7 +38372,7 @@ has the same effect as:
-> "uryyb, jbeyq!"
.brev
-.coNP Function @ sort-group
+.coNP Functions @ sort-group and @ csort-group
.synb
.mets (sort-group < sequence >> [ keyfun <> [ lessfun ]])
.syne
@@ -38389,6 +38389,15 @@ arguments, and then breaks the resulting sequence into groups,
based on the equivalence of the elements under
.metn keyfun .
+The
+.code csort-group
+differs from
+.code sort-group
+in that it is based on the caching
+.code csort
+rather than
+.codn sort .
+
The following equivalence holds:
.verb