From a232b25a1c0fce02368b271b08dd519ff600f5a1 Mon Sep 17 00:00:00 2001 From: Anastasios Chatzialexiou <16361161+tasxatzial@users.noreply.github.com> Date: Sun, 10 Nov 2024 12:45:05 +0200 Subject: [PATCH] add implementation for the optional goal (lists) --- .../list-ops/.meta/src/example_lists.clj | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 exercises/practice/list-ops/.meta/src/example_lists.clj diff --git a/exercises/practice/list-ops/.meta/src/example_lists.clj b/exercises/practice/list-ops/.meta/src/example_lists.clj new file mode 100644 index 00000000..94b9c89e --- /dev/null +++ b/exercises/practice/list-ops/.meta/src/example_lists.clj @@ -0,0 +1,46 @@ +(ns list-ops) + +(declare foldl) +(declare reverse-order) + +(defn append + [coll1 coll2] + (reverse-order (foldl conj coll2 (reverse-order coll1)))) + +(defn concatenate + [colls] + (foldl append colls ())) + +(defn select-if + [pred coll] + (let [reducer (fn [acc el] + (if (pred el) + (conj acc el) + acc))] + (reverse-order (foldl reducer coll ())))) + +(defn length + [coll] + (let [reducer (fn [acc _] + (inc acc))] + (foldl reducer coll 0))) + +(defn apply-to-each + [f coll] + (let [reducer (fn [acc el] + (conj acc (f el)))] + (reverse-order (foldl reducer coll ())))) + +(defn foldl + [f coll acc] + (if (seq coll) + (recur f (rest coll) (f acc (first coll))) + acc)) + +(defn foldr + [f coll acc] + (foldl f (reverse-order coll) acc)) + +(defn reverse-order + [coll] + (foldl conj coll ()))