Class: Mayak::FailableFunction
- Inherits:
-
Object
- Object
- Mayak::FailableFunction
- Extended by:
- T::Generic, T::Helpers, T::Sig
- Defined in:
- lib/mayak/failable_function.rb
Constant Summary collapse
- Input =
type_member
- Output =
type_member
Class Method Summary collapse
Instance Method Summary collapse
- #and_then(another) ⇒ Object (also: #>>)
- #call(input) ⇒ Object
- #compose(another) ⇒ Object (also: #<<)
-
#initialize(&blk) ⇒ FailableFunction
constructor
A new instance of FailableFunction.
- #to_function ⇒ Object
- #to_proc ⇒ Object
Constructor Details
Class Method Details
.from_function(function) ⇒ Object
83 84 85 |
# File 'lib/mayak/failable_function.rb', line 83 def self.from_function(function) Mayak::FailableFunction[T.type_parameter(:Input), T.type_parameter(:Output)].new { |input| function.call(input) } end |
.from_proc(proc) ⇒ Object
32 33 34 |
# File 'lib/mayak/failable_function.rb', line 32 def self.from_proc(proc) ::Mayak::FailableFunction[T.type_parameter(:Input), T.type_parameter(:Output)].new { |input| proc.call(input) } end |
Instance Method Details
#and_then(another) ⇒ Object Also known as: >>
56 57 58 59 60 |
# File 'lib/mayak/failable_function.rb', line 56 def and_then(another) Mayak::FailableFunction[Input, T.type_parameter(:Output2)].new do |input| @blk.call(input).flat_map { |output| another.call(output) } end end |
#call(input) ⇒ Object
37 38 39 |
# File 'lib/mayak/failable_function.rb', line 37 def call(input) @blk.call(input) end |
#compose(another) ⇒ Object Also known as: <<
68 69 70 |
# File 'lib/mayak/failable_function.rb', line 68 def compose(another) Mayak::FailableFunction[T.type_parameter(:Input2), Output].new { |input| another.call(input).flat_map { |new_input| @blk.call(new_input) } } end |
#to_function ⇒ Object
47 48 49 |
# File 'lib/mayak/failable_function.rb', line 47 def to_function Mayak::Function[Input, Mayak::Monads::Try[Output]].new { |input| call(input) } end |
#to_proc ⇒ Object
42 43 44 |
# File 'lib/mayak/failable_function.rb', line 42 def to_proc -> (input) { call(input) } end |