Skip to content

Instantly share code, notes, and snippets.

@polvalente
Created June 21, 2023 00:21
Show Gist options
  • Save polvalente/041a70db000f18a4434c1b8166eeab83 to your computer and use it in GitHub Desktop.
Save polvalente/041a70db000f18a4434c1b8166eeab83 to your computer and use it in GitHub Desktop.
code-golf with error handling solution
# To-Do app
run = fn run_fn, state ->
case IO.gets("Choose one of the following options:\n1. List TO-DOs\n2. Add a new TO-DO\n3. Mark TO-DO as completed\n4. Delete TO-DO.\n5. Exit\n>> ") |> String.trim() do
"1" ->
state.to_dos
|> Enum.reverse()
|> Enum.with_index()
|> Enum.map_join("\n", fn {%{todo: todo, completed: completed}, idx} ->
"#{idx}. #{todo} [#{if(completed, do: "✔", else: " ")}]"
end)
|> IO.puts()
state
"2" ->
todo = IO.gets("Insert your new to-do\n>> ") |> String.trim()
%{state | to_dos: [%{todo: todo, completed: false} | state.to_dos], count: state.count + 1}
"3" ->
try do
{todo_idx, _} = IO.gets("Insert the index for the to-do to be completed\n>> ") |> Integer.parse()
idx = state.count - todo_idx - 1
update_in(state, [:to_dos, Access.at(idx)], &%{&1 | completed: true})
rescue
_ ->
IO.warn("Failed to complete to-do.")
state
end
"4" ->
try do
{todo_idx, _} = IO.gets("Insert the index for the to-do to be deleted\n>> ") |> Integer.parse()
idx = state.count - todo_idx - 1
%{state | to_dos: List.delete_at(state.to_dos, idx), count: state.count - 1}
rescue
_ ->
IO.warn("Failed to delete to-do.")
state
end
"5" ->
IO.puts("Exiting")
:exit
cmd ->
IO.warn("Invalid command #{inspect(cmd)}")
state
end
|> then(fn
:exit ->
:exit
state ->
run_fn.(run_fn, state)
end)
end
run.(run, %{count: 0, to_dos: []})

Output:

Choose one of the following options:

  1. List TO-DOs
  2. Add a new TO-DO
  3. Mark TO-DO as completed
  4. Delete TO-DO.
  5. Exit

2 Insert your new to-do a Choose one of the following options:

  1. List TO-DOs
  2. Add a new TO-DO
  3. Mark TO-DO as completed
  4. Delete TO-DO.
  5. Exit

2 Insert your new to-do b Choose one of the following options:

  1. List TO-DOs
  2. Add a new TO-DO
  3. Mark TO-DO as completed
  4. Delete TO-DO.
  5. Exit

2 Insert your new to-do c Choose one of the following options:

  1. List TO-DOs
  2. Add a new TO-DO
  3. Mark TO-DO as completed
  4. Delete TO-DO.
  5. Exit

1

  1. a [ ]
  2. b [ ]
  3. c [ ] Choose one of the following options:
  4. List TO-DOs
  5. Add a new TO-DO
  6. Mark TO-DO as completed
  7. Delete TO-DO.
  8. Exit

3 Insert the index for the to-do to be completed 2 Choose one of the following options:

  1. List TO-DOs
  2. Add a new TO-DO
  3. Mark TO-DO as completed
  4. Delete TO-DO.
  5. Exit

1

  1. a [ ]
  2. b [ ]
  3. c [✔] Choose one of the following options:
  4. List TO-DOs
  5. Add a new TO-DO
  6. Mark TO-DO as completed
  7. Delete TO-DO.
  8. Exit

3 Insert the index for the to-do to be completed 1 Choose one of the following options:

  1. List TO-DOs
  2. Add a new TO-DO
  3. Mark TO-DO as completed
  4. Delete TO-DO.
  5. Exit

1

  1. a [ ]
  2. b [✔]
  3. c [✔] Choose one of the following options:
  4. List TO-DOs
  5. Add a new TO-DO
  6. Mark TO-DO as completed
  7. Delete TO-DO.
  8. Exit

4 Insert the index for the to-do to be deleted 1 Choose one of the following options:

  1. List TO-DOs
  2. Add a new TO-DO
  3. Mark TO-DO as completed
  4. Delete TO-DO.
  5. Exit

1

  1. a [ ]
  2. c [✔] Choose one of the following options:
  3. List TO-DOs
  4. Add a new TO-DO
  5. Mark TO-DO as completed
  6. Delete TO-DO.
  7. Exit

2 Insert your new to-do d Choose one of the following options:

  1. List TO-DOs
  2. Add a new TO-DO
  3. Mark TO-DO as completed
  4. Delete TO-DO.
  5. Exit

1

  1. a [ ]
  2. c [✔]
  3. d [ ] Choose one of the following options:
  4. List TO-DOs
  5. Add a new TO-DO
  6. Mark TO-DO as completed
  7. Delete TO-DO.
  8. Exit

5 Exiting :exit

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