From 1f5f39019773b2cccf75c5e58082aaf0ecea187f Mon Sep 17 00:00:00 2001 From: Aanand Prasad Date: Fri, 15 Feb 2008 11:38:45 +0000 Subject: [PATCH] Simplified Maybe Added rcov task Refactored Rakefile --- Rakefile | 15 ++++++++++++--- lib/maybe.rb | 60 +++++++++++++++++++++-------------------------------------- test/specs.rb | 32 +++++++++++++++++++++---------- 3 files changed, 55 insertions(+), 52 deletions(-) rewrite lib/maybe.rb (79%) diff --git a/Rakefile b/Rakefile index b8207d6..fb8a20d 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,14 @@ +require 'spec/rake/spectask' + task :default => :test -task :test do - system 'spec test/specs.rb' -end \ No newline at end of file +desc "Run all specs" +Spec::Rake::SpecTask.new('test') do |t| + t.spec_files = FileList['test/specs.rb'] +end + +desc "Run all specs with RCov" +Spec::Rake::SpecTask.new('rcov') do |t| + t.spec_files = FileList['test/specs.rb'] + t.rcov = true +end diff --git a/lib/maybe.rb b/lib/maybe.rb dissimilarity index 79% index 9bfd5dc..c333d45 100644 --- a/lib/maybe.rb +++ b/lib/maybe.rb @@ -1,39 +1,21 @@ -class Maybe - extend Monad - - class << self - alias_method :nothing, :new - alias_method :just, :new - alias_method :unit, :just - end - - attr_accessor :value, :nothing - - def initialize *args - if args.empty? - @nothing = true - else - @value = args.shift - end - end - - def ==(m) - m.is_a? Maybe and ((nothing and m.nothing) or (value = m.value)) - end - - def bind &f - if self.nothing - self - else - f.call(self.value) - end - end - - def to_s - if @nothing - "nothing" - else - "just(#{@value})" - end - end -end +class Maybe + extend Monad + + class << self + alias_method :unit, :new + end + + attr_accessor :value + + def initialize value + @value = value + end + + def bind &f + if value.nil? + self + else + f.call(value) + end + end +end diff --git a/test/specs.rb b/test/specs.rb index 6c086a6..062c32a 100644 --- a/test/specs.rb +++ b/test/specs.rb @@ -1,26 +1,26 @@ require File.join(File.dirname(__FILE__), %w(.. init)) describe "Maybe:" do - specify "one or more `nothing's results in `nothing'" do + specify "one or more nils results in nil" do maybe = Maybe.run do - x <- just(1) - y <- nothing + x <- unit(1) + y <- unit(nil) unit(x+y) end - maybe.should == Maybe.nothing + maybe.value.should == nil end - specify "all `just' results in `just'" do + specify "all non-nil results in complete calculation" do maybe = Maybe.run do - x <- just(1) - y <- just(2) + x <- unit(1) + y <- unit(2) unit(x+y) end - maybe.should == Maybe.just(3) + maybe.value.should == 3 end end @@ -39,7 +39,7 @@ end describe "Monad.run" do specify "should pass extra arguments into the block" do - foo = 100 + foo = 8000 array = Array.run(foo) do |foo| x <- [1,2,3] @@ -48,7 +48,19 @@ describe "Monad.run" do unit(x+y+foo) end - array.should == [111, 121, 131, 112, 122, 132, 113, 123, 133] + array.should == [8011, 8021, 8031, 8012, 8022, 8032, 8013, 8023, 8033] + + foo = 8000 + bar = 70000 + + array = Array.run(foo, bar) do |foo, bar| + x <- [1,2,3] + y <- [10,20,30] + + unit(x+y+foo+bar) + end + + array.should == [78011, 78021, 78031, 78012, 78022, 78032, 78013, 78023, 78033] end specify "should be nestable" do -- 2.11.4.GIT