ribimaybe 0.0.12 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5eaabf0383c05d88afb353a4ed3dbd0d9a6c0894
4
- data.tar.gz: 9271691363379c9749a5f5f7d9376e2e54b9f358
3
+ metadata.gz: c4f36f13ff02dcf449662d3d89acdce59f781329
4
+ data.tar.gz: 09a060eebd6164be01a68f33d6820660a2afb7eb
5
5
  SHA512:
6
- metadata.gz: 8157d075f048b7b0f1af57cbc0b936b596ed8eb6c7d25c3a19455cac0b9ddfea9e8d60f49a5b7ae5331d8e4ec899e0b1ee88a3afe962b52e35542bba404ca552
7
- data.tar.gz: 99921ff3c5289207a063985b424bc038b5a2493f7c6112ec730a28f4b561f1b2228582bd99208ef8a099ceab9ed132791707735da2a90a54ba4af3bf605947df
6
+ metadata.gz: e99de225be69caf575dabfb961167d1b642dd2e497371ef2a301276d72529f70e7e63abe21f55c3f77ba14d523c2b2b15f5bb4a8e34107d9fdc1510bb6c0e479
7
+ data.tar.gz: a57acc14c7af6109c870efd22a31a9e2eec688aa2b9e038ef11f8466d2cfa8ee9a7028ae3b9ac520d0f785e2dba5812528f0ccbf2479f7552c13b0046a705a9d
data/README.md CHANGED
@@ -91,7 +91,15 @@ end # => Just(42)
91
91
  Nothing.bind do |x|
92
92
  rturn(x * x)
93
93
  end # => Nothing
