Refactor the comma in List syntax

This commit is contained in:
Trung Lê
2016-05-05 19:23:25 +10:00
parent 386111ba6d
commit c1e3f6a80d

View File

@@ -2,23 +2,23 @@ defmodule Enums do
use Koans use Koans
koan "Knowing how many elements are in a list is important for book-keeping" do koan "Knowing how many elements are in a list is important for book-keeping" do
assert Enum.count([1 ,2 ,3]) == ___ assert Enum.count([1, 2, 3]) == ___
end end
koan "Depending on the type, it counts pairs" do koan "Depending on the type, it counts pairs" do
assert Enum.count(%{ a: :foo, b: :bar}) == ___ assert Enum.count(%{a: :foo, b: :bar}) == ___
end end
def less_than_five?(n), do: n < 5 def less_than_five?(n), do: n < 5
koan "Elements can have a lot in common" do koan "Elements can have a lot in common" do
assert Enum.all?([1 ,2 ,3], &less_than_five?/1) == ___ assert Enum.all?([1, 2, 3], &less_than_five?/1) == ___
assert Enum.all?([4, 6 ,8], &less_than_five?/1) == ___ assert Enum.all?([4, 6, 8], &less_than_five?/1) == ___
end end
def even?(n), do: rem(n, 2) == 0 def even?(n), do: rem(n, 2) == 0
koan "Sometimes you just want to know if there are any elements fulfilling a condition" do koan "Sometimes you just want to know if there are any elements fulfilling a condition" do
assert Enum.any?([1 ,2 ,3], &even?/1) == ___ assert Enum.any?([1, 2, 3], &even?/1) == ___
assert Enum.any?([1 ,3 ,5], &even?/1) == ___ assert Enum.any?([1, 3, 5], &even?/1) == ___
end end
koan "Sometimes you just want to know if an element is part of the party" do koan "Sometimes you just want to know if an element is part of the party" do
@@ -29,51 +29,51 @@ defmodule Enums do
def multiply_by_ten(n), do: 10 * n def multiply_by_ten(n), do: 10 * n
koan "Map converts each element of a list by running some function with it" do koan "Map converts each element of a list by running some function with it" do
assert Enum.map([1 ,2 ,3], &multiply_by_ten/1) == ___ assert Enum.map([1, 2, 3], &multiply_by_ten/1) == ___
end end
def odd?(n), do: rem(n, 2) == 1 def odd?(n), do: rem(n, 2) == 1
koan "Filter allows you to only keep what you really care about" do koan "Filter allows you to only keep what you really care about" do
assert Enum.filter([1 ,2 ,3], &odd?/1) == ___ assert Enum.filter([1, 2, 3], &odd?/1) == ___
end end
koan "Reject will help you throw out unwanted cruft" do koan "Reject will help you throw out unwanted cruft" do
assert Enum.reject([1 ,2 ,3], &odd?/1) == ___ assert Enum.reject([1, 2, 3], &odd?/1) == ___
end end
koan "You three there, follow me!" do koan "You three there, follow me!" do
assert Enum.take([1 ,2 ,3 ,4 ,5], 3) == ___ assert Enum.take([1, 2, 3, 4, 5], 3) == ___
end end
koan "You can ask for a lot, but Enum won't hand you more than you give" do koan "You can ask for a lot, but Enum won't hand you more than you give" do
assert Enum.take([1 ,2 ,3 ,4 ,5], 10) == ___ assert Enum.take([1, 2, 3, 4, 5], 10) == ___
end end
koan "Just like taking, you can also drop elements" do koan "Just like taking, you can also drop elements" do
assert Enum.drop([-1 ,0 ,1 ,2 ,3], 2) == ___ assert Enum.drop([-1, 0, 1, 2, 3], 2) == ___
end end
koan "Zip-up in pairs!" do koan "Zip-up in pairs!" do
numbers = [1 ,2 ,3] numbers = [1, 2, 3]
letters = [:a, :b, :c] letters = [:a, :b, :c]
assert Enum.zip(numbers, letters) == ___ assert Enum.zip(numbers, letters) == ___
end end
koan "When you want to find that one pesky element" do koan "When you want to find that one pesky element" do
assert Enum.find([1 ,2 ,3, 4], &even?/1) == ___ assert Enum.find([1, 2, 3, 4], &even?/1) == ___
end end
def divisible_by_five?(n), do: rem(n, 5) == 0 def divisible_by_five?(n), do: rem(n, 5) == 0
koan "...but you don't quite find it..." do koan "...but you don't quite find it..." do
assert Enum.find([1 ,2 ,3], &divisible_by_five?/1) == ___ assert Enum.find([1, 2, 3], &divisible_by_five?/1) == ___
end end
koan "...you can settle for a consolation prize" do koan "...you can settle for a consolation prize" do
assert Enum.find([1 ,2 ,3], :no_such_element, &divisible_by_five?/1) == ___ assert Enum.find([1, 2, 3], :no_such_element, &divisible_by_five?/1) == ___
end end
koan "Collapse an entire list of elements down to a single one by repeating a function." do koan "Collapse an entire list of elements down to a single one by repeating a function." do
assert Enum.reduce([1 ,2 ,3], 0, fn(element, accumulator) -> element + accumulator end) == ___ assert Enum.reduce([1, 2, 3], 0, fn(element, accumulator) -> element + accumulator end) == ___
end end
end end