May. 24th, 2016

juan_gandhi: (VP)
 ДЕНЕГ НЕТ 
 А ВЫ ДЕРЖИТЕСЬ 
juan_gandhi: (VP)
Can anybody write a really beautiful one?

All I could come up with is this:

def fizzbuzz(n: Int) = 1 to n foreach { i =>
  def p(k: Int, what: Any, alt:  => Unit) = if (i % k == 0) print(what) else alt
  p(15, "fizzbuzz", p(3, "fizz", p(5, "buzz", p(1, i, {})))); println }


UPD from 2024:
def fizzbuzz(n: Int) = 1 to n foreach { i =>
  def p(k: Int, what: Any, alt:  => Any) = if (i % k == 0) what else alt
  println(p(15, "fizzbuzz", p(3, "fizz", p(5, "buzz", p(1, i, i)))))
}

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

September 2025

S M T W T F S
 1 2345 6
78 9 10 111213
14151617181920
21222324252627
282930    

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Sep. 12th, 2025 07:41 pm
Powered by Dreamwidth Studios