Commit d13410b6a0578f3a40fc33ec5724d9d587e8d983

Thomas de Grivel 2024-11-26T18:44:44

List.last

diff --git a/lib/kc3/0.1/list.kc3 b/lib/kc3/0.1/list.kc3
index 057c15b..a501818 100644
--- a/lib/kc3/0.1/list.kc3
+++ b/lib/kc3/0.1/list.kc3
@@ -31,6 +31,8 @@ defmodule List do
     ([first], sep, acc) { str(reverse([first | acc])) }
   }
 
+  def last = cfn Tag "list_last" (List, Result)
+
   def map = cfn List "list_map" (List, Callable, Result)
 
   def reverse = fn {
diff --git a/libkc3/list.c b/libkc3/list.c
index 900c0d5..1a5d4d1 100644
--- a/libkc3/list.c
+++ b/libkc3/list.c
@@ -291,6 +291,25 @@ bool list_is_plist (const s_list *list)
   return true;
 }
 
+s_tag * list_last (s_list **list, s_tag *dest)
+{
+  s_list *l;
+  s_list *last;
+  s_tag tag = {0};
+  last = NULL;
+  l = *list;
+  while (l) {
+    last = l;
+    l = list_next(l);
+  }
+  if (last) {
+    if (! tag_init_copy(&tag, &last->tag))
+      return NULL;
+  }
+  *dest = tag;
+  return dest;
+}
+
 sw list_length (const s_list *list)
 {
   sw length = 0;
diff --git a/test/ikc3/list.kc3 b/test/ikc3/list.kc3
index 130085e..b471427 100644
--- a/test/ikc3/list.kc3
+++ b/test/ikc3/list.kc3
@@ -52,3 +52,5 @@ quote List.join(["1", "2", "3"], ", ")
 List.join(["1", "2", "3"], ", ")
 quote List.each([1, 2, 3], fn (x) { puts(x) })
 List.each([1, 2, 3], fn (x) { puts(x) })
+quote List.last([1, 2, 3])
+List.last([1, 2, 3])
diff --git a/test/ikc3/list.out.expected b/test/ikc3/list.out.expected
index 61273c4..d04c5d1 100644
--- a/test/ikc3/list.out.expected
+++ b/test/ikc3/list.out.expected
@@ -35,3 +35,5 @@ List.each([1, 2, 3], fn (x) { puts(x) })
 2
 3
 true
+List.last([1, 2, 3])
+3