Skip to content

Instantly share code, notes, and snippets.

@orend
Last active October 13, 2015 10:17
Show Gist options
  • Select an option

  • Save orend/4180462 to your computer and use it in GitHub Desktop.

Select an option

Save orend/4180462 to your computer and use it in GitHub Desktop.
FizBuzz curry
f = proc{|string, num, x| x % num == 0 ? string : x}.curry
f3 = f.('Fizz',3)
f15 = f.('FizzBuzz',15)
f5 = f.('Buzz',5)
puts (1..100).map(&f15).map(&f5).map(&f3)
# and here's another approach
module Enumerable
# map all elements for which the condition is true, leave the rest intact
def map_if(cond)
[].tap { |out| each { |e| cond.call(e) ? out << yield(e) : out << e} }
end
end
f = proc{|i, x| x % i == 0}.curry
puts (1..100).map_if(f.(15)){'FizzBuzz'}.map_if(f.(3)){'Fizz'}.map_if(f.(5)){'Buzz'}
@carsomyr
Copy link
Copy Markdown

carsomyr commented Dec 3, 2012

That's why they call it FUNctional programming!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment