Class: Mayak::Function

Inherits:
Object
  • Object
show all
Extended by:
T::Generic, T::Helpers, T::Sig
Defined in:
lib/mayak/function.rb

Constant Summary collapse

Input =
type_member
Output =
type_member

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Function

Returns a new instance of Function.



17
18
19
# File 'lib/mayak/function.rb', line 17

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

Instance Attribute Details

#blkObject (readonly)

Returns the value of attribute blk.



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

def blk
  @blk
end

Class Method Details

.from_proc(proc) ⇒ Object



31
32
33
# File 'lib/mayak/function.rb', line 31

def self.from_proc(proc)
  ::Mayak::Function[T.type_parameter(:Input), T.type_parameter(:Output)].new { |input| proc.call(input) }
end

Instance Method Details

#and_then(another) ⇒ Object Also known as: >>



50
51
52
# File 'lib/mayak/function.rb', line 50

def and_then(another)
  Mayak::Function[Input, T.type_parameter(:Output2)].new { |a| another.call(blk.call(a)) }
end

#call(input) ⇒ Object



41
42
43
# File 'lib/mayak/function.rb', line 41

def call(input)
  blk.call(input)
end

#compose(another) ⇒ Object Also known as: <<



60
61
62
# File 'lib/mayak/function.rb', line 60

def compose(another)
  Mayak::Function[T.type_parameter(:Input2), Output].new { |a| blk.call(another.call(a)) }
end

#to_procObject



36
37
38
# File 'lib/mayak/function.rb', line 36

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