Last active
February 8, 2016 07:13
-
-
Save ZuraGuerra/de6450d7e2a7b3449121 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 BeerSong do | |
@moduledoc """ | |
This showcases recursion, pattern-matching, multi-line strings, | |
default arguments, embedded docs and pipes. | |
""" | |
import String, only: [downcase: 1] | |
@drink "Take one down and pass it around" | |
@no_beer "Go to the store and buy some more" | |
def sing(beers) when beers < 0, do: :the_end | |
def sing(beers \\ 99) do | |
beers | |
|> song | |
|> lyrics | |
|> IO.puts | |
sing(beers - 1) | |
end | |
defp lyrics({count, next_count, phrase}) do | |
""" | |
#{count} of beer on the wall, #{downcase(count)} of beer. | |
#{phrase}, #{next_count} of beer on the wall. | |
""" | |
end | |
defp song(0), do: {"No more bottles", "99 bottles", @no_beer} | |
defp song(1), do: {"1 bottle", "no more bottles", @drink} | |
defp song(2), do: {"2 bottles", "1 bottle", @drink} | |
defp song(beers), do: {"#{beers} bottles", "#{beers - 1} bottles", @drink} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment