0
0

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 1 year has passed since last update.

多言語FizzBuzzチャレンジ15日目:Elixir

Last updated at Posted at 2022-12-14

これまでのまとめ

本日のお品書き

Elixirさん。名前がときめきますね…。
F#の時も思いましたが、関数型言語に不慣れすぎてちょっと身構えてしまいますね。

FizzBuzz

fizzbuzz.exs
# 匿名関数をfizzbuzzに割り当てている
fizzbuzz = fn(x) ->
  # switchやcaseに相当する。もっと上手なやりかたもありそうだが…。
  cond do
    # 剰余の計算は%ではなくrem
    rem(x, 15) == 0 -> IO.puts "FizzBuzz"
    rem(x, 3) == 0 -> IO.puts "Fizz"
    rem(x, 5) == 0 -> IO.puts "Buzz"
    true -> IO.puts x
  end
end

# 匿名関数に引数を与える際は.(x)らしい。このドットが何者なのか、調べてもわからず。
# パイプを使うと関数感ありますね(個人的感想)
1..100 |> Enum.map(fn(x) -> fizzbuzz.(x) end)

おまけ

Onlineでの良い実行環境を見つけられなかったため、今回はDockerで環境を構築しました。

FROM elixir
COPY ./fizzbuzz.exs /
CMD ["elixir", "fizzbuzz.exs"]
docker build -t elixir:fizzbuzz .
docker run --rm elixir:fizzbuzz
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?