dry-monads 1.6.0 → 1.7.0

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.
@@ -34,31 +34,25 @@ module Dry
34
34
  # Returns self, added to keep the interface compatible with other monads.
35
35
  #
36
36
  # @return [Result::Success, Result::Failure]
37
- def to_result
38
- self
39
- end
37
+ def to_result = self
40
38
 
41
39
  # Returns self.
42
40
  #
43
41
  # @return [Result::Success, Result::Failure]
44
- def to_monad
45
- self
46
- end
42
+ def to_monad = self
47
43
 
48
44
  # Returns the Result monad.
49
45
  # This is how we're doing polymorphism in Ruby 😕
50
46
  #
51
47
  # @return [Monad]
52
- def monad
53
- Result
54
- end
48
+ def monad = Result
55
49
 
56
50
  # Represents a value of a successful operation.
57
51
  #
58
52
  # @api public
59
53
  class Success < Result
60
54
  include RightBiased::Right
61
- include Dry::Equalizer(:value!)
55
+ include ::Dry::Equalizer(:value!)
62
56
 
63
57
  # Shortcut for Success([...])
64
58
  #
@@ -70,9 +64,7 @@ module Dry
70
64
  # end
71
65
  #
72
66
  # @api public
73
- def self.[](*value)
74
- new(value)
75
- end
67
+ def self.[](*value) = new(value)
76
68
 
77
69
  alias_method :success, :value!
78
70
 
@@ -85,19 +77,13 @@ module Dry
85
77
  # Apply the second function to value.
86
78
  #
87
79
  # @api public
88
- def result(_, f)
89
- f.(@value)
90
- end
80
+ def result(_, f) = f.(@value)
91
81
 
92
82
  # Returns false
93
- def failure?
94
- false
95
- end
83
+ def failure? = false
96
84
 
97
85
  # Returns true
98
- def success?
99
- true
100
- end
86
+ def success? = true
101
87
 
102
88
  # Does the same thing as #bind except it also wraps the value
103
89
  # in an instance of Result::Success monad. This allows for easier
@@ -108,9 +94,7 @@ module Dry
108
94
  #
109
95
  # @param args [Array<Object>] arguments will be transparently passed through to #bind
110
96
  # @return [Result::Success]
111
- def fmap(...)
112
- Success.new(bind(...))
113
- end
97
+ def fmap(...) = Success.new(bind(...))
114
98
 
115
99
  # Returns result of applying first function to the internal value.
116
100
  #
@@ -120,9 +104,7 @@ module Dry
120
104
  # @param f [#call] Function to apply
121
105
  # @param _ [#call] Ignored
122
106
  # @return [Any] Return value of `f`
123
- def either(f, _)
124
- f.(success)
125
- end
107
+ def either(f, _) = f.(success)
126
108
 
127
109
  # @return [String]
128
110
  def to_s
@@ -144,9 +126,7 @@ module Dry
144
126
  # Ignores values and returns self, see {Failure#alt_map}
145
127
  #
146
128
  # @return [Result::Success]
147
- def alt_map(_ = nil)
148
- self
149
- end
129
+ def alt_map(_ = nil) = self
150
130
  end
151
131
 
152
132
  # Represents a value of a failed operation.
@@ -154,7 +134,7 @@ module Dry
154
134
  # @api public
155
135
  class Failure < Result
156
136
  include RightBiased::Left
157
- include Dry::Equalizer(:failure)
137
+ include ::Dry::Equalizer(:failure)
158
138
 
159
139
  singleton_class.alias_method(:call, :new)
160
140
 
@@ -194,26 +174,18 @@ module Dry
194
174
  end
195
175
 
196
176
  # @private
197
- def failure
198
- @value
199
- end
177
+ def failure = @value
200
178
 
201
179
  # Apply the first function to value.
202
180
  #
203
181
  # @api public
204
- def result(f, _)
205
- f.(@value)
206
- end
182
+ def result(f, _) = f.(@value)
207
183
 
208
184
  # Returns true
209
- def failure?
210
- true
211
- end
185
+ def failure? = true
212
186
 
213
187
  # Returns false
