Run this notebook

Use Livebook to open this notebook and explore new ideas.

It is easy to get started, on your machine or the cloud.

Click below to open and run it in your Livebook at .

(or change your Livebook location)

# Build FunPark: Model Real-World Data ```elixir Mix.install([ {:fun_park, git: "https://github.com/JKWA/funpark_notebooks.git", branch: "main" } ]) ``` ## Advanced Functional Programming with Elixir | | | | -------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | <img src="https://www.joekoski.com/assets/images/jkelixir_small.jpg" alt="Book cover" width="120"> | **Interactive Examples from Chapter 1**<br/>[Advanced Functional Programming with Elixir](https://pragprog.com/titles/jkelixir/advanced-functional-programming-with-elixir). | ## Define the Ride Model Rides have core attributes: unique ID, name, height/age requirements, and can go offline. They also have experience tags like `:dark`, `:thrill`, or `:water`. ````markdown ```elixir defmodule FunPark.Ride do defstruct id: nil, name: "Unknown Ride", min_age: 0, min_height: 0, wait_time: 0, online: true, tags: [] def make(name, opts \\ []) when is_binary(name) do %__MODULE__{ id: :erlang.unique_integer([:positive]), name: name, min_age: Keyword.get(opts, :min_age, 0), min_height: Keyword.get(opts, :min_height, 0), wait_time: Keyword.get(opts, :wait_time, 0), online: Keyword.get(opts, :online, true), tags: Keyword.get(opts, :tags, []) } end end ``` ```` Create a family-friendly ride: ```elixir FunPark.Ride.make("Tea Cup", wait_time: 10, tags: [:family_friendly]) ``` Create a more restricted ride: ```elixir dark_mansion = FunPark.Ride.make( "Dark Mansion", min_age: 14, tags: [:dark] ) ``` ## Implement Fast Passes for Priority Access FastPasses manage demand for popular rides. Each pass is tied to a specific ride and time. ````markdown ```elixir defmodule FunPark.FastPass do alias FunPark.Ride defstruct id: nil, ride: nil, time: nil def make(%Ride{} = ride, %DateTime{} = time) do %__MODULE__{ id: :erlang.unique_integer([:positive]), ride: ride, time: time } end end ``` ```` Create a time: ```elixir datetime = DateTime.new!(~D[2025-06-01], ~T[13:00:00]) ``` Create a fast pass for the Dark Mansion: ```elixir fast_pass = FunPark.FastPass.make(dark_mansion, datetime) ``` ## Model the Patrons These are called "patrons". Patrons have personal attributes, ticket tiers, fast passes, reward points, and preferences. ````markdown ```elixir defmodule FunPark.Patron do defstruct id: nil, name: nil, age: 0, height: 0, ticket_tier: :basic, fast_passes: [], reward_points: 0, likes: [], dislikes: [] def make(name, age, height, opts \\ []) when is_bitstring(name) and is_integer(age) and is_integer(height) and age > 0 and height > 0 do %__MODULE__{ id: :erlang.unique_integer([:positive]), name: name, age: age, height: height, ticket_tier: Keyword.get(opts, :ticket_tier, :basic), fast_passes: Keyword.get(opts, :fast_passes, []), reward_points: Keyword.get(opts, :reward_points, 0), likes: Keyword.get(opts, :likes, []), dislikes: Keyword.get(opts, :dislikes, []) } end end ``` ```` Create a patron named Alice with a FastPass: ```elixir FunPark.Patron.make("Alice", 15, 120, fast_passes: [fast_pass]) ```
See source

Have you already installed Livebook?

If you already installed Livebook, you can configure the default Livebook location where you want to open notebooks.
Livebook up Checking status We can't reach this Livebook (but we saved your preference anyway)
Run notebook

Not yet? Install Livebook in just a minute

Livebook is open source, free, and ready to run anywhere.

Run in the cloud

on select platforms

To run on Linux, Docker, embedded devices, or Elixir’s Mix, check our README.

PLATINUM SPONSORS
SPONSORS
Code navigation with go to definition of modules and functions Read More