Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What is going on with this article?
@amay077

Ruby と C#(LINQ) と Java8 で FizzBuzz

More than 5 years have passed since last update.

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

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
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
amay077
ランチの時は呼ぶといい!

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
3
Help us understand the problem. What is going on with this article?