214
- def success?
215
- false
216
- end
188
+ def success? = false
217
189
 
218
190
  # If a block is given passes internal value to it and returns the result,
219
191
  # otherwise simply returns the first argument.
@@ -243,9 +215,7 @@ module Dry
243
215
  #
244
216
  # @param args [Array<Object>] arguments will be passed to the underlying `#or` call
245
217
  # @return [Result::Success] Wrapped value
246
- def or_fmap(...)
247
- Success.new(self.or(...))
248
- end
218
+ def or_fmap(...) = Success.new(self.or(...))
249
219
 
250
220
  # @return [String]
251
221
  def to_s
@@ -260,9 +230,7 @@ module Dry
260
230
  # Transform to a Success instance
261
231
  #
262
232
  # @return [Result::Success]
263
- def flip
264
- Success.new(@value)
265
- end
233
+ def flip = Success.new(@value)
266
234
 
267
235
  # @see RightBiased::Left#value_or
268
236
  def value_or(val = nil)
@@ -287,9 +255,7 @@ module Dry
287
255
  # @param _ [#call] Ignored
288
256
  # @param g [#call] Function to call
289
257
  # @return [Any] Return value of `g`
290
- def either(_, g)
291
- g.(failure)
292
- end
258
+ def either(_, g) = g.(failure)
293
259
 
294
260
  # Lifts a block/proc over Failure
295
261
  #
@@ -405,9 +371,7 @@ module Dry
405
371
  # @param fail [#call] Fallback value
406
372
  # @param block [Proc] Fallback block
407
373
  # @return [Success<Any>]
408
- def to_result(_fail = Unit)
409
- Result::Success.new(@value)
410
- end
374
+ def to_result(_fail = Unit) = Result::Success.new(@value)
411
375
  end
412
376
 
413
377
  class None < Maybe
@@ -442,9 +406,7 @@ module Dry
442
406
  class Try
443
407
  class Value < Try
444
408
  # @return [Result::Success]
445
- def to_result
446
- Dry::Monads::Result::Success.new(@value)
447
- end
409
+ def to_result = ::Dry::Monads::Result::Success.new(@value)
448
410
  end
449
411
 
450
412
  class Error < Try
@@ -460,9 +422,7 @@ module Dry
460
422
  # Converts to Result::Success
461
423
  #
462
424
  # @return [Result::Success]
463
- def to_result
464
- Result.pure(value!)
465
- end
425
+ def to_result = Result.pure(value!)
466
426
  end
467
427
 
468
428
  class Invalid < Validated
@@ -21,9 +21,7 @@ module Dry
21
21
  # Unwraps the underlying value
22
22
  #
23
23
  # @return [Object]
24
- def value!
25
- @value
26
- end
24
+ def value! = @value
27
25
 
28
26
  # Calls the passed in Proc object with value stored in self
29
27
  # and returns the result.
@@ -65,25 +63,19 @@ module Dry
65
63
  #
66
64
  # @param [Array<Object>] args arguments will be transparently passed through to #bind
67
65
  # @return [RightBiased::Right]
68
- def tee(...)
69
- bind(...).bind { self }
70
- end
66
+ def tee(...) = bind(...).bind { self }
71
67
 
72
68
  # Abstract method for lifting a block over the monad type.
73
69
  # Must be implemented for a right-biased monad.
74
70
  #
75
71
  # @return [RightBiased::Right]
76
- def fmap(*)
77
- raise NotImplementedError
78
- end
72
+ def fmap(...) = bind(...)
79
73
 
80
74
  # Ignores arguments and returns self. It exists to keep the interface
81
75
  # identical to that of {RightBiased::Left}.
82
76
  #
83
77
  # @return [RightBiased::Right]
84
- def or(*)
85
- self
86
- end
78
+ def or(...) = self
87
79
 
88
80
  # Ignores arguments and returns self. It exists to keep the interface
89
81
  # identical to that of {RightBiased::Left}.
@@ -91,24 +83,18 @@ module Dry
91
83
  # @param _alt [RightBiased::Right, RightBiased::Left]
92
84
  #
93
85
  # @return [RightBiased::Right]
