Skip to content

Instantly share code, notes, and snippets.

@seanmor5
Created June 21, 2023 00:32
Show Gist options
  • Save seanmor5/d593f0955fd9ddf36f2fe60e8cef9577 to your computer and use it in GitHub Desktop.
Save seanmor5/d593f0955fd9ddf36f2fe60e8cef9577 to your computer and use it in GitHub Desktop.
main = fn ->
cmd = IO.gets("Enter a command: ") |> String.trim()
case cmd do
"1" ->
todos = Process.get(:todos) || []
IO.puts(Enum.join(todos, "\n"))
"2" ->
todos = Process.get(:todos) || []
new_todo = IO.gets("Enter a todo: ") |> String.trim()
Process.put(:todos, [new_todo | todos])
x when x in ~w(3 4) ->
# cheating
todos = Process.get(:todos) || []
action = if x == "3", do: "complete", else: "delete"
idx = IO.gets("Enter a todo to #{action}: ") |> String.trim()
{idx, _} = Integer.parse(idx)
new_todos = List.delete_at(todos, idx)
Process.put(:todos, new_todos)
"5" ->
Process.exit(self(), :kill)
_ ->
IO.puts("Invalid command")
end
end
Stream.repeatedly(fn -> main.() end) |> Stream.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment