Skip to content

Instantly share code, notes, and snippets.

@hubertlepicki
Created June 23, 2015 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hubertlepicki/4017a2fbb576cc678f93 to your computer and use it in GitHub Desktop.
Save hubertlepicki/4017a2fbb576cc678f93 to your computer and use it in GitHub Desktop.
Elixir pattern matching makes you stop using if-clauses or exceptions
defmodule Files do
def run do
process_file File.read("hello.txt")
end
def process_file({:ok, contents}) do
IO.puts "File contents are here:"
IO.puts contents
end
def process_file({:error, _}) do
IO.puts "Could not open file :("
end
end
Files.run
# Run it:
# $ elixir files.exs
# Could not open file :(
# $ touch hello.txt
# elixir files.exs
# File contents are here:
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment