ruby-maybe 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source :rubygems
2
+
3
+ group :test do
4
+ gem 'rake'
5
+ end
@@ -0,0 +1,10 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ rake (10.0.3)
5
+
6
+ PLATFORMS
7
+ ruby
8
+
9
+ DEPENDENCIES
10
+ rake
@@ -0,0 +1,9 @@
1
+ require 'rake/testtask'
2
+
3
+ task :default => :test
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.libs.push "lib"
7
+ t.test_files = FileList['spec/*_spec.rb']
8
+ t.verbose = true
9
+ end
@@ -0,0 +1,45 @@
1
+ class Maybe
2
+ def bind(&block)
3
+ Maybe.new
4
+ end
5
+ end
6
+
7
+ class Just < Maybe
8
+ def initialize(value)
9
+ @value = value
10
+ end
11
+
12
+ def bind(&block)
13
+ value = block.call(@value)
14
+ warn("Not returning a Maybe from #bind is really bad form...") unless value.kind_of?(Maybe)
15
+ value
16
+ end
17
+
18
+ def ==(object)
19
+ if object.class == Just
20
+ object.value == self.value
21
+ else
22
+ false
23
+ end
24
+ end
25
+
26
+ protected
27
+
28
+ def value
29
+ @value
30
+ end
31
+ end
32
+
33
+ class Nothing < Maybe
34
+ def bind
35
+ Nothing.new
36
+ end
37
+
38
+ def ==(object)
39
+ if object.class == Nothing
40
+ true
41
+ else
42
+ false
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ lib = File.expand_path('../lib/', __FILE__)
2
+ $:.unshift lib unless $:.include?(lib)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "ruby-maybe"
6
+ s.version = "0.0.1"
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Callum Stott"]
9
+ s.email = ["callum.stott@me.com"]
10
+ s.summary = "Maybe monad implementation for Ruby"
11
+
12
+ s.require_paths = ['lib']
13
+ s.files = `git ls-files`.split("\n")
14
+ end
@@ -0,0 +1,72 @@
1
+ require 'minitest/autorun'
2
+ require 'ruby-maybe'
3
+
4
+ describe "Just" do
5
+ describe "#bind" do
6
+ it "applys the passed block to its boxed value" do
7
+ Just.new(5).bind { |val| Just.new(val * 2) }.must_equal Just.new(10)
8
+ end
9
+ end
10
+ end
11
+
12
+ describe "Nothing" do
13
+ describe "#bind" do
14
+ it "does not execute the passed block" do
15
+ executed = false
16
+ Nothing.new.bind { |val| executed = true }
17
+ executed.must_equal false
18
+ end
19
+
20
+ it "returns a Nothing" do
21
+ Nothing.new.bind { |val| val }.kind_of?(Nothing).must_equal true
22
+ end
23
+ end
24
+
25
+ describe "#==" do
26
+ it "returns false if the passed object is not a Just" do
27
+ (Just.new(5) == 5).must_equal false
28
+ end
29
+
30
+ it "returns true if the passed object is a Just with the same value" do
31
+ (Just.new(5) == Just.new(5)).must_equal true
32
+ end
33
+ end
34
+ end
35
+
36
+ describe "Maybe" do
37
+ describe "#bind" do
38
+ it "does not execute the passed block" do
39
+ executed = false
40
+ Maybe.new.bind { |val| executed = true }
41
+ executed.must_equal false
42
+ end
43
+
44
+ it "returns a Maybe" do
45
+ Maybe.new.bind { |val| val }.kind_of?(Maybe).must_equal true
46
+ end
47
+ end
48
+
49
+ describe "#==" do
50
+ it "returns false if the passed object is not a Nothing" do
51
+ (Nothing.new == 5).must_equal false
52
+ end
53
+
54
+ it "returns true if the passed object is a Nothing" do
55
+ (Nothing.new == Nothing.new).must_equal true
56
+ end
57
+ end
58
+ end
59
+
60
+ describe "Integration Specs" do
61
+ it "works as expected" do
62
+ Just.new(5).bind { |val|
63
+ Just.new(val * 3)
64
+ }.bind { |val|
65
+ if (val > 10)
66
+ Nothing.new
67
+ else
68
+ Just.new(val)
69
+ end
70
+ }.must_equal(Nothing.new)
71
+ end
72
+ end
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-maybe
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Callum Stott
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
15
+ email:
16
+ - callum.stott@me.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - Rakefile
25
+ - lib/ruby-maybe.rb
26
+ - ruby-maybe.gemspec
27
+ - spec/ruby-maybe_spec.rb
28
+ homepage:
29
+ licenses: []
30
+ post_install_message:
31
+ rdoc_options: []
32
+ require_paths:
33
+ - lib
34
+ required_ruby_version: !ruby/object:Gem::Requirement
35
+ none: false
36
+ requirements:
37
+ - - ! '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ required_rubygems_version: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 1.8.23
49
+ signing_key:
50
+ specification_version: 3
51
+ summary: Maybe monad implementation for Ruby
52
+ test_files: []