Last active
August 29, 2015 14:24
-
-
Save Dania02525/3559821dffbc7d88d46c to your computer and use it in GitHub Desktop.
Elixir fizzbuzz pattern matching
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 Fizzbuzz do | |
| def fizzbuzzer(n) do | |
| case {rem(n, 3), rem(n, 5), n} do | |
| {0, 0, _} -> | |
| "FizzBuzz" | |
| {0, _, _} -> | |
| "Fizz" | |
| {_, 0, _} -> | |
| "Buzz" | |
| {_, _, n} -> | |
| n | |
| end | |
| end | |
| def fizzbuzz(range) do | |
| if Range.range?(range) do | |
| range | |
| |> Enum.map(fn(n) -> fizzbuzzer(n) end) | |
| |> Enum.each(fn(x) -> IO.puts(x) end) | |
| else | |
| IO.puts("first parameter must be a range!") | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment