-
-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add implementation for the optional goal (lists)
- Loading branch information
1 parent
0e49dd3
commit a232b25
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ())) |