Last active
December 20, 2016 14:42
-
-
Save kana-sama/63a40b00a1565de919db690e6767c109 to your computer and use it in GitHub Desktop.
FizzBuzz. Try elm there: http://elm-lang.org/try
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 | |
defp fizz({i, r} = line) do | |
if rem(i, 3) == 0 do | |
{i, r <> "Fizz"} | |
else | |
line | |
end | |
end | |
defp buzz({i, r} = line) do | |
if rem(i, 5) == 0 do | |
{i, r <> "Buzz"} | |
else | |
line | |
end | |
end | |
defp number({i, r} = line) do | |
if r === "" do | |
{i, Integer.to_string i} | |
else | |
line | |
end | |
end | |
def fizz_buzz(list) do | |
list | |
|> Enum.map(fn i -> {i, ""} end) | |
|> Enum.map(&fizz/1) | |
|> Enum.map(&buzz/1) | |
|> Enum.map(&number/1) | |
|> Enum.map(fn {_, s} -> s end) | |
end | |
end | |
1..100 | |
|> FizzBuzz.fizz_buzz | |
|> IO.inspect |
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
import Html exposing (ul, li, text) | |
fizzBuzz list = | |
let | |
fizz ( i, r ) = | |
if i % 3 == 0 then | |
( i, r ++ "Fizz" ) | |
else | |
( i, r ) | |
buzz ( i, r ) = | |
if i % 5 == 0 then | |
( i, r ++ "Buzz" ) | |
else | |
( i, r ) | |
number ( i, r ) = | |
if String.isEmpty r then | |
( i, toString i ) | |
else | |
( i, r ) | |
in | |
list | |
|> List.map (\i -> ( i, "" )) | |
|> List.map fizz | |
|> List.map buzz | |
|> List.map number | |
|> List.map Tuple.second | |
main = | |
let | |
lines = | |
List.range 1 100 | |
|> fizzBuzz | |
|> List.map (\i -> li [] [ text i ]) | |
in | |
ul [] lines |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment