Created
July 29, 2014 02:59
-
-
Save mutablestate/86193c677213ddb528b3 to your computer and use it in GitHub Desktop.
FizzBuzz implementation in Elixir using cond
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 ElixirBuzz do | |
| @moduledoc "It implements a fizzbuzz solution with cond" | |
| @doc """ | |
| Returns a range from 1 to the given number in as list substituting a number for a pre-defined string when divisible by 3, 5, or 15 (3 and 5) | |
| """ | |
| def buzz(number) do | |
| Enum.map(1..number, fn n -> | |
| cond do | |
| rem(n, 15) == 0 -> "ElixirBuzz" | |
| rem(n, 3) == 0 -> "Elixir" | |
| rem(n, 5) == 0 -> "Buzz" | |
| true -> n | |
| end | |
| end) | |
| end | |
| end | |
| @doc """ | |
| iex> c "elixir_buzz.exs" | |
| iex> ElixirBuzz.buzz(50) | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment