dry-monads 0.0.1 → 0.0.2
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 +4 -4
- data/LICENSE +1 -1
- data/README.md +2 -185
- data/lib/dry/monads/either.rb +4 -0
- data/lib/dry/monads/maybe.rb +4 -0
- data/lib/dry/monads/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 578a10e9ec2b0ca63809f588c68d14a643a43f4b
|
4
|
+
data.tar.gz: 8d69e82a8e58de7488aca590a7f321c978431734
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f2d1786620c51d8ab53e8b59099143e8867f9ac2223b155a946bee1d6b23bc5740a4623250002e33df54562c371d802977b27553f2b70ab3270b906a51a75ead
|
7
|
+
data.tar.gz: 46bc1e583a5fbb1e597aad4ed157757aa960abe39bf3aab92af54110f17bebf09a10a1ce2d0d4f198f0dfc0174841fd3b360275a716d95e6c45f77f64e0e3175
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -34,192 +34,9 @@ Or install it yourself as:
|
|
34
34
|
$ gem install dry-monads
|
35
35
|
```
|
36
36
|
|
37
|
-
##
|
37
|
+
## Links
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
The `Maybe` mondad is used when a series of computations that could return `nil`
|
42
|
-
at any point.
|
43
|
-
|
44
|
-
#### `bind`
|
45
|
-
|
46
|
-
```ruby
|
47
|
-
require 'dry-monads'
|
48
|
-
|
49
|
-
M = Dry::Monads
|
50
|
-
|
51
|
-
maybe_user = M.Maybe(user).bind do |u|
|
52
|
-
M.Maybe(user.address).bind do |a|
|
53
|
-
M.Maybe(a.street)
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
# If user with address exists
|
58
|
-
# => Some("Street Address")
|
59
|
-
# If user or address is nil
|
60
|
-
# => None()
|
61
|
-
|
62
|
-
# You also can pass a proc to #bind
|
63
|
-
|
64
|
-
add_two = -> x { x + 2 }
|
65
|
-
|
66
|
-
M.Maybe(5).bind(add_two).bind(add_two) # => Some(9)
|
67
|
-
|
68
|
-
```
|
69
|
-
|
70
|
-
#### `fmap`
|
71
|
-
|
72
|
-
Similar to `bind` but lifts the result for you.
|
73
|
-
|
74
|
-
```ruby
|
75
|
-
require 'dry-monads'
|
76
|
-
|
77
|
-
Dry::Monads::Maybe(user).fmap(&:address).fmap(&:street)
|
78
|
-
|
79
|
-
# If user with address exists
|
80
|
-
# => Some("Street Address")
|
81
|
-
# If user or address is nil
|
82
|
-
# => None()
|
83
|
-
```
|
84
|
-
|
85
|
-
### Either monad
|
86
|
-
|
87
|
-
The `Either` mondad is useful to express a series of computations that might
|
88
|
-
return an error object with additional information.
|
89
|
-
|
90
|
-
The `Either` mixin has two type constructors: `Right` and `Left`. The `Right`
|
91
|
-
can be thought of as "everything went right" and the `Left` is used when
|
92
|
-
"something has gone wrong".
|
93
|
-
|
94
|
-
#### `Either::Mixin`
|
95
|
-
|
96
|
-
```ruby
|
97
|
-
require 'dry-monads'
|
98
|
-
|
99
|
-
class EitherCalculator
|
100
|
-
include Dry::Monads::Either::Mixin
|
101
|
-
|
102
|
-
attr_accessor :input
|
103
|
-
|
104
|
-
def calculate
|
105
|
-
i = Integer(input)
|
106
|
-
|
107
|
-
Right(i).bind do |value|
|
108
|
-
if value > 1
|
109
|
-
Right(value + 3)
|
110
|
-
else
|
111
|
-
Left("value was less than 1")
|
112
|
-
end
|
113
|
-
end.bind do |value|
|
114
|
-
if value % 2 == 0
|
115
|
-
Right(value * 2)
|
116
|
-
else
|
117
|
-
Left("value was not even")
|
118
|
-
end
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
# EitherCalculator instance
|
124
|
-
c = EitherCalculator.new
|
125
|
-
|
126
|
-
# If everything went right
|
127
|
-
c.input = 3
|
128
|
-
result = c.calculate
|
129
|
-
result # => Right(12)
|
130
|
-
result.value # => 12
|
131
|
-
|
132
|
-
# If if failed in the first block
|
133
|
-
c.input = 0
|
134
|
-
result = c.calculate
|
135
|
-
result # => Left("value was less than 1")
|
136
|
-
result.value # => "value was less than 1"
|
137
|
-
|
138
|
-
# if it failed in the second block
|
139
|
-
c.input = 2
|
140
|
-
result = c.calculate
|
141
|
-
result # => Left("value was not even")
|
142
|
-
result.value # => "value was not even"
|
143
|
-
```
|
144
|
-
|
145
|
-
#### `fmap`
|
146
|
-
|
147
|
-
An example of using `fmap` with `Right` and `Left`.
|
148
|
-
|
149
|
-
```ruby
|
150
|
-
require 'dry-monads'
|
151
|
-
|
152
|
-
M = Dry::Monads
|
153
|
-
|
154
|
-
result = if foo > bar
|
155
|
-
M.Right(10)
|
156
|
-
else
|
157
|
-
M.Left("wrong")
|
158
|
-
end.fmap { |x| x * 2 }
|
159
|
-
|
160
|
-
# If everything went right
|
161
|
-
result # => Right(20)
|
162
|
-
# If it did not
|
163
|
-
result # => Left("wrong")
|
164
|
-
|
165
|
-
# #fmap accepts proc as well as #bind
|
166
|
-
|
167
|
-
upcase = :upcase.to_proc
|
168
|
-
|
169
|
-
M.Right('hello').fmap(upcase) # => Right("HELLO")
|
170
|
-
```
|
171
|
-
|
172
|
-
#### `or`
|
173
|
-
|
174
|
-
An example of using `or` with `Right` and `Left`.
|
175
|
-
|
176
|
-
```ruby
|
177
|
-
M = Dry::Monads
|
178
|
-
|
179
|
-
M.Right(10).or(M.Right(99)) # => Right(10)
|
180
|
-
M.Left("error").or(M.Left("new error")) # => Left("new error")
|
181
|
-
M.Left("error").or { |err| M.Left("new #{err}") } # => Left("new error")
|
182
|
-
```
|
183
|
-
|
184
|
-
#### `to_maybe`
|
185
|
-
|
186
|
-
Sometimes it's useful to turn an 'Either' into a 'Maybe'
|
187
|
-
|
188
|
-
```ruby
|
189
|
-
require 'dry-monads'
|
190
|
-
|
191
|
-
result = if foo > bar
|
192
|
-
Dry::Monads.Right(10)
|
193
|
-
else
|
194
|
-
Dry::Monads.Left("wrong")
|
195
|
-
end.to_maybe
|
196
|
-
|
197
|
-
# If everything went right
|
198
|
-
result # => Some(10)
|
199
|
-
# If it did not
|
200
|
-
result # => None()
|
201
|
-
```
|
202
|
-
|
203
|
-
### Try monad
|
204
|
-
|
205
|
-
Examples of using the `Try` monad.
|
206
|
-
|
207
|
-
```ruby
|
208
|
-
require 'dry-monads'
|
209
|
-
|
210
|
-
extend Dry::Monads::Try::Mixin
|
211
|
-
|
212
|
-
res = Try() { 10 / 2 }
|
213
|
-
res.value if res.success?
|
214
|
-
# => 5
|
215
|
-
|
216
|
-
res = Try() { 10 / 0 }
|
217
|
-
res.exception if res.failure?
|
218
|
-
# => #<ZeroDivisionError: divided by 0>
|
219
|
-
|
220
|
-
Try(NoMethodError, NotImplementedError) { 10 / 0 }
|
221
|
-
# => raise ZeroDivisionError: divided by 0 exception
|
222
|
-
```
|
39
|
+
* [Documentation](http://dry-rb.org/gems/dry-monads)
|
223
40
|
|
224
41
|
## Development
|
225
42
|
|
data/lib/dry/monads/either.rb
CHANGED
data/lib/dry/monads/maybe.rb
CHANGED
data/lib/dry/monads/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dry-monads
|
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
|
- Nikita Shilnikov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-06-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|