94
- def |(_alt)
95
- self
96
- end
86
+ def |(_alt) = self
97
87
 
98
88
  # A lifted version of `#or`. For {RightBiased::Right} acts in the same way as `#or`,
99
89
  # that is returns itselt.
100
90
  #
101
91
  # @return [RightBiased::Right]
102
- def or_fmap(*)
103
- self
104
- end
92
+ def or_fmap(...) = self
105
93
 
106
94
  # Returns value. It exists to keep the interface identical to that of RightBiased::Left
107
95
  #
108
96
  # @return [Object]
109
- def value_or(_val = nil)
110
- @value
111
- end
97
+ def value_or(_val = nil) = @value
112
98
 
113
99
  # Applies the stored value to the given argument if the argument has type of Right,
114
100
  # otherwise returns the argument.
@@ -145,9 +131,7 @@ module Dry
145
131
  # # => Success(Unit)
146
132
  #
147
133
  # @return [RightBiased::Right]
148
- def discard
149
- fmap { Unit }
150
- end
134
+ def discard = fmap { Unit }
151
135
 
152
136
  # Removes one level of monad structure by joining two values.
153
137
  #
@@ -158,9 +142,7 @@ module Dry
158
142
  # Failure(:not_a_number).flatten # => Failure(:not_a_number)
159
143
  #
160
144
  # @return [RightBiased::Right,RightBiased::Left]
161
- def flatten
162
- bind(&:itself)
163
- end
145
+ def flatten = bind(&:itself)
164
146
 
165
147
  # Combines the wrapped value with another monadic value.
166
148
  # If both values are right-sided, yields a block and passes a tuple
@@ -252,38 +234,28 @@ module Dry
252
234
  module Left
253
235
  # @private
254
236
  # @return [String] Caller location
255
- def self.trace_caller
256
- caller_locations(2, 1)[0].to_s
257
- end
237
+ def self.trace_caller = caller_locations(2, 1)[0].to_s
258
238
 
259
239
  # Raises an error on accessing internal value
260
- def value!
261
- raise UnwrapError, self
262
- end
240
+ def value! = raise UnwrapError, self
263
241
 
264
242
  # Ignores the input parameter and returns self. It exists to keep the interface
265
243
  # identical to that of {RightBiased::Right}.
266
244
  #
267
245
  # @return [RightBiased::Left]
268
- def bind(*)
269
- self
270
- end
246
+ def bind(...) = self
271
247
 
272
248
  # Ignores the input parameter and returns self. It exists to keep the interface
273
249
  # identical to that of {RightBiased::Right}.
274
250
  #
275
251
  # @return [RightBiased::Left]
276
- def tee(*)
277
- self
278
- end
252
+ def tee(...) = self
279
253
 
280
254
  # Ignores the input parameter and returns self. It exists to keep the interface
281
255
  # identical to that of {RightBiased::Right}.
282
256
  #
283
257
  # @return [RightBiased::Left]
284
- def fmap(*)
285
- self
286
- end
258
+ def fmap(...) = self
287
259
 
288
260
  # Left-biased #bind version.
289
261
  #
@@ -293,18 +265,14 @@ module Dry
293
265
  # Dry::Monads.None.or { Time.now } # => current time
294
266
  #
295
267
  # @return [Object]
296
- def or(*)
297
- raise NotImplementedError
298
- end
268
+ def or(...) = raise NotImplementedError
299
269
 
300
270
  # Returns the passed value. Works in pair with {RightBiased::Right#|}.
301
271
  #
302
272
  # @param alt [RightBiased::Right, RightBiased::Left]
303
273
  #
304
274
  # @return [RightBiased::Right, RightBiased::Left]
305
- def |(alt)
306
- self.or(alt)
307
- end
275
+ def |(alt) = self.or(alt)
308
276
 
309
277
  # A lifted version of `#or`. This is basically `#or` + `#fmap`.
310
278
  #
@@ -313,9 +281,7 @@ module Dry
313
281
  # Dry::Monads.None.or_fmap { Time.now } # => Some(current time)
314
282
  #
315
283
  # @return [RightBiased::Left, RightBiased::Right]
