LoginSignup
3
3

More than 5 years have passed since last update.

Ruby と C#(LINQ) と Java8 で FizzBuzz

Last updated at Posted at 2014-02-28

出尽くしたネタなんでしょうけども、個人としては初めて書きました(^_^;)

Ruby

勉強会で教えてもらったので、初めて書きました。

fizzbuzz.rb
p (1..100).to_a
.map{|x| 
  if    x%15==0 then "FizzBuzz" 
  elsif x%3==0  then "Fizz" 
  elsif x%5==0  then "Buzz" else x end}
.join(",")

C#

fizzbuzz.cs
Console.WriteLine(
Enumerable.Range(1, 100)
.Select(x=> 
  x%15==0 ? "FizzBuzz" :
  x%3==0  ? "Fizz" :
  x%5==0  ? "Buzz" : x.ToString())
.Aggregate("", (acc, x) => acc=="" ? x : acc + "," + x));

文字列をつなげるのは String.Join で良いんだけど、Ruby の join っぽくしてみたかったので、Aggregate を使ってみました。

Java8

System.out.println(
Stream.iterate(1, x->x++).limit(100)
.map(x->
  x%15==0 ? "FizzBuzz" :
  x%3==0  ? "Fizz" :
  x%5==0  ? "Buzz" : String.valueOf(x))
.collect(Collectors.joining(",")));

Ruby 短いっすね。

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