Skip to content

Instantly share code, notes, and snippets.

@scottmascio2115
Last active December 18, 2016 20:14
Show Gist options
  • Select an option

  • Save scottmascio2115/74b6f67d299a44b61b34941ee26fdaeb to your computer and use it in GitHub Desktop.

Select an option

Save scottmascio2115/74b6f67d299a44b61b34941ee26fdaeb to your computer and use it in GitHub Desktop.
defmodule Todo do
use GenServer
def init(tasks // "") do
{:ok, tasks}
end
def handle_cast({:add, task}, tasks) do
{:noreply, tasks <> " | " <> task}
end
def handle_cast({:remove, task}, tasks) do
{:noreply, tasks |> String.replace("| #{task}", "")}
end
def handle_call(:all, _from, tasks) do
{:reply, tasks, tasks}
end
end
# {:ok, pid} = GenServer.start(Todo, "foo")
# tasks = GenServer.call(pid, :all)
# GenServer.cast(pid, {:add, "bar"})
# GenServer.cast(pid, {:remove, "bar"})
## TODO
# Encapsolate the GenServer logic in the module
@rsnorman
Copy link

I've seen better code from a 3 year old.

@scottmascio2115
Copy link
Author

@rsnorman hahaah just seeing this ^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment