Skip to content

Commit

Permalink
add accumulate exercise (#407)
Browse files Browse the repository at this point in the history
  • Loading branch information
kmarker1101 authored Jun 15, 2024
1 parent b21ffaa commit ef5dce3
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "accumulate",
"name": "Accumulate",
"uuid": "093463bf-e8df-4dd9-b6e3-b5ec007cb781",
"practices": [],
"prerequisites": [],
"difficulty": 2
}
]
},
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": [
"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/"
}
15 changes: 15 additions & 0 deletions exercises/practice/accumulate/.meta/example.el
Original file line number Diff line number Diff line change
@@ -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
30 changes: 30 additions & 0 deletions exercises/practice/accumulate/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
43 changes: 43 additions & 0 deletions exercises/practice/accumulate/accumulate-test.el
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions exercises/practice/accumulate/accumulate.el
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ef5dce3

Please sign in to comment.