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

October 2025

S M T W T F S
    1 23 4
5 678 91011
12131415161718
19202122232425
262728293031 

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Oct. 9th, 2025 11:12 pm
Powered by Dreamwidth Studios