From e2b65990f8d38fd60a072e7a76cba09c96518ecf Mon Sep 17 00:00:00 2001 From: Hugo Duksis Date: Thu, 8 Dec 2016 19:37:58 +0100 Subject: [PATCH 1/3] Adds count and reverse --- lib/list_ops.ex | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/list_ops.ex b/lib/list_ops.ex index 3010084..5b6b4d7 100644 --- a/lib/list_ops.ex +++ b/lib/list_ops.ex @@ -1,2 +1,21 @@ defmodule ListOps do + def count([]) do + 0 + end + + def count([_head | tail]) do + 1 + count(tail) + end + + def reverse(list) do + reverse(list, []) + end + + def reverse([], acumulator) do + acumulator + end + + def reverse([head | tail], acumulator) do + reverse(tail, [head | acumulator]) + end end From 938478963e3e7ece2000f8919113fff8a2c70fed Mon Sep 17 00:00:00 2001 From: Hugo Duksis Date: Fri, 9 Dec 2016 09:51:22 +0100 Subject: [PATCH 2/3] Make internal functions private --- lib/list_ops.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/list_ops.ex b/lib/list_ops.ex index 5b6b4d7..eaa75a5 100644 --- a/lib/list_ops.ex +++ b/lib/list_ops.ex @@ -11,11 +11,11 @@ defmodule ListOps do reverse(list, []) end - def reverse([], acumulator) do + defp reverse([], acumulator) do acumulator end - def reverse([head | tail], acumulator) do + defp reverse([head | tail], acumulator) do reverse(tail, [head | acumulator]) end end From be440b580882428d23d155b059038623070ee310 Mon Sep 17 00:00:00 2001 From: Hugo Duksis Date: Fri, 9 Dec 2016 09:58:00 +0100 Subject: [PATCH 3/3] Replace usage of odd?/1 with Integer.is_odd/1 --- test/list_ops_test.exs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/list_ops_test.exs b/test/list_ops_test.exs index c16ecd3..617e680 100644 --- a/test/list_ops_test.exs +++ b/test/list_ops_test.exs @@ -39,15 +39,15 @@ defmodule ListOpsTest do # end # # test "filter of empty list" do - # assert ListOps.filter([], &odd?/1) == [] + # assert ListOps.filter([], &Integer.is_odd/1) == [] # end # # test "filter of normal list" do - # assert ListOps.filter([1,2,3,4], &odd?/1) == [1,3] + # assert ListOps.filter([1,2,3,4], &Integer.is_odd/1) == [1,3] # end # # test "filter of huge list" do - # assert ListOps.filter(Enum.to_list(1..1_000_000), &odd?/1) == + # assert ListOps.filter(Enum.to_list(1..1_000_000), &Integer.is_odd/1) == # Enum.map(1..500_000, &(&1*2-1)) # end #