# paulfioravanti's solution

## to Accumulate in the Elm Track

Published at Jun 26 2019 · 0 comments
Instructions
Test suite
Solution

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.

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

Conversation with James Edward Gray II https://twitter.com/jeg2

## Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (square, tests)

import Accumulate exposing (accumulate)
import Expect
import String
import Test exposing (..)

square : Int -> Int
square x =
x * x

tests : Test
tests =
describe "Accumulate"
[ test "[] Accumulate" <|
\() -> Expect.equal [] (accumulate square [])
, skip <|
test "square Accumulate" <|
\() -> Expect.equal [ 1, 4, 9 ] (accumulate square [ 1, 2, 3 ])
, skip <|
test "toUpper Accumulate" <|
\() ->
Expect.equal [ "HELLO", "WORLD" ]
(accumulate String.toUpper [ "hello", "world" ])
, skip <|
test "reverse Accumulate" <|
\() ->
Expect.equal [ "olleh", "dlrow" ]
(accumulate String.reverse [ "hello", "world" ])
]``````
``````module Accumulate exposing (accumulate)

accumulate : (a -> b) -> List a -> List b
accumulate func input =
case input of
head :: tail ->
func head :: accumulate func tail

[] ->
[]``````