15 Jul 2013, 03:42
Dave_gnome_head_isolated_pragsmall

Dave Thomas (337 posts)

  • Extend your stack server with a push interface which adds a single value to the top of the stack. This will be implemented as a cast.

    Experiment in iex with pushing and popping values.

25 Aug 2013, 20:21
Generic-user-small

Steve Downey (2 posts)

defmodule MyStack.Stack do
  use GenServer.Behaviour      

  def init(list)           
  when is_list(list) do
    { :ok, list }
  end

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

  def handle_cast({:push, new_head}, list) do
    { :noreply, [new_head | list]}
  end
  
end
  You must be logged in to comment