Modify lists and maps style

This commit is contained in:
fabien townsend
2016-04-24 10:18:57 +02:00
parent c60f7c0033
commit ca1bd36322
6 changed files with 27 additions and 27 deletions

View File

@@ -37,7 +37,7 @@ defmodule Maps do
end end
koan "Or you can use some syntactic sugar for existing elements" do koan "Or you can use some syntactic sugar for existing elements" do
younger_person = %{ @person | age: 16 } younger_person = %{@person | age: 16}
assert Map.fetch(younger_person, :age) == ___ assert Map.fetch(younger_person, :age) == ___
end end
@@ -51,12 +51,12 @@ defmodule Maps do
end end
koan "When merging, the last map wins" do koan "When merging, the last map wins" do
merged = Map.merge(@person, %{ last_name: "Baratheon"}) merged = Map.merge(@person, %{last_name: "Baratheon"})
assert Map.fetch(merged, :last_name) == ___ assert Map.fetch(merged, :last_name) == ___
end end
koan "You can also select sub-maps out of a larger map" do koan "You can also select sub-maps out of a larger map" do
initial = %{ name: "Jon", last_name: "Snow", age: 15} initial = %{name: "Jon", last_name: "Snow", age: 15}
assert Map.take(initial, [:name, :last_name]) == ___ assert Map.take(initial, [:name, :last_name]) == ___
end end
end end

View File

@@ -2,7 +2,7 @@ defmodule Structs do
use Koans use Koans
defmodule Person do defmodule Person do
defstruct [:name, :age ] defstruct [:name, :age]
end end
koan "Structs are defined and named after a module" do koan "Structs are defined and named after a module" do
@@ -22,7 +22,7 @@ defmodule Structs do
koan "Update fields with the pipe '|' operator" do koan "Update fields with the pipe '|' operator" do
joe = %Person{name: "Joe", age: 23} joe = %Person{name: "Joe", age: 23}
older = %{ joe | age: joe.age + 10} older = %{joe | age: joe.age + 10}
assert older.age == ___ assert older.age == ___
end end

View File

@@ -16,7 +16,7 @@ defmodule PatternMatching do
end end
koan "Patterns can be used to pull things apart" do koan "Patterns can be used to pull things apart" do
[head | tail] = [1,2,3,4] [head | tail] = [1, 2, 3, 4]
assert head == ___ assert head == ___
assert tail == ___ assert tail == ___
@@ -24,13 +24,13 @@ defmodule PatternMatching do
koan "And then put them back together" do koan "And then put them back together" do
head = 1 head = 1
tail = [2,3,4] tail = [2, 3, 4]
assert ___ == [head | tail] assert ___ == [head | tail]
end end
koan "Some values can be ignored" do koan "Some values can be ignored" do
[_first, _second, third, _fourth] = [1,2,3,4] [_first, _second, third, _fourth] = [1, 2, 3, 4]
assert third == ___ assert third == ___
end end

View File

@@ -35,7 +35,7 @@ defmodule Functions do
def sum_up(_thing), do: :single_thing def sum_up(_thing), do: :single_thing
koan "Functions can be picky and apply only to certain types" do koan "Functions can be picky and apply only to certain types" do
assert sum_up([1,2,3]) == ___ assert sum_up([1 ,2 ,3]) == ___
assert sum_up(1) == ___ assert sum_up(1) == ___
end end

View File

@@ -2,7 +2,7 @@ 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
@@ -11,69 +11,69 @@ defmodule Enums do
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?([6,7,8,9], &less_than_five?/1) == ___ assert Enum.all?([6 ,7 ,8 ,9], &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
input = [1,2,3] input = [1 ,2 ,3]
assert Enum.member?(input, 1) == ___ assert Enum.member?(input, 1) == ___
assert Enum.member?(input, 30) == ___ assert Enum.member?(input, 30) == ___
end end
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], &even?/1) == ___ assert Enum.find([1 ,2 ,3], &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

View File

@@ -37,7 +37,7 @@ defmodule Tasks do
end end
koan "You can yield to multiple tasks at once and extract the results" do koan "You can yield to multiple tasks at once and extract the results" do
squares = [1,2,3,4] squares = [1 ,2 ,3 ,4]
|> Enum.map(fn(number) -> Task.async(fn -> number * number end) end) |> Enum.map(fn(number) -> Task.async(fn -> number * number end) end)
|> Task.yield_many(100) |> Task.yield_many(100)
|> Enum.map(fn({_task,{:ok, result}}) -> result end) |> Enum.map(fn({_task,{:ok, result}}) -> result end)