Commit 2d3c12f6e0f33ac82425c9bd4184759442ef1875

Thomas de Grivel 2024-10-23T08:50:17

List.filter

diff --git a/lib/kc3/0.1/list.kc3 b/lib/kc3/0.1/list.kc3
index a72eb3f..e174d04 100644
--- a/lib/kc3/0.1/list.kc3
+++ b/lib/kc3/0.1/list.kc3
@@ -9,6 +9,18 @@ defmodule List do
     ([a | b], f) { f(a); each(b, f) }
   }
 
+  def filter = fn {
+    (list, f) { filter(list, f, []) }
+    ([], _, acc) { List.reverse(acc) }
+    ([first | rest], f, acc) {
+      if (result = f(first)) do
+        filter(rest, f, [result | acc])
+      else
+        filter(rest, f, acc)
+      end
+    }
+  }
+
   def find_if = fn {
     ([], _) { false }
     ([a | b], f) do