Last active
August 29, 2015 14:02
-
-
Save bjhaid/0e21f6f7e4252dc0789f to your computer and use it in GitHub Desktop.
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
class Proc | |
#This defines a method that can allow the case equality === http://www.ruby-doc.org/core-2.1.1/Proc.html#method-i-3D-3D-3D be called on 2 proc objects | |
def &o | |
->(a) { call(a) && o[a] } | |
end | |
end | |
def multiple_of(x) | |
#A partial function http://en.wikipedia.org/wiki/Partial_function | |
->(a) { a % x == 0 } | |
end | |
(1..100).each do |x| | |
case x | |
when multiple_of(3) & multiple_of(5) | |
#elegance of the & method defined on the proc object shows ... multiple_of(3) returns | |
#->(a) { a % 3 == 0 } | |
#multiple_of(5) returns ->(a) { a % 5 == 0 } | |
#both are combined with the & to return something similar to ->(a) { (->(a) { a % 3 == 0 }) && ->(a) { a % 5 == 0 } } | |
#ruby now calls this new proc object with x to return a truthy or falsy value to determine if it should continue to the next line of the switch | |
puts "FizzBuzz" | |
when multiple_of(3) | |
puts "Fizz" | |
when multiple_of(5) | |
puts "Buzz" | |
else | |
puts x | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment