Last active
October 13, 2015 10:17
-
-
Save orend/4180462 to your computer and use it in GitHub Desktop.
FizBuzz curry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's why they call it FUNctional programming!