94
+
95
+ # We even have >= but you need to pass a Proc or a lambda.
96
+ Just(42) >= -> (x) do
97
+ rturn(x - 21) >= -> (y) do
98
+ if x * x > 100 then rturn(y) else rturn(x) end
99
+ end
100
+ end
94
101
  ```
102
+
95
103
  ## Contributing
96
104
 
97
105
  1. Fork it
data/lib/ribimaybe.rb CHANGED
@@ -59,9 +59,13 @@ module Ribimaybe
59
59
  # No operation. Always returns Nothing.
60
60
  #
61
61
  Contract Proc => Nothing
62
- def self.bind(&_)
62
+ def self.bind(fn = nil, &_)
63
63
  self
64
64
  end
65
+
66
+ class << self
67
+ alias_method :>=, :bind
68
+ end
65
69
  end
66
70
 
67
71
  class Just
@@ -165,9 +169,11 @@ module Ribimaybe
165
169
  # end # => Just(2)
166
170
  #
167
171
  Contract Proc => Or[Nothing, Just]
168
- def bind(&fn)
169
- fn.curry.(@value)
172
+ def bind(fn = nil, &block)
173
+ (fn || block).curry.(@value)
170
174
  end
175
+
176
+ alias_method :>=, :bind
171
177
  end
172
178
 
173
179
  # Converts nil to Nothing or lifts value into a Just. Accepts a optional
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Ribimaybe
2
- VERSION = "0.0.12"
2
+ VERSION = "0.0.13"
3
3
  end
@@ -21,81 +21,83 @@ describe "Applicative Instance" do
21
21
  ->(x){ ->(y) { x } }.(SecureRandom.base64(1000))
22
22
  end
23
23
 
24
- # pure id <*> v = v
25
- describe "identity" do
26
- context "when i have nothing" do
27
- it do
28
- expect(pure(&id).apply(Nothing)).to eq(Nothing)
24
+ [:apply, :>>].each do |m|
25
+ # pure id <*> v = v
26
+ describe "identity" do
27
+ context "when i have nothing" do
28
+ it do
29
+ expect(pure(&id).public_send(m, Nothing)).to eq(Nothing)
30
+ end
29
31
  end
30
- end
31
32
 
32
- context "when i have just :x" do
33
- it do
34
- expect(pure(&id).apply(pure(:x))).to eq(pure(:x))
33
+ context "when i have just :x" do
34
+ it do
35
+ expect(pure(&id).public_send(m, pure(:x))).to eq(pure(:x))
36
+ end
35
37
  end
36
38
  end
37
- end
38
39
 
39
- # pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
40
- describe "composition" do
41
- context "when i have nothing" do
42
- it do
43
- lhs = pure(&dot).apply(pure(&f)).apply(pure(&g)).apply(Nothing)
44
- rhs = pure(&f).apply(pure(&g).apply(Nothing))
45
- expect(lhs).to eq(rhs)
40
+ # pure (.) <*> u <*> v <*> w = u <*> (v <*> w)
41
+ describe "composition" do
42
+ context "when i have nothing" do
43
+ it do
44
+ lhs = pure(&dot).public_send(m, pure(&f)).public_send(m, pure(&g)).public_send(m, Nothing)
45
+ rhs = pure(&f).public_send(m, pure(&g).public_send(m, Nothing))
46
+ expect(lhs).to eq(rhs)
47
+ end
46
48
  end
47
- end
48
49
 
49
- context "when i have just :x" do
50
- it do
51
- lhs = pure(&dot).apply(pure(&f)).apply(pure(&g)).apply(pure(:x))
52
- rhs = pure(&f).apply(pure(&g).apply(pure(:x)))
53
- expect(lhs).to eq(rhs)
50
+ context "when i have just :x" do
51
+ it do
52
+ lhs = pure(&dot).public_send(m, pure(&f)).public_send(m, pure(&g)).public_send(m, pure(:x))
53
+ rhs = pure(&f).public_send(m, pure(&g).public_send(m, pure(:x)))
54
+ expect(lhs).to eq(rhs)
55
+ end
54
56
  end
55
57
  end
56
- end
57
58
 
58
- # pure f <*> pure x = pure (f x)
59
- describe "homomorphism" do
60
- context "when i have nothing" do
61
- it do
62
- expect(pure(&f).apply(Nothing)).to eq(Nothing)
59
+ # pure f <*> pure x = pure (f x)
60
+ describe "homomorphism" do
61
+ context "when i have nothing" do
62
+ it do
63
+ expect(pure(&f).public_send(m, Nothing)).to eq(Nothing)
64
+ end
63
65
  end
64
- end
65
66
 
66
- context "when i have just :x" do
67
- it do
68
- expect(pure(&f).apply(pure(:x))).to eq(pure(f.(:x)))
67
+ context "when i have just :x" do
68
+ it do
69
+ expect(pure(&f).public_send(m, pure(:x))).to eq(pure(f.(:x)))
70
+ end
69
71
  end
70
72
  end
71
- end
72
73
 
73
- # u <*> pure y = pure ($ y) <*> u
74
- describe "interchange" do
75
- context "when i have nothing" do
76
- it do
77
- expect(pure(&f).apply(Nothing)).to eq(pure(&ap.(f)).apply(Nothing))
74
+ # u <*> pure y = pure ($ y) <*> u
75
+ describe "interchange" do
76
+ context "when i have nothing" do
77
+ it do
78
+ expect(pure(&f).public_send(m, Nothing)).to eq(pure(&ap.(f)).public_send(m, Nothing))
79
+ end
78
80
  end
79
- end
80
81
 
81
- context "when i have just :x" do
82
- it do
83
- expect(pure(&f).apply(pure(:x))).to eq(pure(&ap.(f)).apply(pure(:x)))
82
+ context "when i have just :x" do
83
+ it do
84
+ expect(pure(&f).public_send(m, pure(:x))).to eq(pure(&ap.(f)).public_send(m, pure(:x)))
85
+ end
84
86
  end
85
87
  end
86
- end
87
88
 
88
- # fmap f x = pure f <*> x
89
- describe "map" do
90
- context "when i have nothing" do
91
- it do
92
- expect(Nothing.map(&f)).to eq(pure(&f).apply(Nothing))
89
+ # fmap f x = pure f <*> x
90
+ describe "map" do
91
+ context "when i have nothing" do
92
+ it do
93
+ expect(Nothing.map(&f)).to eq(pure(&f).public_send(m, Nothing))
94
+ end
93
95
  end
94
- end
95
96
 
96
- context "when i have just :x" do
97
- it do
98
- expect(Just(:x).map(&f)).to eq(pure(&f).apply(Just(:x)))
97
+ context "when i have just :x" do
98
+ it do
99
+ expect(Just(:x).map(&f)).to eq(pure(&f).public_send(m, Just(:x)))
100
+ end
99
101
  end
100
102
  end
101
103
  end
data/spec/monad_spec.rb CHANGED
@@ -17,51 +17,53 @@ describe "Monad Instance" do
17
17
  ->(x){ ->(y) { rturn(x) } }.(SecureRandom.base64(1000))
18
18
  end
19
19
 
20
- # return a >>= f = f a
21
- describe "left identity" do
22
- context "when i have nothing" do
23
- it do
24
- expect(Nothing.bind(&lifted_id)).to eq(Nothing)
20
+ [:bind, :>=].each do |m|
21
+ # return a >>= f = f a
22
+ describe "left identity" do
23
+ context "when i have nothing" do
24
+ it do
25
+ expect(Nothing.public_send(m, &lifted_id)).to eq(Nothing)
26
+ end
25
27
  end
26
- end
27
28
 
28
- context "when i have just :x" do
29
- it do
30
- expect(rturn(:x).bind(&lifted_id)).to eq(lifted_id.(:x))
29
+ context "when i have just :x" do
30
+ it do
31
+ expect(rturn(:x).public_send(m, &lifted_id)).to eq(lifted_id.(:x))
32
+ end
31
33
  end
32
34
  end
33
- end
34
35
 
35
- # m >>= return = m
36
- describe "right identity" do
37
- context "when i have nothing" do
38
- it do
39
- expect(Nothing.bind(&lifted_id)).to eq(Nothing)
36
+ # m >>= return = m
37
+ describe "right identity" do
38
+ context "when i have nothing" do
39
+ it do
40
+ expect(Nothing.public_send(m, &lifted_id)).to eq(Nothing)
41
+ end
40
42
  end
41
- end
42
43
 
43
- context "when i have just :x" do
44
- it do
45
- expect(Just(:x).bind(&lifted_id)).to eq(Just(:x))
44
+ context "when i have just :x" do
45
+ it do
46
+ expect(Just(:x).public_send(m, &lifted_id)).to eq(Just(:x))
47
+ end
46
48
  end
47
49
  end
48
- end
49
50
 
50
- # (m >>= f) >>= g = m >>= (\x -> f x >>= g)
51
- describe "associativity" do
52
- context "when i have nothing" do
53
- it do
54
- lhs = Nothing.bind(&f).bind(&g)
55
- rhs = Nothing.bind { |x| f.(x).bind(&g) }
56
- expect(lhs).to eq(rhs)
51
+ # (m >>= f) >>= g = m >>= (\x -> f x >>= g)
52
+ describe "associativity" do
53
+ context "when i have nothing" do
54
+ it do
55
+ lhs = Nothing.public_send(m, &f).public_send(m, &g)
56
+ rhs = Nothing.bind { |x| f.(x).public_send(m, &g) }
57
+ expect(lhs).to eq(rhs)
58
+ end
57
59
  end
58
- end
59
60
 
60
- context "when i have just :x" do
61
- it do
62
- lhs = Just(:x).bind(&f).bind(&g)
63
- rhs = Just(:x).bind { |x| f.(x).bind(&g) }
64
- expect(lhs).to eq(rhs)
61
+ context "when i have just :x" do
62
+ it do
63
+ lhs = Just(:x).public_send(m, &f).public_send(m, &g)
64
+ rhs = Just(:x).bind { |x| f.(x).public_send(m, &g) }
65
+ expect(lhs).to eq(rhs)
66
+ end
65
67
  end
66
68
  end
67
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ribimaybe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.12
4
+ version: 0.0.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - unsymbol