Files
elixir-koans/lib/koans/10_structs.ex
Tim Jarratt 00eb17014a Move sigils to later in the lessons
While doing this, I also discovered that
there is also a reference to lists in numbers, but
that might a bit easier for someone to grasp, given
the hint that the koan gives, and the output they
see when they run it.
2017-04-25 22:38:49 +02:00

68 lines
1.7 KiB
Elixir

defmodule Structs do
use Koans
@intro "Structs"
defmodule Person do
defstruct [:name, :age]
end
koan "Structs are defined and named after a module" do
person = %Person{}
assert person == ___
end
koan "Unless previously defined, fields begin as nil" do
nobody = %Person{}
assert nobody.age == ___
end
koan "You can pass initial values to structs" do
joe = %Person{name: "Joe", age: 23}
assert joe.name == ___
end
koan "Update fields with the cons '|' operator" do
joe = %Person{name: "Joe", age: 23}
older = %{joe | age: joe.age + 10}
assert older.age == ___
end
defmodule Plane do
defstruct passengers: 0, maker: :boeing
end
def plane?(%Plane{}), do: true
def plane?(_), do: false
koan "Or onto the type of the struct itself" do
assert plane?(%Plane{passengers: 417, maker: :boeing}) == ___
assert plane?(%Person{}) == ___
end
koan "Struct can be treated like maps" do
silvia = %Person{age: 22, name: "Silvia"}
assert Map.fetch(silvia, :age) == ___
end
defmodule Airline do
defstruct plane: %Plane{}, name: "Southwest"
end
koan "Use the put_in macro to replace a nested value" do
airline = %Airline{plane: %Plane{maker: :boeing}}
assert put_in(airline.plane.maker, :airbus) == ___
end
koan "Use the update_in macro to modify a nested value" do
airline = %Airline{plane: %Plane{maker: :boeing, passengers: 200}}
assert update_in(airline.plane.passengers, &(&1 + 2)) == ___
end
koan "Use the put_in macro with atoms to replace a nested value in a non-struct" do
airline = %{plane: %{maker: :boeing}, name: "Southwest"}
assert put_in(airline[:plane][:maker], :cessna) == ___
end
end