Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle inpure functions with error monad #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
defmodule DungeonCrawl.CLI.BaseCommands do
use Monad.Operators
alias Mix.Shell.IO, as: Shell

import Monad.Result, only: [success: 1, success?: 1, error: 1, return: 1]

def display_options(options) do
options
|> Enum.with_index(1)
|> Enum.each(fn {option, index} ->
Shell.info("#{index} - #{option}")
end)

options
return(options)
end

def generate_question(options) do
Expand All @@ -17,7 +20,40 @@ defmodule DungeonCrawl.CLI.BaseCommands do
end

def parse_answer(answer) do
{option, _} = Integer.parse(answer)
option - 1
case Integer.parse(answer) do
:error -> error("Invalid option")
{option, _} -> success(option - 1)
end
end

def find_option_by_index(index, options) do
case Enum.at(options, index) do
nil -> error("Invalid option")
chosen_option -> success(chosen_option)
end
end

def ask_for_option(options) do
result =
return(options)
~>> (&display_options/1)
~>> (&generate_question/1)
~>> (&Shell.prompt/1)
~>> (&parse_answer/1)
~>> (&find_option_by_index(&1, options))

if success?(result) do
result.value
else
display_error(result.error)
ask_for_option(options)
end
end

def display_error(message) do
Shell.cmd("clear")
Shell.error(message)
Shell.prompt("Press Enter to continue.")
Shell.cmd("clear")
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ defmodule DungeonCrawl.CLI.HeroChoice do
def start do
Shell.cmd("clear")
Shell.info("Start by choosing your hero:")
heroes = DungeonCrawl.Heroes.all()
find_hero_by_index = &Enum.at(heroes, &1)

heroes
|> display_options
|> generate_question
|> Shell.prompt()
|> parse_answer
|> find_hero_by_index.()
DungeonCrawl.Heroes.all()
|> ask_for_option
|> confirm_hero
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,8 @@ defmodule DungeonCrawl.CLI.RoomActionsChoice do
import DungeonCrawl.CLI.BaseCommands

def start(room) do
room_actions = room.actions
find_action_by_index = &Enum.at(room_actions, &1)
Shell.info(room.description())

chosen_action =
room_actions
|> display_options
|> generate_question
|> Shell.prompt()
|> parse_answer
|> find_action_by_index.()

chosen_action = ask_for_option(room.actions)
{room, chosen_action}
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ defmodule DungeonCrawl.MixProject do
# Run "mix help deps" to learn about dependencies.
defp deps do
[
{:dialyxir, "~> 0.5", only: [:dev], runtime: false}
{:dialyxir, "~> 0.5", only: [:dev], runtime: false},
{:monadex, "~> 1.1"}
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
%{
"dialyxir": {:hex, :dialyxir, "0.5.1", "b331b091720fd93e878137add264bac4f644e1ddae07a70bf7062c7862c4b952", [:mix], [], "hexpm"},
"monadex": {:hex, :monadex, "1.1.3", "54e32ce327b728afb8290b45c35137489f8010078dec9fd840a2efc188549795", [:mix], [], "hexpm"},
}