Answers for Chapter 5, Using Higher-Order Functions

  • The EnchanterShop module should look like this:

     defmodule​ EnchanterShop ​do
     def​ test_data ​do
      [
      %{​title:​ ​"​​Longsword"​, ​price:​ 50, ​magic:​ false},
      %{​title:​ ​"​​Healing Potion"​, ​price:​ 60, ​magic:​ true},
      %{​title:​ ​"​​Rope"​, ​price:​ 10, ​magic:​ false},
      %{​title:​ ​"​​Dragon's Spear"​, ​price:​ 100, ​magic:​ true},
      ]
     end
      @enchanter_name ​"​​Edwin"
     
     def​ enchant_for_sale(items) ​do
      Enum.map(items, &transform/1)
     end
     
     defp​ transform(item = %{​magic:​ true}), ​do​: item
     defp​ transform(item) ​do
      %{
     title:​ ​"​​#{​@enchanter_name​}​​'s ​​#{​item.title​}​​"​,
     price:​ item.price * 3,

Get Learn Functional Programming with Elixir now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.