summaryrefslogtreecommitdiffstats
path: root/lib.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2022-02-21 03:08:11 -0800
committerKaz Kylheku <kaz@kylheku.com>2022-02-21 03:08:11 -0800
commitaad381bc6ce5458ef2483238066ae7fe1577d508 (patch)
treece47953b39b6a1fadcbf45052cc79c83f55bdbf6 /lib.c
parentc914063fd0eb8c02d8362b86a61933c656a938bc (diff)
downloadtxr-aad381bc6ce5458ef2483238066ae7fe1577d508.tar.gz
txr-aad381bc6ce5458ef2483238066ae7fe1577d508.tar.bz2
txr-aad381bc6ce5458ef2483238066ae7fe1577d508.zip
New functions: find-max-key and find-min-key.
* eval.c (eval_init): Register find-max-key and find-min-key intrinsics. * lib.c (find_max_key, find_min_key): New functions. * lib.h (find_max_key, find_min_key): Declared. * txr.1: Documented. * stdlib/doc-syms.tl: Updated.
Diffstat (limited to 'lib.c')
-rw-r--r--lib.c30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib.c b/lib.c
index 2a3b7680..ba8dca50 100644
--- a/lib.c
+++ b/lib.c
@@ -10940,11 +10940,41 @@ val find_max(val seq, val testfun, val keyfun)
}
}
+val find_max_key(val seq, val testfun_in, val keyfun_in)
+{
+ val self = lit("find-max-key");
+ val testfun = default_arg(testfun_in, greater_f);
+ val keyfun = default_arg(keyfun_in, identity_f);
+ seq_iter_t iter;
+ val elem;
+
+ seq_iter_init(self, &iter, seq);
+
+ if (seq_get(&iter, &elem)) {
+ val maxkey = funcall1(keyfun, elem);
+
+ while (seq_get(&iter, &elem)) {
+ val key = funcall1(keyfun, elem);
+ if (funcall2(testfun, key, maxkey))
+ maxkey = key;
+ }
+
+ return maxkey;
+ }
+
+ return nil;
+}
+
val find_min(val seq, val testfun, val keyfun)
{
return find_max(seq, default_arg(testfun, less_f), keyfun);
}
+val find_min_key(val seq, val testfun, val keyfun)
+{
+ return find_max_key(seq, default_arg(testfun, less_f), keyfun);
+}
+
val find_true(val pred, val seq, val key)
{
val self = lit("find-true");