Created
February 8, 2018 18:10
-
-
Save stevegrossi/84907dd2aafaaf2e698113cd0915f56a to your computer and use it in GitHub Desktop.
Elixir command parsing
This file contains 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
# 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, []} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.