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

November 2025

S M T W T F S
       1
23456 7 8
9 1011 12 1314 15
16171819 202122
23242526272829
30      

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Nov. 21st, 2025 06:55 am
Powered by Dreamwidth Studios