Sha256: 7139475aac36fdd3c410703e96b8bb4850bfe28549bd71ff5f9085fba8e4dc02

Contents?: true

Size: 534 Bytes

Versions: 396

Compression:

Stored size: 534 Bytes

Contents

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([], _fun) do
    []
  end

  def accumulate([head | tail], fun) do
    [fun.(head) | accumulate(tail, fun)]
  end
end

Version data entries

396 entries across 396 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/accumulate/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/accumulate/example.exs