Skip to content

Commit

Permalink
Identity monad
Browse files Browse the repository at this point in the history
  • Loading branch information
aanand committed Nov 3, 2011
1 parent 7df43c4 commit 46e7245
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
14 changes: 14 additions & 0 deletions lib/do_notation/monads/identity.rb
@@ -0,0 +1,14 @@
module Identity
include Monad

class << self
def unit(obj)
obj
end

def bind(obj, &fn)
fn.call(obj)
end
end
end

27 changes: 27 additions & 0 deletions spec/identity_spec.rb
@@ -0,0 +1,27 @@
require File.join(File.dirname(__FILE__), 'spec_helper')
require 'do_notation/monads/identity'

describe "Identity:" do
specify "just works like sequential code" do
value = Identity.run do
x <- 1
y <- 2

x + y
end

value.should == 3
end

specify "doesn't do anything special with nil" do
lambda {
value = Identity.run do
x <- 1
y <- nil

x + y
end
}.should raise_error(TypeError)
end
end

5 changes: 3 additions & 2 deletions spec/monad_spec.rb
Expand Up @@ -28,8 +28,9 @@
unit(x+y)
end

Array.run(&block).should == Array.unit(3)
Maybe.run(&block).should == Maybe.unit(3)
[Identity, Maybe, Array].each do |monad|
monad.run(&block).should == monad.unit(3)
end
end

specify "should be nestable" do
Expand Down

0 comments on commit 46e7245

Please sign in to comment.