This repository has been archived by the owner on May 30, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cc4dda8
commit f4527a8
Showing
7 changed files
with
105 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
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,22 @@ | ||
# Instructions | ||
|
||
Implement the `accumulate` operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection. | ||
|
||
Given the collection of numbers: | ||
|
||
- 1, 2, 3, 4, 5 | ||
|
||
And the operation: | ||
|
||
- square a number (`x => x * x`) | ||
|
||
Your code should be able to produce the collection of squares: | ||
|
||
- 1, 4, 9, 16, 25 | ||
|
||
Check out the test suite to see the expected function signature. | ||
|
||
## Restrictions | ||
|
||
Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library! | ||
Solve this one yourself using other basic tools instead. |
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,19 @@ | ||
{ | ||
"authors": [ | ||
"ErikSchierboom" | ||
], | ||
"files": { | ||
"solution": [ | ||
"src/accumulate.cljs" | ||
], | ||
"test": [ | ||
"test/accumulate_test.cljs" | ||
], | ||
"example": [ | ||
".meta/src/example.cljs" | ||
] | ||
}, | ||
"blurb": "Implement the `accumulate` operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.", | ||
"source": "Conversation with James Edward Gray II", | ||
"source_url": "https://twitter.com/jeg2" | ||
} |
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,9 @@ | ||
(ns accumulate) | ||
|
||
(defn accumulate [f xs] | ||
(loop [xs xs | ||
accum []] | ||
(if | ||
(empty? xs) accum | ||
(recur (rest xs) (conj accum (f (first xs))))))) | ||
|
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,10 @@ | ||
{:deps | ||
{org.clojure/clojure {:mvn/version "1.10.1"} | ||
org.clojure/clojurescript {:mvn/version "1.10.773"}} | ||
|
||
:aliases | ||
{:test | ||
{:extra-paths ["test"] | ||
:extra-deps | ||
{olical/cljs-test-runner {:mvn/version "3.8.0"}} | ||
:main-opts ["-m" "cljs-test-runner.main"]}}} |
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,5 @@ | ||
(ns accumulate) | ||
|
||
(defn accumulate [] ;; <- arglist goes here | ||
;; your code goes here | ||
) |
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,30 @@ | ||
(ns accumulate-test | ||
(:require [clojure.test :refer [deftest is]] | ||
accumulate)) | ||
|
||
(defn- square [n] (* n n)) | ||
|
||
(defn- to-s [xs] (apply str xs)) | ||
|
||
(deftest empty-accumulation | ||
(is (= [] (accumulate/accumulate square [])))) | ||
|
||
(deftest accumulate-squares | ||
(is (= [1 4 9] (accumulate/accumulate square [1 2 3])))) | ||
|
||
(deftest accumulate-upcases | ||
(is (= ["HELLO", "WORLD"] | ||
(->> ["hello" "world"] | ||
(accumulate/accumulate clojure.string/upper-case) | ||
(map to-s))))) | ||
|
||
(deftest accumulate-reversed-strings | ||
(is (= ["eht" "kciuq" "nworb" "xof" "cte"] | ||
(->> ["the" "quick" "brown" "fox" "etc"] | ||
(accumulate/accumulate reverse) | ||
(map to-s))))) | ||
|
||
(deftest accumulate-recursively | ||
(is (= [["a1" "a2" "a3"] ["b1" "b2" "b3"] ["c1" "c2" "c3"]] | ||
(-> #(accumulate/accumulate (fn [n] (str % n)) [1 2 3]) | ||
(accumulate/accumulate "abc"))))) |