Pull helper methods out of koans
This commit is contained in:
@@ -13,24 +13,26 @@ defmodule Enums do
|
|||||||
assert Enum.count([a: 77, b: 23, c: 12, d: 33, e: 90, f: 113]) == :__
|
assert Enum.count([a: 77, b: 23, c: 12, d: 33, e: 90, f: 113]) == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
less_than_five = &(&1 <=5)
|
assert Enum.all?([1,2,3], &less_than_five/1) == :__
|
||||||
assert Enum.all?([1,2,3], less_than_five) == :__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def less_than_two(n), do: n < 2
|
||||||
koan "If one if different, all elements are not alike" do
|
koan "If one if different, all elements are not alike" do
|
||||||
less_than_two = &(&1 <=2)
|
assert Enum.all?([1, 2, 3, 2], &less_than_two/1) == :__
|
||||||
assert Enum.all?([1, 2, 3, 2], less_than_two) == :__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_even?(n), do: rem(n, 2) == 0
|
||||||
|
|
||||||
koan "sometimes you you just want to know if there are any elements fullfilling a condition" do
|
koan "sometimes you you just want to know if there are any elements fullfilling a condition" do
|
||||||
is_even? = &(rem(&1, 2) == 0)
|
assert Enum.any?([1,2,3], &is_even?/1) == :__
|
||||||
assert Enum.any?([1,2,3], is_even?) == :__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def divisible_by_five(n), do: rem(n, 5) == 0
|
||||||
|
|
||||||
koan "if not a single element fits the bill, any? returns false" do
|
koan "if not a single element fits the bill, any? returns false" do
|
||||||
divisible_by_five = &(rem(&1, 5) == 0)
|
assert Enum.any?([1,2,3], &divisible_by_five/1) == :__
|
||||||
assert Enum.any?([1,2,3], divisible_by_five) == :__
|
|
||||||
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
|
||||||
@@ -41,30 +43,31 @@ defmodule Enums do
|
|||||||
assert Enum.member?([1,2,3], 30) == :__
|
assert Enum.member?([1,2,3], 30) == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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
|
||||||
multiply_by_ten = &(&1 * 10)
|
assert Enum.map([1,2,3], &multiply_by_ten/1) == :__
|
||||||
assert Enum.map([1,2,3], multiply_by_ten) == :__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "You can even return a list with entirely different types" do
|
koan "You can even return a list with entirely different types" do
|
||||||
is_even? = &(rem(&1,2) == 0)
|
assert Enum.map([1,2,3], &is_even?/1) == :__
|
||||||
assert Enum.map([1,2,3], is_even?) == :__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
koan "But keep in mind that the original list remains unchanged" do
|
koan "But keep in mind that the original list remains unchanged" do
|
||||||
input = [1,2,3,4]
|
input = [1,2,3,4]
|
||||||
assert Enum.map(input, fn element -> rem(element, 2) == 0 end) == :__
|
assert Enum.map(input, &is_even?/1) == :__
|
||||||
assert input == :__
|
assert input == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_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
|
||||||
is_odd? = &(rem(&1, 2) == 1)
|
assert Enum.filter([1,2,3], &is_odd?/1) == :__
|
||||||
assert Enum.filter([1,2,3], is_odd?) == :__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "Reject will help you throw out unwanted cruft" do
|
koan "Reject will help you throw out unwanted cruft" do
|
||||||
is_odd? = &(rem(&1, 2) == 1)
|
assert Enum.reject([1,2,3], &is_odd?/1) == :__
|
||||||
assert Enum.reject([1,2,3], is_odd?) == :__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "You three there, follow me!" do
|
koan "You three there, follow me!" do
|
||||||
@@ -75,18 +78,18 @@ defmodule Enums do
|
|||||||
assert Enum.take([1,2,3,4,5], 10) == :__
|
assert Enum.take([1,2,3,4,5], 10) == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def less_than_four(n), do: n < 4
|
||||||
koan "Take what you can..." do
|
koan "Take what you can..." do
|
||||||
less_than_four = &(&1 < 4)
|
assert Enum.take_while([1,2,3,4,5,6,7], &less_than_four/1) == :__
|
||||||
assert Enum.take_while([1,2,3,4,5,6,7], less_than_four) == :__
|
|
||||||
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
|
||||||
|
|
||||||
|
def negative?(n), do: n < 0
|
||||||
koan "Drop elements until you are happy" do
|
koan "Drop elements until you are happy" do
|
||||||
negative = &(&1 <= 0)
|
assert Enum.drop_while([-1,0,1,2,3], &negative?/1) == :__
|
||||||
assert Enum.drop_while([-1,0,1,2,3], negative) == :__
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "Forming groups makes uns stronger" do
|
koan "Forming groups makes uns stronger" do
|
||||||
@@ -97,7 +100,6 @@ defmodule Enums do
|
|||||||
|
|
||||||
koan "You get as many groups as you can have different results" do
|
koan "You get as many groups as you can have different results" do
|
||||||
assert Enum.group_by([1,2,3,4,5,6], fn element -> rem(element, 3) end) == :__
|
assert Enum.group_by([1,2,3,4,5,6], fn element -> rem(element, 3) end) == :__
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "Zip-up in pairs!" do
|
koan "Zip-up in pairs!" do
|
||||||
@@ -113,15 +115,15 @@ defmodule Enums do
|
|||||||
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], fn element -> rem(element,2) == 0 end) == :__
|
assert Enum.find([1,2,3], &is_even?/1) == :__
|
||||||
end
|
end
|
||||||
|
|
||||||
koan "...but you don't quite find it..." do
|
koan "...but you don't quite find it..." do
|
||||||
assert Enum.find([1,2,3], fn element -> rem(element,5) == 0 end) == :__
|
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, fn element -> rem(element,5) == 0 end) == :__
|
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
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ defmodule KoansHarnessTest do
|
|||||||
[1,2,3,4,5],
|
[1,2,3,4,5],
|
||||||
[1,2,3],
|
[1,2,3],
|
||||||
[1,2,3],
|
[1,2,3],
|
||||||
[1,2,3],
|
[0,1,2,3],
|
||||||
%{ :odd => [3,1], :even => [4,2] },
|
%{ :odd => [3,1], :even => [4,2] },
|
||||||
%{ 0 => [6, 3], 1 => [4, 1], 2 => [5, 2]},
|
%{ 0 => [6, 3], 1 => [4, 1], 2 => [5, 2]},
|
||||||
[{1, :a}, {2, :b}, {3, :c}],
|
[{1, :a}, {2, :b}, {3, :c}],
|
||||||
|
|||||||
Reference in New Issue
Block a user