Class: Mayak::OptionalFunction

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(&blk) ⇒ OptionalFunction

Returns a new instance of OptionalFunction.



14
15
16
# File 'lib/mayak/optional_function.rb', line 14

def initialize(&blk)
  @blk = T.let(blk, T.proc.params(input: Input).returns(Mayak::Monads::Maybe[Output]))
end

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_functionObject



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_procObject



24
25
26
# File 'lib/mayak/optional_function.rb', line 24

def to_proc
  -> (input) { call(input) }
end