diff --git a/config.json b/config.json index 68294d3c..cb944adc 100644 --- a/config.json +++ b/config.json @@ -846,6 +846,14 @@ "practices": [], "prerequisites": [], "difficulty": 5 + }, + { + "slug": "accumulate", + "name": "Accumulate", + "uuid": "093463bf-e8df-4dd9-b6e3-b5ec007cb781", + "practices": [], + "prerequisites": [], + "difficulty": 2 } ] }, diff --git a/exercises/practice/accumulate/.docs/instructions.md b/exercises/practice/accumulate/.docs/instructions.md new file mode 100644 index 00000000..c25a03fa --- /dev/null +++ b/exercises/practice/accumulate/.docs/instructions.md @@ -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. diff --git a/exercises/practice/accumulate/.meta/config.json b/exercises/practice/accumulate/.meta/config.json new file mode 100644 index 00000000..884c92ba --- /dev/null +++ b/exercises/practice/accumulate/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "kmarker1101" + ], + "files": { + "solution": [ + "accumulate.el" + ], + "test": [ + "accumulate-test.el" + ], + "example": [ + ".meta/example.el" + ] + }, + "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": "http://graysoftinc.com/" +} diff --git a/exercises/practice/accumulate/.meta/example.el b/exercises/practice/accumulate/.meta/example.el new file mode 100644 index 00000000..b0c8d080 --- /dev/null +++ b/exercises/practice/accumulate/.meta/example.el @@ -0,0 +1,15 @@ +;;; accumulate.el --- Accumulate (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(defun accumulate (lst op) + (cond + ((null lst) lst) + (t (cons (funcall op (car lst)) (accumulate (cdr lst) op))))) + + +(provide 'accumulate) +;;; accumulate.el ends here diff --git a/exercises/practice/accumulate/.meta/tests.toml b/exercises/practice/accumulate/.meta/tests.toml new file mode 100644 index 00000000..d7858e07 --- /dev/null +++ b/exercises/practice/accumulate/.meta/tests.toml @@ -0,0 +1,30 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[64d97c14-36dd-44a8-9621-2cecebd6ed23] +description = "accumulate empty" + +[00008ed2-4651-4929-8c08-8b4dbd70872e] +description = "accumulate squares" + +[551016da-4396-4cae-b0ec-4c3a1a264125] +description = "accumulate upcases" + +[cdf95597-b6ec-4eac-a838-3480d13d0d05] +description = "accumulate reversed strings" + +[bee8e9b6-b16f-4cd2-be3b-ccf7457e50bb] +description = "accumulate recursively" +include = false + +[0b357334-4cad-49e1-a741-425202edfc7c] +description = "accumulate recursively" +reimplements = "bee8e9b6-b16f-4cd2-be3b-ccf7457e50bb" diff --git a/exercises/practice/accumulate/accumulate-test.el b/exercises/practice/accumulate/accumulate-test.el new file mode 100644 index 00000000..25a8dc6b --- /dev/null +++ b/exercises/practice/accumulate/accumulate-test.el @@ -0,0 +1,43 @@ +;;; accumulate-test.el --- Accumulate (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(load-file "accumulate.el") +(declare-function accumulate "accumulate.el" (lst op)) + + +(ert-deftest accumulate-empty () + (let ((result (accumulate '() (lambda (x) (* x x))))) + (should (equal result '())))) + + +(ert-deftest accumulate-square () + (let ((result (accumulate '(1 2 3 4) (lambda (x) (* x x))))) + (should (equal result '(1 4 9 16))))) + + +(ert-deftest accumulate-upcases () + (let ((result (accumulate '("Hello" "world") 'upcase))) + (should (equal result '("HELLO" "WORLD"))))) + + +(ert-deftest accumulate-reversed-strings () + (let ((result (accumulate '("the" "quick" "brown" "fox" "etc") (lambda (x) (apply #'string (reverse (string-to-list x))))))) + (should (equal result '("eht" "kciuq" "nworb" "xof" "cte"))))) + + +(ert-deftest accumulate-recursively () + (let* ((inner-list '("1" "2" "3")) + (result (accumulate '("a" "b" "c") + (lambda (x) + (mapcar (lambda (y) (concat x y)) inner-list))))) + (should (equal result '(("a1" "a2" "a3") + ("b1" "b2" "b3") + ("c1" "c2" "c3")))))) + + +(provide 'accumulate-test) +;;; accumulate-test.el ends here diff --git a/exercises/practice/accumulate/accumulate.el b/exercises/practice/accumulate/accumulate.el new file mode 100644 index 00000000..098d060d --- /dev/null +++ b/exercises/practice/accumulate/accumulate.el @@ -0,0 +1,13 @@ +;;; accumulate.el --- Accumulate (exercism) -*- lexical-binding: t; -*- + +;;; Commentary: + +;;; Code: + + +(defun accumulate (lst op) + (error "Delete this S-Expression and write your own implementation")) + + +(provide 'accumulate) +;;; accumulate.el ends here