Skip to content

Instantly share code, notes, and snippets.

@0xGGGGG
Last active December 18, 2015 21:19
Show Gist options
  • Save 0xGGGGG/5846691 to your computer and use it in GitHub Desktop.
Save 0xGGGGG/5846691 to your computer and use it in GitHub Desktop.
doctest not working for nonmatching function clause.
defmodule ListExtensions do
@moduledoc false
@doc """
Traverses all of the elements in given list
and then sums the result and returns it.
Does not accept types other than list.
### Examples
iex> ListExtensions.mapsum [1,2,3,4], &1 * &1
1 + 4 + 9 + 16
iex> ListExtensions.mapsum {1,2,3,4}, &1 * &1
** (FunctionClauseError)
"""
def mapsum(list, fun)
when is_list(list) do
do_mapsum(list, 0, fun)
end
defp do_mapsum([], sum, _), do: sum
defp do_mapsum([ head | list ], sum, fun) do
do_mapsum(list, sum + fun.(head), fun)
end
end
ExUnit.start
defmodule ListExtensions.Test do
use ExUnit.Case, async: true
doctest ListExtensions
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment