Skip to content

Latest commit

 

History

History
132 lines (99 loc) · 3.76 KB

polymorphism.livemd

File metadata and controls

132 lines (99 loc) · 3.76 KB

Polymorphism

Mix.install([
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"},
  {:tested_cell, github: "brooklinjazz/tested_cell"},
  {:utils, path: "#{__DIR__}/../utils"}
])

Navigation

Return Home Report An Issue

Setup

Ensure you type the ea keyboard shortcut to evaluate all Elixir cells before starting. Alternatively you can evaluate the Elixir cells as you read.

Polymorphism

The word polymorphism comes from the Greek word for "many shapes". It means essentially the same thing for our programs. We often want to define a single general behavior, but apply it to different things.

  flowchart
    A[Polymorphic Behavior] --- B[Specific Implementation]
    A --- C[Specific Implementation]
    A --- D[Specific Implementation]
    A --- E[Specific Implementation]
    A --- F[Specific Implementation]
Loading

Running is a great real-world example of polymorphism. Many different animals run, and they all run in different ways.

  flowchart
    A[Run] --- B[Cheetahs]
    A --- C[Cats]
    A --- D[Dogs]
    A --- E[Horses]
    A --- F[Humans]
Loading

For a more technical example, you can think of many devices that need to implement an off feature. You can turn off your computer, your tv, your lights, and your phone. Each needs to do this differently under the hood, but the behavior is universal.

  flowchart
    A[off] --- B[TV]
    A --- C[Computer]
    A --- D[Phone]
    A --- E[Lights]
Loading

Anytime you have a universal behavior, but separate implementation depending on some context, it's an opportunity to use polymorphism to improve the clarity of your code.

You've also been using the Enum module which provides polymorphic behavior for different collection data types.

  flowchart
    A[Enum] --- B[map]
    A --- C[list]
    A --- D[keyword-list]
    A --- E[range]
Loading

You have also already learned some methods of achieving polymorphism with multi-clause functions.

defmodule Greeter do
  def hi(name) do
    "Hi #{name}!"
  end

  def hi(name1, name2) do
    "Hi #{name1} and #{name2}!"
  end

  def hi(_name1, _name2, _name3) do
    "Hi everyone!"
  end
end

For example, multiple arity functions could be said to be polymorphic, They create the same generic behavior but with different implementations depending on the number of arguments given.

  flowchart
    A[Greeter.hi] --> B[1 person]
    A --> C[2 people]
    A --> D[3 people]
Loading

Polymorphism can be achieved through many means. You will learn more about the tools available in Elixir to produce behavior as you go through the course.

Your Turn

Think of an example of polymorphic behavior that you encounter in every day life. What is some common abstraction you often use which actually requires different specific behavior?

Running is one example, can you think of others?

Commit Your Progress

Run the following in your command line from the beta_curriculum folder to track and save your progress in a Git commit.

$ git add .
$ git commit -m "finish polymorphism section"

Up Next

Previous Next
Timeline Advanced Pattern Matching