9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ElixirでFizzBuzz書いてみた

Posted at

Elixirに入門したのでFizzBuzzを書いてみました。書き方は色々あると思うので色んなパターンを見てみたいですね!

fizzbuzz.ex
defmodule FizzBuzz do
  def fizz_buzz(n) do
    Enum.map_join 1..n, fn x ->
      cond do
        rem(x, 15) === 0 ->
          "FizzBuzz"
        rem(x, 3) === 0 ->
          "Fizz"
        rem(x, 5) === 0 ->
          "Buzz"
        true ->
          x
      end
    end
  end
end

IO.inspect FizzBuzz.fizz_buzz(30)
> mix run fizzbuzz.ex
"12Fizz4BuzzFizz78FizzBuzz11Fizz1314FizzBuzz1617Fizz19BuzzFizz2223FizzBuzz26Fizz2829FizzBuzz"
9
8
6

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?