small medium large xlarge

Generic-user-small
14 Nov 2017, 16:41
Ilyes (1 post)

@pragprog: First of why does the (sub)forum not have a search functionality?

Hi,

I am reading and writing along with the code when I noticed the fallowing in Part 1 Chapter 2 Model Data and Behavior Sub-chapter Transforming Data (Page 40 of the B6.0 pdf version of the book). I think it can be refactored from:

Original:

  def guess(%Island{} = island, %Coordinate{} = coordinate) do
    case MapSet.member?(island.coordinates, coordinate) do
      true ->
        hit_coordinates = MapSet.put(island.hit_coordinates, coordinate)
        {:hit, %{island | hit_coordinates: hit_coordinates}}

      false ->
        :miss
    end
  end

To:

  def guess(%Island{} = island, %Coordinate{} = coordinate) do
    case MapSet.member?(island.coordinates, coordinate) do
      true ->
        {:hit, update_in(island.hit_coordinates, &MapSet.put(&1, coordinate))}

      false ->
        :miss
    end
  end

What do you guys think?

(Why not use CommonMark/Markdown? Yet another format -.-)

You must be logged in to comment