This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defprotocol Add do | |
| def calculate(a, b) | |
| end | |
| # Now we can define implementations of it for the various kinds of | |
| # data types we might get inside our collections that we are zipping. | |
| defimpl Add, for: Integer do | |
| def calculate(a, b) when is_integer(b), do: a + b | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defprotocol Zip do | |
| def apply(collection_1, collection_2, operation) | |
| end | |
| # We can implement it for lists: | |
| defimpl Zip, for: List do | |
| def apply(a, b, calculate) do | |
| Enum.zip(a, b) | |
| |> Enum.map(fn {a, b} -> calculate.(a, b) end) | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| list_1 = [1, 2] | |
| list_2 = [3, 4] | |
| Zip.apply(list_1, list_2, fn x, y -> x + y end) #=> [4, 6] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Test do | |
| def thing() do | |
| 1+1 | |
| end | |
| defmacro my_macro do | |
| quote do | |
| 1 + 1 | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| response = %{"meat_type" => "medium rare", "pickles" => false, "collection_date" => "2019-11-04"} | |
| # We want a struct so that we can do things like implement protocols for it | |
| defmodule SteamedHam do | |
| defstruct [:meat_type, :pickles, :collection_date] | |
| end | |
| # With no !, the struct function selects only the fields defined in the schema. | |
| steamed_ham = struct(SteamedHam, response) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule AmazeOn do | |
| defstruct [price: 10] | |
| end | |
| defmodule Area do | |
| defstruct [] | |
| end | |
| defmodule Square do | |
| defstruct [:side] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Perimeter do | |
| defstruct [] | |
| end | |
| defprotocol PerimeterProtocol do | |
| def calculate(shape) | |
| end | |
| defimpl Shape, for: Perimeter do | |
| def calculate(%Perimeter{}, shape) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Circle do | |
| defstruct [:radius] | |
| end | |
| defimpl AreaProtocol, for: Circle do | |
| def calculate(%Circle{radius: radius}) do | |
| 3.14 * radius * radius | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Square do | |
| defstruct [:side] | |
| end | |
| defmodule Area do | |
| defstruct [] | |
| end | |
| defprotocol Shape do | |
| def calculate(calculation, shape) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Shape.calculate(:area, %Square{side: 10}) |