316
- def or_fmap(*)
317
- raise NotImplementedError
318
- end
284
+ def or_fmap(...) = raise NotImplementedError
319
285
 
320
286
  # Returns the passed value
321
287
  #
@@ -332,33 +298,25 @@ module Dry
332
298
  # identical to that of {RightBiased::Right}.
333
299
  #
334
300
  # @return [RightBiased::Left]
335
- def apply(*)
336
- self
337
- end
301
+ def apply(...) = self
338
302
 
339
303
  # Returns self back. It exists to keep the interface
340
304
  # identical to that of {RightBiased::Right}.
341
305
  #
342
306
  # @return [RightBiased::Left]
343
- def discard
344
- self
345
- end
307
+ def discard = self
346
308
 
347
309
  # Returns self back. It exists to keep the interface
348
310
  # identical to that of {RightBiased::Right}.
349
311
  #
350
312
  # @return [RightBiased::Left]
351
- def flatten
352
- self
353
- end
313
+ def flatten = self
354
314
 
355
315
  # Returns self back. It exists to keep the interface
356
316
  # identical to that of {RightBiased::Right}.
357
317
  #
358
318
  # @return [RightBiased::Left]
359
- def and(_)
360
- self
361
- end
319
+ def and(_) = self
362
320
 
363
321
  # Pattern matching
364
322
  #
@@ -27,11 +27,11 @@ module Dry
27
27
  # @param block [Proc] a task to run
28
28
  # @return [Task]
29
29
  #
30
- def new(promise = nil, &block)
30
+ def new(promise = nil, &)
31
31
  if promise
32
32
  super(promise)
33
33
  else
34
- super(Promise.execute(&block))
34
+ super(Promise.execute(&))
35
35
  end
36
36
  end
37
37
 
@@ -50,8 +50,8 @@ module Dry
50
50
  # from concurrent-ruby
51
51
  #
52
52
  # @return [Task]
53
- def [](executor, &block)
54
- new(Promise.execute(executor: executor, &block))
53
+ def [](executor, &)
54
+ new(Promise.execute(executor: executor, &))
55
55
  end
56
56
 
57
57
  # Returns a completed task from the given value
@@ -73,9 +73,7 @@ module Dry
73
73
  #
74
74
  # @param exc [Exception]
75
75
  # @return [Task]
76
- def failed(exc)
77
- new(Promise.reject(exc))
78
- end
76
+ def failed(exc) = new(Promise.reject(exc))
79
77
  end
80
78
 
81
79
  include ConversionStubs[:to_maybe, :to_result]
@@ -110,9 +108,7 @@ module Dry
110
108
  # @param block [Proc]
111
109
  # @return [Task]
112
110
  # @api public
113
- def fmap(&block)
114
- self.class.new(promise.then(&block))
115
- end
111
+ def fmap(&) = self.class.new(promise.then(&))
116
112
 
117
113
  # Composes two tasks to run one after another.
118
114
  # A more common name is `then` exists as an alias.
@@ -148,9 +144,7 @@ module Dry
148
144
  #
149
145
  # @param block [Proc]
150
146
  # @return [Task]
151
- def or_fmap(&block)
152
- self.class.new(promise.rescue(&block))
153
- end
147
+ def or_fmap(&) = self.class.new(promise.rescue(&))
154
148
 
155
149
  # Rescues the error with a block that returns another task.
156
150
  #
@@ -180,9 +174,7 @@ module Dry
180
174
  #
181
175
  # @param block [Proc]
182
176
  # @return [Object]
183
- def value_or(&block)
184
- promise.rescue(&block).wait.value
185
- end
177
+ def value_or(&) = promise.rescue(&).wait.value
186
178
 
187
179
  # Blocks the current thread until the task is complete.
188
180
  #
@@ -206,21 +198,15 @@ module Dry
206
198
  # Whether the computation is complete.
207
199
  #
208
200
  # @return [Boolean]
209
- def complete?
210
- promise.complete?
211
- end
201
+ def complete? = promise.complete?
212
202
 
213
203
  # @return [Class]
