Skip to content

Instantly share code, notes, and snippets.

@wende
Last active May 31, 2017 16:50
Show Gist options
  • Save wende/5eb84f28ebf809e1ceb90adf9f8ac81d to your computer and use it in GitHub Desktop.
Save wende/5eb84f28ebf809e1ceb90adf9f8ac81d to your computer and use it in GitHub Desktop.
Elmchemy article #3
defmodule FizzBuzz do
use Elmchemy
import XList, only: [{:'range', 0},{:'map', 0}]
@doc """
Fizzes the buzzes and buzzfizzes the fizz out of buzz
iex> import FizzBuzz
iex> fizzbuzz.(1).(7)
"1 2 Fizz 4 Buzz Fizz 7"
"""
@spec fizzbuzz() :: (integer -> (integer -> String.t))
@spec fizzbuzz(integer, integer) :: String.t
curry fizzbuzz/2
def fizzbuzz(from, to) do
fizz_buzz = fn(n) -> case {rem(n, 3), rem(n, 5)} do
{0, 0} -> "FizzBuzz"
{0, _} -> "Fizz"
{_, 0} -> "Buzz"
_ -> to_string.(n)
end end
XList.range.(from).(to)
|> (map.(fizz_buzz >>> to_string)).()
|> (join_words).()
end
@spec join_words() :: (list(String.t) -> String.t)
@spec join_words(list(String.t)) :: String.t
defp join_words(list) do
XString.join.(" ").(list)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment