Skip to content

Commit

Permalink
Merge pull request #151 from dalexj/clearer-sum-of-multiples
Browse files Browse the repository at this point in the history
Get rid of sum of multiples default arguments
  • Loading branch information
parkerl committed Mar 23, 2016
2 parents 3867a18 + 0bd5881 commit 4ee3718
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 13 deletions.
4 changes: 1 addition & 3 deletions exercises/sum-of-multiples/example.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ defmodule SumOfMultiples do

@doc """
Adds up all numbers from 1 to a given end number that are multiples of the factors provided.
The default factors are 3 and 5.
"""
@spec to(non_neg_integer, [non_neg_integer]) :: non_neg_integer
def to(limit, factors \\ [3, 5]) do
def to(limit, factors) do
Enum.reduce(1..limit-1, 0, fn(n, acc) ->
if Enum.any?(factors, &(rem(n, &1) == 0)), do: acc + n, else: acc end)
end
Expand Down
6 changes: 2 additions & 4 deletions exercises/sum-of-multiples/sum_of_multiples.exs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
defmodule SumOfMultiples do
@doc """
Adds up all numbers from 1 to a given end number that are multiples of the factors provided.
The default factors are 3 and 5.
"""
@spec to(non_neg_integer, [non_neg_integer]) :: non_neg_integer
def to(limit, factors \\ [3, 5]) do
def to(limit, factors) do

end
end
12 changes: 6 additions & 6 deletions exercises/sum-of-multiples/sum_of_multiples_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,32 @@ defmodule SumOfMultiplesTest do

# @tag :pending
test "sum to 1" do
assert SumOfMultiples.to(1) == 0
assert SumOfMultiples.to(1, [3, 5]) == 0
end

@tag :pending
test "sum to 3" do
assert SumOfMultiples.to(4) == 3
assert SumOfMultiples.to(4, [3, 5]) == 3
end

@tag :pending
test "sum to 10" do
assert SumOfMultiples.to(10) == 23
assert SumOfMultiples.to(10, [3, 5]) == 23
end

@tag :pending
test "sum to 20" do
assert SumOfMultiples.to(20) == 78
assert SumOfMultiples.to(20, [3, 5]) == 78
end

@tag :pending
test "sum to 100" do
assert SumOfMultiples.to(100) == 2318
assert SumOfMultiples.to(100, [3, 5]) == 2318
end

@tag :pending
test "sum to 1000" do
assert SumOfMultiples.to(1000) == 233168
assert SumOfMultiples.to(1000, [3, 5]) == 233168
end

@tag :pending
Expand Down

0 comments on commit 4ee3718

Please sign in to comment.