Class: Divergent::Success
- Inherits:
-
Object
- Object
- Divergent::Success
show all
- Includes:
- Try
- Defined in:
- lib/divergent/try.rb
Overview
Instance Method Summary
collapse
Methods included from Try
#get_or_else, #or_else, unit
Methods included from Monad
included
Constructor Details
#initialize(value) ⇒ Success
Returns a new instance of Success.
136
137
138
|
# File 'lib/divergent/try.rb', line 136
def initialize(value)
@value = value
end
|
Instance Method Details
#each {|@value| ... } ⇒ Object
160
161
162
163
|
# File 'lib/divergent/try.rb', line 160
def each()
yield(@value)
nil
end
|
#failure? ⇒ Boolean
152
153
154
|
# File 'lib/divergent/try.rb', line 152
def failure?
false
end
|
#filter ⇒ Object
171
172
173
174
175
176
177
178
179
|
# File 'lib/divergent/try.rb', line 171
def filter()
fmap do |v|
if yield(v)
self
else
Failure.new(NoSuchElementError.new("Predicate does not hold for #{v}"))
end
end
end
|
#flatten ⇒ Object
197
198
199
200
201
202
203
|
# File 'lib/divergent/try.rb', line 197
def flatten
if @value.is_a? Try
@value
else
self
end
end
|
#fmap ⇒ Object
140
141
142
143
144
145
146
|
# File 'lib/divergent/try.rb', line 140
def fmap()
begin
yield @value
rescue => e
Failure.new(e)
end
end
|
#get ⇒ Object
156
157
158
|
# File 'lib/divergent/try.rb', line 156
def get
@value
end
|
#map ⇒ Object
165
166
167
168
169
|
# File 'lib/divergent/try.rb', line 165
def map()
fmap do |v|
Try.unit { yield v }
end
end
|
#recover(*_errors, &block) ⇒ Object
185
186
187
|
# File 'lib/divergent/try.rb', line 185
def recover(*_errors, &block)
self
end
|
#recover_with(&block) ⇒ Object
181
182
183
|
# File 'lib/divergent/try.rb', line 181
def recover_with(&block)
self
end
|
#success? ⇒ Boolean
148
149
150
|
# File 'lib/divergent/try.rb', line 148
def success?
true
end
|
#to_s ⇒ Object
Also known as:
inspect
205
206
207
|
# File 'lib/divergent/try.rb', line 205
def to_s
"Success<#{@value}>"
end
|
193
194
195
|
# File 'lib/divergent/try.rb', line 193
def transform(s, _f)
fmap(&s)
end
|