Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created December 3, 2012 17:15
Show Gist options
  • Save pmarreck/4196452 to your computer and use it in GitHub Desktop.
Save pmarreck/4196452 to your computer and use it in GitHub Desktop.
functional fizzbuzz in ruby without any conditionals
# Ruby fizzbuzz, functional style, no conditionals whatsoever
one_if_divisible_by = lambda{|num, x| (1-((x.to_f / num) % 1).ceil)}.curry
fizz1 = one_if_divisible_by.(3)
buzz1 = one_if_divisible_by.(5)
one_to_word = lambda{|func, word, n| word * func.(n)}.curry
one_to_n_to_s = lambda{|func, n| n.to_s * func.(n)}.curry
fizz = one_to_word.(fizz1,'Fizz')
buzz = one_to_word.(buzz1,'Buzz')
func_or_func = lambda{|func1, func2, n| func1.(n) | func2.(n) }.curry
fizz1_or_buzz1 = func_or_func.(fizz1, buzz1)
not_func = lambda {|func, n| 1 - func.(n)}.curry
not_fizz1_or_buzz1 = not_func.(fizz1_or_buzz1)
not_fizz1_or_buzz1_to_word = one_to_n_to_s.(not_fizz1_or_buzz1)
append_func1_func2_func3 = lambda{|func1, func2, func3, n| func1.(n) << func2.(n) << func3.(n)}.curry
fizzbuzz = append_func1_func2_func3.(fizz, buzz, not_fizz1_or_buzz1_to_word)
puts (1..100).map(&fizzbuzz)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment