Last active
December 18, 2016 20:14
-
-
Save scottmascio2115/74b6f67d299a44b61b34941ee26fdaeb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Todo do | |
use GenServer | |
## Client side code | |
def start(tasks \\ "") do | |
{:ok, pid} = GenServer.start(__MODULE__, tasks) | |
pid | |
end | |
def add_task(pid, task) do | |
GenServer.cast(pid, {:add, task}) | |
end | |
def remove_task(pid, task) do | |
GenServer.cast(pid, {:remove, task}) | |
end | |
def all_tasks(pid) do | |
GenServer.call(pid, :all) | |
end | |
## Server side code: | |
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 | |
# pid = Todo.start | |
# Todo.add_task(pid, "Make eggs") | |
# Todo.all_tasks(pid) | |
## TODO | |
# Ask user for what it wants to do. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've seen better code from a 3 year old.