Class: Mayak::FailableFunction

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

Constructor Details

#initialize(&blk) ⇒ FailableFunction

Returns a new instance of FailableFunction.



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

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

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_functionObject



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_procObject



42
43
44
# File 'lib/mayak/failable_function.rb', line 42

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