Sha256: a57517626f2fd6b4414a670c65b731f47bc1c06e18bbfef4fb08770b720d3eca

Contents?: true

Size: 1.55 KB

Versions: 69

Compression:

Stored size: 1.55 KB

Contents

defmodule ListOps do
  # Please don't use any external modules (especially List) in your
  # implementation. The point of this exercise is to create these basic functions
  # yourself.
  #
  # Note that `++` is a function from an external module (Kernel, which is
  # automatically important`) and so shouldn't be used either.

  @spec count(list) :: non_neg_integer
  def count(l), do: do_count(l, 0)

  defp do_count([], acc), do: acc
  defp do_count([_ | t], acc), do: do_count(t, acc + 1)

  @spec reverse(list) :: list
  def reverse(l), do: do_reverse(l, [])

  defp do_reverse([], acc), do: acc
  defp do_reverse([h | t], acc), do: do_reverse(t, [h | acc])

  @spec map(list, (any -> any)) :: list
  def map(l, f), do: do_map(l, f, []) |> reverse()

  defp do_map([], _, acc), do: acc
  defp do_map([h | t], f, acc), do: do_map(t, f, [f.(h) | acc])

  @spec filter(list, (any -> as_boolean(term))) :: list
  def filter(l, f), do: do_filter(l, f, []) |> reverse()

  defp do_filter([], _, acc), do: acc

  defp do_filter([h | t], f, acc) do
    if f.(h) do
      do_filter(t, f, [h | acc])
    else
      do_filter(t, f, acc)
    end
  end

  @type acc :: any
  @spec reduce(list, acc, (any, acc -> acc)) :: acc
  def reduce([], acc, _), do: acc
  def reduce([h | t], acc, f), do: reduce(t, f.(h, acc), f)

  @spec append(list, list) :: list
  def append(a, b), do: do_append(reverse(a), b)

  defp do_append([], b), do: b
  defp do_append([h | t], b), do: do_append(t, [h | b])

  @spec concat([[any]]) :: [any]
  def concat(ll), do: reverse(ll) |> reduce([], &append(&1, &2))
end

Version data entries

69 entries across 69 versions & 1 rubygems

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