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

December 2025

S M T W T F S
  1 2 3 4 56
7 89101112 13
14 151617 181920
21 222324252627
28293031   

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Dec. 22nd, 2025 09:18 pm
Powered by Dreamwidth Studios