Created
June 23, 2015 09:54
-
-
Save hubertlepicki/4017a2fbb576cc678f93 to your computer and use it in GitHub Desktop.
Elixir pattern matching makes you stop using if-clauses or exceptions
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
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