Simplified Maybemaster
authorAanand Prasad <[email protected]>
Fri, 15 Feb 2008 11:38:45 +0000 (15 11:38 +0000)
committerAanand Prasad <[email protected]>
Fri, 15 Feb 2008 11:38:45 +0000 (15 11:38 +0000)
Added rcov task
Refactored Rakefile

Rakefile
lib/maybe.rb
test/specs.rb

index b8207d6..fb8a20d 100644 (file)
--- 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
dissimilarity index 79%
index 9bfd5dc..c333d45 100644 (file)
@@ -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
index 6c086a6..062c32a 100644 (file)
@@ -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