Class: Mayak::OptionalFunction
- Inherits:
-
Object
- Object
- Mayak::OptionalFunction
- Extended by:
- T::Generic, T::Helpers, T::Sig
- Defined in:
- lib/mayak/optional_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) ⇒ OptionalFunction
constructor
A new instance of OptionalFunction.
- #to_function ⇒ Object
- #to_proc ⇒ Object
Constructor Details
Class Method Details
.from_function(function) ⇒ Object
65 66 67 |
# File 'lib/mayak/optional_function.rb', line 65 def self.from_function(function) Mayak::OptionalFunction[T.type_parameter(:Input), T.type_parameter(:Output)].new { |input| function.call(input) } end |
Instance Method Details
#and_then(another) ⇒ Object Also known as: >>
38 39 40 41 42 |
# File 'lib/mayak/optional_function.rb', line 38 def and_then(another) Mayak::OptionalFunction[Input, T.type_parameter(:Output2)].new do |input| @blk.call(input).flat_map { |output| another.call(output) } end end |
#call(input) ⇒ Object
19 20 21 |
# File 'lib/mayak/optional_function.rb', line 19 def call(input) @blk.call(input) end |
#compose(another) ⇒ Object Also known as: <<
50 51 52 |
# File 'lib/mayak/optional_function.rb', line 50 def compose(another) Mayak::OptionalFunction[T.type_parameter(:Input2), Output].new { |input| another.call(input).flat_map { |new_input| @blk.call(new_input) } } end |
#to_function ⇒ Object
29 30 31 |
# File 'lib/mayak/optional_function.rb', line 29 def to_function Mayak::Function[Input, Mayak::Monads::Maybe[Output]].new { |input| call(input) } end |
#to_proc ⇒ Object
24 25 26 |
# File 'lib/mayak/optional_function.rb', line 24 def to_proc -> (input) { call(input) } end |