Skip to content

Commit

Permalink
add implementation for the optional goal (lists)
Browse files Browse the repository at this point in the history
  • Loading branch information
tasxatzial committed Nov 10, 2024
1 parent 0e49dd3 commit a232b25
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions exercises/practice/list-ops/.meta/src/example_lists.clj
Original file line number Diff line number Diff line change
@@ -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 ()))

0 comments on commit a232b25

Please sign in to comment.