Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of thekeele

thekeele's solution

to Accumulate in the Elixir Track

Published at Jul 13 2018 · 0 comments
Test suite


This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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.


Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

Running tests

Execute the tests with:

$ elixir accumulate_test.exs

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

For more detailed information about the Elixir track, please see the help page.


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

Submitting Incomplete Solutions

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


if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("accumulate.exs", __DIR__)

ExUnit.configure(exclude: :pending, trace: true)

defmodule AccumulateTest do
  use ExUnit.Case

  test "accumulate empty list" do
    assert Accumulate.accumulate([], fn n -> n * n end) == []

  @tag :pending
  test "accumulate square numbers" do
    assert Accumulate.accumulate([1, 2, 3], fn n -> n * n end) == [1, 4, 9]

  @tag :pending
  test "accumulate upcased strings" do
    fun = fn w -> String.upcase(w) end
    assert Accumulate.accumulate(["hello", "world"], fun) == ["HELLO", "WORLD"]

  @tag :pending
  test "accumulate reversed strings" do
    fun = fn w -> String.reverse(w) end
    words = ~w(the quick brown fox etc)
    expected = ["eht", "kciuq", "nworb", "xof", "cte"]
    assert Accumulate.accumulate(words, fun) == expected

  @tag :pending
  test "nested accumulate" do
    chars = ~w(a b c)
    nums = ~w(1 2 3)
    fun = fn c -> Accumulate.accumulate(nums, &(c <> &1)) end
    expected = [["a1", "a2", "a3"], ["b1", "b2", "b3"], ["c1", "c2", "c3"]]
    assert Accumulate.accumulate(chars, fun) == expected
defmodule Accumulate do
  @doc """
    Given a list and a function, apply the function to each list item and
    replace it with the function's return value.

    Returns a list.

    ## Examples

      iex> Accumulate.accumulate([], fn(x) -> x * 2 end)

      iex> Accumulate.accumulate([1, 2, 3], fn(x) -> x * 2 end)
      [2, 4, 6]

  @spec accumulate(list, (any -> any)) :: list
  def accumulate([], _), do: []
  def accumulate([car | cdr], fun) do
    [fun.(car) | accumulate(cdr, fun)]

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?