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(list, fun) do
  #     cond do
  #         length(list) == 0 -> []
  #         length(list) == 1 -> fun.(List.first(list))
  #         true ->
  #             [head | tail] = list
  #             [fun.(head),  accumulate(tail, fun)]
  #     end
  # end

  @spec accumulate(list, (any -> any)) :: list
  def accumulate(list, fun) do
      List.foldl(list, [], (fn(x, acc) -> acc ++ [fun.(x)] end))
  end
end
