consequence 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +54 -5
- data/lib/consequence.rb +1 -0
- data/lib/consequence/monad.rb +8 -0
- data/lib/consequence/option.rb +13 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6cdf2c9dad1c8995f8853db69da3d6804daf2029
|
4
|
+
data.tar.gz: bfc2e9093a371c54ee2c39c273c2c5ffea14ba18
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3066cbf8aa5a1415c37788e81f550c9ad96cc6b4a69604c26a2bc2ac8936e9ee610edc20b49bb694f37082b8cc8a084c0e9660f9e4bfb0085a00622550c0014b
|
7
|
+
data.tar.gz: 845c28a333e1ef7676cba5bbb6af7846eed3af5495e184b64a60f64adc7ebf3e128684886f59122d9bc804874f196154f77664cf5d0a1cc91622c3f56dbb6f71
|
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Consequence
|
2
2
|
|
3
|
+
[![Gem Version](https://badge.fury.io/rb/consequence.svg)](http://badge.fury.io/rb/consequence)
|
4
|
+
|
3
5
|
Monad implementation to be used with [contracts.ruby](https://github.com/egonSchiele/contracts.ruby)
|
4
6
|
|
5
7
|
## Example Usage
|
@@ -7,14 +9,12 @@ Monad implementation to be used with [contracts.ruby](https://github.com/egonSch
|
|
7
9
|
``` ruby
|
8
10
|
require 'consequence'
|
9
11
|
|
10
|
-
class
|
12
|
+
class HirePirate
|
11
13
|
include Consequence
|
12
14
|
alias_method :m, :method
|
13
15
|
|
14
16
|
def apply(applicant)
|
15
|
-
Success[applicant] >> m(:eye_patch_check)
|
16
|
-
<< m(:log)
|
17
|
-
>> :sign_and_date
|
17
|
+
Success[applicant] >> m(:eye_patch_check) << m(:log) >> :sign_and_date
|
18
18
|
end
|
19
19
|
|
20
20
|
private
|
@@ -56,7 +56,56 @@ If called with a Symbol instead of a method, it is sent as a message to the valu
|
|
56
56
|
|
57
57
|
A Success Monad wraps up all exceptions in a Failed Monad and a Failed Monad ignores all chained methods. This allows all possible failures in a long process to be dealt with at the end.
|
58
58
|
|
59
|
-
###
|
59
|
+
### Option
|
60
|
+
|
61
|
+
A Option Monad only applies a method if it's value is not nil, otherwise it ignores them. This prevents MissingMethod errors from methods trying to be applied to nil.
|
62
|
+
|
63
|
+
## Example Implementation of Left and Right
|
64
|
+
|
65
|
+
``` ruby
|
66
|
+
require 'consequence'
|
67
|
+
|
68
|
+
class Crab
|
69
|
+
include Consequence
|
70
|
+
alias_method :m, :method
|
71
|
+
|
72
|
+
Contract Monad => Monad
|
73
|
+
def begin(direction = Left[0])
|
74
|
+
direction >> m(:turn) >> m(:move) << m(:log) >> m(:continue?)
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
Left = Class.new(Monad)
|
80
|
+
Right = Class.new(Monad)
|
81
|
+
|
82
|
+
Contract Left => Num
|
83
|
+
def move(monad)
|
84
|
+
monad.value + 5
|
85
|
+
end
|
86
|
+
|
87
|
+
Contract Right => Num
|
88
|
+
def move(monad)
|
89
|
+
monad.value - 5
|
90
|
+
end
|
91
|
+
|
92
|
+
Contract Num => Monad
|
93
|
+
def turn(value)
|
94
|
+
rand > 0.5 ? Left[value] : Right[value]
|
95
|
+
end
|
96
|
+
|
97
|
+
def log(value)
|
98
|
+
puts value
|
99
|
+
end
|
100
|
+
|
101
|
+
Contract Monad => Monad
|
102
|
+
def continue?(monad)
|
103
|
+
monad.value.abs > 25 ? monad : monad >> m(:begin)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
Crab.new.begin
|
108
|
+
```
|
60
109
|
|
61
110
|
## Installation
|
62
111
|
|
data/lib/consequence.rb
CHANGED
data/lib/consequence/monad.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: consequence
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Max White
|
@@ -61,6 +61,7 @@ files:
|
|
61
61
|
- lib/consequence.rb
|
62
62
|
- lib/consequence/failure.rb
|
63
63
|
- lib/consequence/monad.rb
|
64
|
+
- lib/consequence/option.rb
|
64
65
|
- lib/consequence/success.rb
|
65
66
|
homepage:
|
66
67
|
licenses:
|