Skip to content

Commit ef5dce3

Browse files
authored
add accumulate exercise (exercism#407)
1 parent b21ffaa commit ef5dce3

File tree

7 files changed

+150
-0
lines changed

7 files changed

+150
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,14 @@
846846
"practices": [],
847847
"prerequisites": [],
848848
"difficulty": 5
849+
},
850+
{
851+
"slug": "accumulate",
852+
"name": "Accumulate",
853+
"uuid": "093463bf-e8df-4dd9-b6e3-b5ec007cb781",
854+
"practices": [],
855+
"prerequisites": [],
856+
"difficulty": 2
849857
}
850858
]
851859
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Instructions
2+
3+
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.
4+
5+
Given the collection of numbers:
6+
7+
- 1, 2, 3, 4, 5
8+
9+
And the operation:
10+
11+
- square a number (`x => x * x`)
12+
13+
Your code should be able to produce the collection of squares:
14+
15+
- 1, 4, 9, 16, 25
16+
17+
Check out the test suite to see the expected function signature.
18+
19+
## Restrictions
20+
21+
Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library!
22+
Solve this one yourself using other basic tools instead.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"kmarker1101"
4+
],
5+
"files": {
6+
"solution": [
7+
"accumulate.el"
8+
],
9+
"test": [
10+
"accumulate-test.el"
11+
],
12+
"example": [
13+
".meta/example.el"
14+
]
15+
},
16+
"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.",
17+
"source": "Conversation with James Edward Gray II",
18+
"source_url": "http://graysoftinc.com/"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
;;; accumulate.el --- Accumulate (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun accumulate (lst op)
9+
(cond
10+
((null lst) lst)
11+
(t (cons (funcall op (car lst)) (accumulate (cdr lst) op)))))
12+
13+
14+
(provide 'accumulate)
15+
;;; accumulate.el ends here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[64d97c14-36dd-44a8-9621-2cecebd6ed23]
13+
description = "accumulate empty"
14+
15+
[00008ed2-4651-4929-8c08-8b4dbd70872e]
16+
description = "accumulate squares"
17+
18+
[551016da-4396-4cae-b0ec-4c3a1a264125]
19+
description = "accumulate upcases"
20+
21+
[cdf95597-b6ec-4eac-a838-3480d13d0d05]
22+
description = "accumulate reversed strings"
23+
24+
[bee8e9b6-b16f-4cd2-be3b-ccf7457e50bb]
25+
description = "accumulate recursively"
26+
include = false
27+
28+
[0b357334-4cad-49e1-a741-425202edfc7c]
29+
description = "accumulate recursively"
30+
reimplements = "bee8e9b6-b16f-4cd2-be3b-ccf7457e50bb"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
;;; accumulate-test.el --- Accumulate (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(load-file "accumulate.el")
9+
(declare-function accumulate "accumulate.el" (lst op))
10+
11+
12+
(ert-deftest accumulate-empty ()
13+
(let ((result (accumulate '() (lambda (x) (* x x)))))
14+
(should (equal result '()))))
15+
16+
17+
(ert-deftest accumulate-square ()
18+
(let ((result (accumulate '(1 2 3 4) (lambda (x) (* x x)))))
19+
(should (equal result '(1 4 9 16)))))
20+
21+
22+
(ert-deftest accumulate-upcases ()
23+
(let ((result (accumulate '("Hello" "world") 'upcase)))
24+
(should (equal result '("HELLO" "WORLD")))))
25+
26+
27+
(ert-deftest accumulate-reversed-strings ()
28+
(let ((result (accumulate '("the" "quick" "brown" "fox" "etc") (lambda (x) (apply #'string (reverse (string-to-list x)))))))
29+
(should (equal result '("eht" "kciuq" "nworb" "xof" "cte")))))
30+
31+
32+
(ert-deftest accumulate-recursively ()
33+
(let* ((inner-list '("1" "2" "3"))
34+
(result (accumulate '("a" "b" "c")
35+
(lambda (x)
36+
(mapcar (lambda (y) (concat x y)) inner-list)))))
37+
(should (equal result '(("a1" "a2" "a3")
38+
("b1" "b2" "b3")
39+
("c1" "c2" "c3"))))))
40+
41+
42+
(provide 'accumulate-test)
43+
;;; accumulate-test.el ends here
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
;;; accumulate.el --- Accumulate (exercism) -*- lexical-binding: t; -*-
2+
3+
;;; Commentary:
4+
5+
;;; Code:
6+
7+
8+
(defun accumulate (lst op)
9+
(error "Delete this S-Expression and write your own implementation"))
10+
11+
12+
(provide 'accumulate)
13+
;;; accumulate.el ends here

0 commit comments

Comments
 (0)