Powered by AppSignal & Oban Pro

Stack Server

exercises/stack_server.livemd

Stack Server

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue Testing GenServersTested Stack

Stack

A stack is a data structure which can push elements onto the top of the stack and pop them off of the top of the stack. We’re going to create a Stack process which stores a stack as a list of elements in its state. We should be able to send a {:push, element} message and a :pop message to add and remove elements from the beginning of the list.

The {:push, element} message should add an element onto the stack, and return the current state.

{:ok, stack_pid} = GenServer.start_link(Stack, [])

GenServer.call(stack_pid, {:push, 1})
[1]

GenServer.call(stack_pid, {:push, 2})
[2, 1]

The :pop message should remove an element from the stack and return the removed element.

2 = GenServer.call(stack_pid, :pop)

[1] = :sys.get_state(stack_pid)

1 = GenServer.call(stack_pid, :pop)

[] = :sys.get_state(stack_pid)

Example Solution

defmodule Stack do
  use GenServer

  def start_link(_opts) do
    GenServer.start_link(__MODULE__, [])
  end

  def push(stack_pid, element) do
    GenServer.call(stack_pid, {:push, element})
  end

  def pop(stack_pid) do
    GenServer.call(stack_pid, :pop)
  end

  def init(_opts) do
    {:ok, []}
  end

  def handle_call({:push, element}, _from, state) do
    new_state = [element | state]
    {:reply, new_state, new_state}
  end

  def handle_call(:pop, _from, state) do
    [head | tail] = state
    {:reply, head, tail}
  end
end

Implement the Stack module below. You do not need to handle :pop-ing an element from an empty stack.

Consider starting with the handle_call/3 callback functions, then implement the client API functions Stack.push/2 and Stack.pop/1.

defmodule Stack do
  use GenServer

  @doc """
  Start the `Stack` process.

  ## Examples

      iex> {:ok, pid} = Stack.start_link([])
  """
  def start_link(_opts) do
  end

  @doc """
  Synchronously push an element onto the top of the `Stack`. Return the current stack.

  ## Examples

      iex> {:ok, pid} = Stack.start_link([])
      iex> Stack.push(pid, 1)
      [1]
      iex> Stack.push(pid, 2)
      [2, 1]
      iex> Stack.push(pid, 3)
      [3, 2, 1]
  """
  def push(stack_pid, element) do
  end

  @doc """
  Pop an element from the top of the `Stack`.

  ## Examples

      iex> {:ok, pid} = Stack.start_link([])
      iex> Stack.push(pid, 1)
      iex> Stack.push(pid, 2)
      iex> Stack.push(pid, 3)
      iex> Stack.pop(pid)
      3
      iex> Stack.pop(pid)
      2
      iex> Stack.pop(pid)
      1
  """
  def pop(stack_pid) do
  end

  @doc """
  Necessary callback function to start the `Stack` process.

  ## Examples

      iex> {:ok, pid} = GenServer.start_link(Stack, []) 
  """
  def init(_opts) do
  end

  @doc """
  Callback function to add an element onto the top of the `Stack`.

  ## Examples

      iex> {:ok, pid} = GenServer.start_link(Stack, [])
      iex> GenServer.call(pid, {:push, 1})
      [1]
      iex> GenServer.call(pid, {:push, 2})
      [2, 1]
      iex> GenServer.call(pid, {:push, 3})
      [3, 2, 1]
  """
  def handle_call({:push, element}, _from, state) do
  end

  @doc """
  Callback function to pop an element off of the top of the `Stack`.
  You do not need to handle popping when a stack is empty.

  ## Examples

      iex> {:ok, pid} = GenServer.start_link(Stack, [])
      iex> GenServer.call(pid, {:push, 1})
      iex> GenServer.call(pid, {:push, 2})
      iex> GenServer.call(pid, {:push, 3})
      iex> GenServer.call(pid, :pop)
      3
      iex> GenServer.call(pid, :pop)
      2
      iex> GenServer.call(pid, :pop)
      1
  """
  def handle_call(:pop, _from, state) do
  end
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Stack Server exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue Testing GenServersTested Stack