Skip to content

Instantly share code, notes, and snippets.

@stevegrossi
Created February 8, 2018 18:10
Show Gist options
  • Save stevegrossi/84907dd2aafaaf2e698113cd0915f56a to your computer and use it in GitHub Desktop.
Save stevegrossi/84907dd2aafaaf2e698113cd0915f56a to your computer and use it in GitHub Desktop.
Elixir command parsing
# I've been wondering about a pattern-matching-driven approach to what we've been talking about, e.g.
def parse_command(input) do
input
|> String.strip
|> match_command
end
def match_command("north"), do: {Player, :move, [:north, player, world]}
def match_command("east"), do: {Player, :move, [:east, player, world]}
def match_command("west"), do: {Player, :move, [:west, player, world]}
# ...
def match_command("quit"), do: {Game, :quit, []}
def match_command(_), do: {Interface, :unknown_command, []}
@yakryder
Copy link

For some reason I'm resistant. This would instantly solve some problems for me, but it's hard to get past the desire to keep the command library decoupled from the caller.

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