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:
And the operation:
x => x * x)
Your code should be able to produce the collection of squares:
Check out the test suite to see the expected function signature.
Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.
Refer to the Installing Elm page for information about installing elm.
The code you have to write is located inside the
src/ directory of the exercise.
Elm automatically installs packages dependencies the first time you run the tests
so we can start by running the tests from the exercise directory with:
To automatically run tests again when you save changes:
$ elm-test --watch
As you work your way through the tests suite in the file
be sure to remove the
calls from each test until you get them all passing!
Conversation with James Edward Gray II https://twitter.com/jeg2
It is possible to submit an incomplete solution so you can see how others have completed the exercise.
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 = List.map func input
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.