The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

Mike Over (2) [Avatar] Offline
#1
Listing 1.1 code is incorrect.


defmodule RaceCar do
  defstruct [:tires, :power, :acceleration, :speed]

  def accelerate(%RaceCar{speed: speed, amount: amount} = racecar) do
    Map.put(racecar, :speed, speed + amount)
  end
end

ferrari_tires = [
  %Tire{location: :front_right, kind: :racing},
  %Tire{location: :front_left, kind: :racing},
  %Tire{location: :back_right, kind: :racing},
  %Tire{location: :back_left, kind: :racing}
]
ferrari = %RaceCar{tires: ferrari_tires,
                   power: %Engine{model: "FR223"},
                   acceleration: 60,
                   speed: 0}
ferrari.speed
# => 0
RaceCar.accelerate(ferrari)
# => 60
ferrari.speed
# => 0
new_ferrari = RaceCar.accelerate(ferrari)
new_ferrari.speed
# => 60
ferrari.speed
# => 0


The accelerate function matches to a Racecar that has an "amount" key but there is no such field defined in the struct. I think what you want to do instead is:
def accelerate(%RaceCar{speed: speed, acceleration: acceleration} = racecar) do
  Map.put(racecar, :speed, speed + acceleration)
end

Thorsten Deinert (8) [Avatar] Offline
#2
I just wonder about why using Map.put/3 over
%RaceCar{racecar | speed: speed + acceleration}

in that example.
Geoffrey Lessel (8) [Avatar] Offline
#3
Mike Over wrote:Listing 1.1 code is incorrect.


defmodule RaceCar do
  defstruct [:tires, :power, :acceleration, :speed]

  def accelerate(%RaceCar{speed: speed, amount: amount} = racecar) do
    Map.put(racecar, :speed, speed + amount)
  end
end

ferrari_tires = [
  %Tire{location: :front_right, kind: :racing},
  %Tire{location: :front_left, kind: :racing},
  %Tire{location: :back_right, kind: :racing},
  %Tire{location: :back_left, kind: :racing}
]
ferrari = %RaceCar{tires: ferrari_tires,
                   power: %Engine{model: "FR223"},
                   acceleration: 60,
                   speed: 0}
ferrari.speed
# => 0
RaceCar.accelerate(ferrari)
# => 60
ferrari.speed
# => 0
new_ferrari = RaceCar.accelerate(ferrari)
new_ferrari.speed
# => 60
ferrari.speed
# => 0


The accelerate function matches to a Racecar that has an "amount" key but there is no such field defined in the struct. I think what you want to do instead is:
def accelerate(%RaceCar{speed: speed, acceleration: acceleration} = racecar) do
  Map.put(racecar, :speed, speed + acceleration)
end



Thanks and good catch. I've updated the base text substituting acceleration for amount.
Geoffrey Lessel (8) [Avatar] Offline
#4
Thorsten Deinert wrote:I just wonder about why using Map.put/3 over
%RaceCar{racecar | speed: speed + acceleration}

in that example.


In this early introduction to Elixir syntax, I decided to not use special syntax like that and instead rely mostly on functions. This also helps to show the functional nature of Elixir.