Created
January 25, 2013 19:49
-
-
Save nurugger07/4637291 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
# Ruby-2.0.0-preview1 | |
module ProjectEuler | |
refine Enumerator::Lazy do | |
def method_missing(method, *args, &block) | |
if method.to_s =~ /multiple_of_(.*)$/ | |
multiple_of $1.to_i, args[0].to_i | |
else | |
super | |
end | |
end | |
def multiple_of(multiple, number) | |
number % multiple == 0 | |
end | |
def select_multiples_of_3_and_5 | |
select { |x| multiple_of_3(x) || multiple_of_5(x) } | |
end | |
def then_sum | |
reduce(:+) | |
end | |
end | |
end | |
class WithoutEuler | |
def self.call_multiple_of_3_on_lazy | |
(1..5).lazy.select_multiples_of_3_and_5 | |
end | |
end | |
class FunctionalRuby | |
using ProjectEuler | |
def self.sum_multiples_of_3_and_5_for_range(range) | |
range.lazy.select_multiples_of_3_and_5.then_sum | |
end | |
end | |
describe "Ruby 2.0 features" do | |
context "Multiples of 3 and 5" do | |
it "Sum of all the multiples of 3 or 5 below 1000" do | |
FunctionalRuby.sum_multiples_of_3_and_5_for_range((1..1000)).should eq(234168) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment