59
lib/koans/08_structs.ex
Normal file
59
lib/koans/08_structs.ex
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
defmodule Structs do
|
||||||
|
use Koans
|
||||||
|
|
||||||
|
defmodule Person do
|
||||||
|
defstruct [:name, :age ]
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "structs are defined and named after a module" do
|
||||||
|
assert %Person{}
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "you can access the fields of a struct" do
|
||||||
|
nobody = %Person{}
|
||||||
|
assert nobody.age == nil
|
||||||
|
assert nobody.name == nil
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "You can pass initial values to structs" do
|
||||||
|
joe = %Person{name: "Joe", age: 23}
|
||||||
|
assert joe.age == 23
|
||||||
|
assert joe.name == "Joe"
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "update fields with the pipe '|' operator" do
|
||||||
|
joe = %Person{name: "Joe", age: 23}
|
||||||
|
older = %{ joe | age: joe.age + 10}
|
||||||
|
assert older.age == 33
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "the original struct is not affected by updates" do
|
||||||
|
joe = %Person{name: "Joe", age: 23}
|
||||||
|
assert %{ joe | age: joe.age + 10}.age == 33
|
||||||
|
assert joe.age == 23
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "you can pattern match into the fields of a struct" do
|
||||||
|
%Person{age: age} = %Person{age: 22, name: "Silvia"}
|
||||||
|
assert age == 22
|
||||||
|
end
|
||||||
|
|
||||||
|
defmodule Plane do
|
||||||
|
defstruct passengers: 0, maker: :boeing
|
||||||
|
end
|
||||||
|
|
||||||
|
koan "or onto the type of the struct itself" do
|
||||||
|
assert is_a_plane(%Plane{passengers: 417, maker: :boeing})
|
||||||
|
refute is_a_plane(%Person{age: 22, name: "Silvia"})
|
||||||
|
end
|
||||||
|
|
||||||
|
def is_a_plane(%Plane{}), do: true
|
||||||
|
def is_a_plane(_), do: false
|
||||||
|
|
||||||
|
koan "are basically maps" do
|
||||||
|
silvia = %Person{age: 22, name: "Silvia"}
|
||||||
|
|
||||||
|
assert is_map(silvia)
|
||||||
|
assert Map.fetch!(silvia, :age) == 22
|
||||||
|
end
|
||||||
|
end
|
@@ -6,7 +6,8 @@ defmodule Runner do
|
|||||||
Maps,
|
Maps,
|
||||||
Functions,
|
Functions,
|
||||||
Enums,
|
Enums,
|
||||||
Arithmetic
|
Arithmetic,
|
||||||
|
Structs
|
||||||
]
|
]
|
||||||
|
|
||||||
def run(options) do
|
def run(options) do
|
||||||
|
@@ -4,7 +4,7 @@ defmodule Watcher do
|
|||||||
def callback(file, events) do
|
def callback(file, events) do
|
||||||
if Enum.member?(events, :modified) do
|
if Enum.member?(events, :modified) do
|
||||||
try do
|
try do
|
||||||
[{mod, _}] = Code.load_file(file)
|
[{mod, _} | _] = Code.load_file(file)
|
||||||
Runner.run(mod)
|
Runner.run(mod)
|
||||||
rescue
|
rescue
|
||||||
e -> Display.format_compile_error(e)
|
e -> Display.format_compile_error(e)
|
||||||
|
Reference in New Issue
Block a user