Class: Deterministic::Result
- Includes:
- Monad, PatternMatching
- Defined in:
- lib/deterministic/result.rb
Overview
Abstract parent of Success and Failure
Defined Under Namespace
Modules: PatternMatching Classes: Failure, Success
Instance Method Summary collapse
-
#and(other) ⇒ Object
‘pipe(self: Result(a), op: |Result(a)| -> b) -> Result(a)` Replaces `Success a` with `Result b`.
-
#and_then(&block) ⇒ Object
‘and_then(self: Success(a), op: |a| -> Result(b)) -> Result(b)` Replaces `Success a` with the result of the block.
- #failure? ⇒ Boolean
-
#map(proc = nil, &block) ⇒ Object
(also: #>>)
‘map(self: Success(a), op: |a| -> Result(b)) -> Result(b)` Maps a `Success` with the value `a` to another `Result` with the value `b`.
-
#map_err(proc = nil, &block) ⇒ Object
‘map_err(self: Failure(a), op: |a| -> Result(b)) -> Result(b)` Maps a `Failure` with the value `a` to another `Result` with the value `b`.
-
#or(other) ⇒ Object
‘or(self: Failure(a), other: Result(b)) -> Result(b)` Replaces `Failure a` with `Result`.
-
#or_else(&block) ⇒ Object
‘or_else(self: Failure(a), op: |a| -> Result(b)) -> Result(b)` Replaces `Failure a` with the result of the block.
-
#pipe(proc = nil, &block) ⇒ Object
(also: #**)
‘pipe(self: Result(a), op: |Result(a)| -> b) -> Result(a)` Executes the block passed, but completely ignores its result.
- #success? ⇒ Boolean
-
#try(proc = nil, &block) ⇒ Object
(also: #>=)
‘pipe(self: Result(a), op: |Result(a)| -> b) -> Result(a)` Executes the block passed, but completely ignores its result.
Methods included from PatternMatching
Methods included from Monad
#==, #bind, #fmap, #initialize, #inspect, #join, #to_s, #value
Instance Method Details
#and(other) ⇒ Object
‘pipe(self: Result(a), op: |Result(a)| -> b) -> Result(a)` Replaces `Success a` with `Result b`. If a `Failure` is passed as argument, it is ignored.
46 47 48 49 50 |
# File 'lib/deterministic/result.rb', line 46 def and(other) return self if failure? raise NotMonadError, "Expected #{other.inspect} to be an Result" unless other.is_a? Result other end |
#and_then(&block) ⇒ Object
‘and_then(self: Success(a), op: |a| -> Result(b)) -> Result(b)` Replaces `Success a` with the result of the block. If a `Failure` is passed as argument, it is ignored.
54 55 56 57 |
# File 'lib/deterministic/result.rb', line 54 def and_then(&block) return self if failure? bind(&block) end |
#failure? ⇒ Boolean
31 32 33 |
# File 'lib/deterministic/result.rb', line 31 def failure? is_a? Failure end |
#map(proc = nil, &block) ⇒ Object Also known as: >>
‘map(self: Success(a), op: |a| -> Result(b)) -> Result(b)` Maps a `Success` with the value `a` to another `Result` with the value `b`. It works like `#bind` but only on `Success`.
76 77 78 79 |
# File 'lib/deterministic/result.rb', line 76 def map(proc=nil, &block) return self if failure? bind(proc || block) end |
#map_err(proc = nil, &block) ⇒ Object
‘map_err(self: Failure(a), op: |a| -> Result(b)) -> Result(b)` Maps a `Failure` with the value `a` to another `Result` with the value `b`. It works like `#bind` but only on `Failure`.
85 86 87 88 |
# File 'lib/deterministic/result.rb', line 85 def map_err(proc=nil, &block) return self if success? bind(proc || block) end |
#or(other) ⇒ Object
‘or(self: Failure(a), other: Result(b)) -> Result(b)` Replaces `Failure a` with `Result`. If a `Failure` is passed as argument, it is ignored.
61 62 63 64 65 |
# File 'lib/deterministic/result.rb', line 61 def or(other) return self if success? raise NotMonadError, "Expected #{other.inspect} to be an Result" unless other.is_a? Result return other end |
#or_else(&block) ⇒ Object
‘or_else(self: Failure(a), op: |a| -> Result(b)) -> Result(b)` Replaces `Failure a` with the result of the block. If a `Success` is passed as argument, it is ignored.
69 70 71 72 |
# File 'lib/deterministic/result.rb', line 69 def or_else(&block) return self if success? bind(&block) end |
#pipe(proc = nil, &block) ⇒ Object Also known as: **
‘pipe(self: Result(a), op: |Result(a)| -> b) -> Result(a)` Executes the block passed, but completely ignores its result. If an error is raised within the block it will NOT be catched.
37 38 39 40 |
# File 'lib/deterministic/result.rb', line 37 def pipe(proc=nil, &block) (proc || block).call(self) self end |
#success? ⇒ Boolean
27 28 29 |
# File 'lib/deterministic/result.rb', line 27 def success? is_a? Success end |
#try(proc = nil, &block) ⇒ Object Also known as: >=
‘pipe(self: Result(a), op: |Result(a)| -> b) -> Result(a)` Executes the block passed, but completely ignores its result. If an error is raised within the block it will NOT be catched.
92 93 94 95 96 |
# File 'lib/deterministic/result.rb', line 92 def try(proc=nil, &block) map(proc, &block) rescue => err Failure(err) end |