Last active
December 18, 2015 21:19
-
-
Save 0xGGGGG/5846691 to your computer and use it in GitHub Desktop.
doctest not working for nonmatching function clause.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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