Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Add accumulate exercise (#110)
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSchierboom authored May 6, 2023
1 parent cc4dda8 commit f4527a8
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,16 @@
"difficulty": 5,
"topics": null
},
{
"slug": "accumulate",
"name": "Accumulate",
"uuid": "204845d0-73b3-4f68-b8d7-05af5bbff118",
"practices": [],
"prerequisites": [
"numbers"
],
"difficulty": 2
},
{
"slug": "pov",
"name": "Pov",
Expand Down
22 changes: 22 additions & 0 deletions exercises/practice/accumulate/.docs/instructions.md
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.
19 changes: 19 additions & 0 deletions exercises/practice/accumulate/.meta/config.json
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"
}
9 changes: 9 additions & 0 deletions exercises/practice/accumulate/.meta/src/example.cljs
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)))))))

10 changes: 10 additions & 0 deletions exercises/practice/accumulate/deps.edn
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"]}}}
5 changes: 5 additions & 0 deletions exercises/practice/accumulate/src/accumulate.cljs
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
)
30 changes: 30 additions & 0 deletions exercises/practice/accumulate/test/accumulate_test.cljs
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")))))

0 comments on commit f4527a8

Please sign in to comment.