-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove wip status * add vectors as prerequisite * make instructions append more clear * change 'init' parameter to 'acc' * split into one deftest per test * update instructions append * rewrite example solution * update starter file * update instructions append * add implementation for the optional goal (lists)
- Loading branch information
1 parent
587a1e8
commit d94f4b5
Showing
7 changed files
with
221 additions
and
120 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
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 |
---|---|---|
@@ -1,8 +1,19 @@ | ||
# Instructions append | ||
|
||
## Implementation tips | ||
## Appendix | ||
|
||
- The instructions use lists because they are synced with a shared repository to maintain consistency across tracks. | ||
However, for this exercise in the Clojure track, you should assume that you are working with vectors instead of lists. | ||
- It is important not to reuse existing Clojure built-in functions with similar functionality, as doing so would diminish the intended learning value of the exercise. | ||
Some of these functions include `into`, `concat`, `cat`, `lazy-cat`, `mapcat`, `flatten`, `filter`, `filterv`, `remove`, `count`, `map`, `mapv`, `reduce`, `transduce`, `reverse`, and `rseq` from the **clojure.core** namespace. | ||
The instructions are synced with a shared repository to ensure consistency across all language tracks. | ||
For this exercise in the Clojure track, assume both the input and output are vectors. | ||
As a stretch goal, consider how you could implement the solution without using lists anywhere in your code. | ||
Also, think about the efficiency of your program. | ||
|
||
It is important not to reuse existing Clojure built-in functions with similar functionality, as doing so would diminish the intended learning value of the exercise. | ||
Key functions from the **clojure.core** namespace to avoid include `into`, `concat`, `cat`, `lazy-cat`, `mapcat`, `flatten`, `filter`, `filterv`, `remove`, `count`, `map`, `mapv`, `reduce`, `transduce`, `reverse`, and `rseq`. | ||
|
||
### Optional goals | ||
|
||
Try to pass the tests by devising a solution that assumes both the input and output are lists instead of vectors. | ||
The test suite does not need to be modified. | ||
This time, consider how you could implement the solution without using vectors anywhere in your code. | ||
|
||
If you decide to publish this solution, be sure to include a comment indicating that it addresses the optional goal of using lists. Don't forget to update the docstrings! |
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
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 |
---|---|---|
@@ -1,34 +1,50 @@ | ||
(ns list-ops) | ||
|
||
(defn foldl [f coll init] | ||
(loop [acc init l coll] | ||
(cond | ||
(empty? l) acc | ||
:else (recur (f acc (first l)) (rest l))))) | ||
|
||
(defn append [coll1 coll2] | ||
(list-ops/foldl (fn [acc elem] (conj acc elem)) coll2 coll1)) | ||
|
||
(defn concatenate [coll] | ||
(cond | ||
(empty? coll) [] | ||
:else (list-ops/foldl (fn [acc elem] (list-ops/append acc elem)) (rest coll) (first coll)))) | ||
|
||
(defn select-if [pred coll] | ||
(loop [acc [] l coll] | ||
(cond | ||
(empty? l) acc | ||
(pred (first l)) (recur (conj acc (first l)) (rest l)) | ||
:else (recur acc (rest l))))) | ||
|
||
(defn length [coll] | ||
(list-ops/foldl (fn [acc elem] (+ acc 1)) coll 0)) | ||
|
||
(defn apply-to-each [f coll] | ||
(list-ops/foldl (fn [acc elem] (conj acc (f elem))) coll [])) | ||
|
||
(defn reverse-order [coll] | ||
(list-ops/foldl (fn [acc elem] (cons elem acc)) coll [])) | ||
|
||
(defn foldr [f coll init] | ||
(list-ops/foldl f (list-ops/reverse-order coll) init)) | ||
(declare foldl) | ||
(declare reverse-order) | ||
|
||
(defn append | ||
[coll1 coll2] | ||
(foldl conj coll2 coll1)) | ||
|
||
(defn concatenate | ||
[colls] | ||
(foldl append colls [])) | ||
|
||
(defn select-if | ||
[pred coll] | ||
(let [reducer (fn [acc el] | ||
(if (pred el) | ||
(conj acc el) | ||
acc))] | ||
(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)))] | ||
(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] | ||
(loop [index (dec (length coll)) | ||
result []] | ||
(if (neg? index) | ||
result | ||
(recur (dec index) (conj result (coll index)))))) |
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 ())) |
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
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