214
- def monad
215
- Task
216
- end
204
+ def monad = Task
217
205
 
218
206
  # Returns self.
219
207
  #
220
208
  # @return [Maybe::Some, Maybe::None]
221
- def to_monad
222
- self
223
- end
209
+ def to_monad = self
224
210
 
225
211
  # Applies the stored value to the given argument.
226
212
  #
@@ -233,17 +219,15 @@ module Dry
233
219
  #
234
220
  # @param val [Task]
235
221
  # @return [Task]
236
- def apply(val = Undefined, &block)
237
- arg = Undefined.default(val, &block)
222
+ def apply(val = Undefined, &)
223
+ arg = Undefined.default(val, &)
238
224
  bind { |f| arg.fmap { curry(f).(_1) } }
239
225
  end
240
226
 
241
227
  # Maps a successful result to Unit, effectively discards it
242
228
  #
243
229
  # @return [Task]
244
- def discard
245
- fmap { Unit }
246
- end
230
+ def discard = fmap { Unit }
247
231
 
248
232
  private
249
233
 
@@ -302,9 +286,7 @@ module Dry
302
286
  #
303
287
  # @param block [Proc]
304
288
  # @return Task
305
- def Task(&block)
306
- Task.new(&block)
307
- end
289
+ def Task(&) = Task.new(&)
308
290
  end
309
291
 
310
292
  include Constructors
@@ -73,23 +73,17 @@ module Dry
73
73
  end
74
74
 
75
75
  # Returns true for an instance of a {Try::Value} monad.
76
- def value?
77
- is_a?(Value)
78
- end
76
+ def value? = is_a?(Value)
79
77
  alias_method :success?, :value?
80
78
 
81
79
  # Returns true for an instance of a {Try::Error} monad.
82
- def error?
83
- is_a?(Error)
84
- end
80
+ def error? = is_a?(Error)
85
81
  alias_method :failure?, :error?
86
82
 
87
83
  # Returns self.
88
84
  #
89
85
  # @return [Try::Value, Try::Error]
90
- def to_monad
91
- self
92
- end
86
+ def to_monad = self
93
87
 
94
88
  # Represents a result of a successful execution.
95
89
  #
@@ -168,9 +162,7 @@ module Dry
168
162
  # @param errors [Class] List of Exception subclasses
169
163
  #
170
164
  # @return [Try::Value]
171
- def recover(*_errors)
172
- self
173
- end
165
+ def recover(*_errors) = self
174
166
  end
175
167
 
176
168
  # Represents a result of a failed execution.
@@ -186,13 +178,11 @@ module Dry
186
178
  def initialize(exception)
187
179
  super()
188
180
 
189
- @exception = exception
181
+ @exception = @value = exception
190
182
  end
191
183
 
192
184
  # @return [String]
193
- def to_s
194
- "Try::Error(#{exception.class}: #{exception.message})"
195
- end
185
+ def to_s = "Try::Error(#{exception.class}: #{exception.message})"
196
186
  alias_method :inspect, :to_s
197
187
 
198
188
  # If a block is given passes internal value to it and returns the result,
@@ -215,9 +205,7 @@ module Dry
215
205
 
216
206
  # @param other [Try]
217
207
  # @return [Boolean]
218
- def ===(other)
219
- Error === other && exception === other.exception
220
- end
208
+ def ===(other) = Error === other && exception === other.exception
221
209
 
222
210
  # Acts in a similar way to `rescue`. It checks if
223
211
  # {exception} is one of {errors} and yields the block if so.
@@ -19,18 +19,9 @@ module Dry
19
19
  # => Some(Unit)
20
20
  #
21
21
  Unit = ::Object.new.tap do |unit|
22
- def unit.to_s
23
- "Unit"
24
- end
25
-
26
- def unit.inspect
27
- "Unit"
28
- end
29
-
30
- def unit.deconstruct
31
- EMPTY_ARRAY
32
- end
33
-
22
+ def unit.to_s = "Unit"
23
+ def unit.inspect = "Unit"
24
+ def unit.deconstruct = EMPTY_ARRAY
34
25
  unit.freeze
35
26
  end
36
27
  end