Class: Mayak::Lazy

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

Constant Summary collapse

Value =
type_member

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&blk) ⇒ Lazy

Returns a new instance of Lazy.



14
15
16
17
18
# File 'lib/mayak/lazy.rb', line 14

def initialize(&blk)
  @thunk = T.let(blk, T.proc.returns(Value))
  @value = T.let(nil, T.nilable(Value))
  @forced = T.let(false, T::Boolean)
end

Class Method Details

.combine_five(first, second, third, fourth, fifth, &blk) ⇒ Object



195
196
197
198
199
# File 'lib/mayak/lazy.rb', line 195

def self.combine_five(first, second, third, fourth, fifth, &blk)
  ::Mayak::Lazy[T.type_parameter(:ResultValue)].new do
    blk.call(first.value, second.value, third.value, fourth.value, fifth.value)
  end
end

.combine_four(first, second, third, fourth, &blk) ⇒ Object



162
163
164
165
166
# File 'lib/mayak/lazy.rb', line 162

def self.combine_four(first, second, third, fourth, &blk)
  ::Mayak::Lazy[T.type_parameter(:ResultValue)].new do
    blk.call(first.value, second.value, third.value, fourth.value)
  end
end

.combine_many(lazies, initial, &blk) ⇒ Object



288
289
290
291
292
# File 'lib/mayak/lazy.rb', line 288

def self.combine_many(lazies, initial, &blk)
  ::Mayak::Lazy[T.type_parameter(:Result)].new do
    lazies.reduce(initial) { |acc, element| blk.call(acc, element.value) }
  end
end

.combine_seven(first, second, third, fourth, fifth, sixth, seventh, &blk) ⇒ Object



270
271
272
273
274
# File 'lib/mayak/lazy.rb', line 270

def self.combine_seven(first, second, third, fourth, fifth, sixth, seventh, &blk)
  ::Mayak::Lazy[T.type_parameter(:ResultValue)].new do
    blk.call(first.value, second.value, third.value, fourth.value, fifth.value, sixth.value, seventh.value)
  end
end

.combine_six(first, second, third, fourth, fifth, sixth, &blk) ⇒ Object



231
232
233
234
235
# File 'lib/mayak/lazy.rb', line 231

def self.combine_six(first, second, third, fourth, fifth, sixth, &blk)
  ::Mayak::Lazy[T.type_parameter(:ResultValue)].new do
    blk.call(first.value, second.value, third.value, fourth.value, fifth.value, sixth.value)
  end
end

.combine_three(first, second, third, &blk) ⇒ Object



132
133
134
135
136
# File 'lib/mayak/lazy.rb', line 132

def self.combine_three(first, second, third, &blk)
  ::Mayak::Lazy[T.type_parameter(:ResultValue)].new do
    blk.call(first.value, second.value, third.value)
  end
end

.combine_two(first, second, &blk) ⇒ Object



105
106
107
108
109
# File 'lib/mayak/lazy.rb', line 105

def self.combine_two(first, second, &blk)
  ::Mayak::Lazy[T.type_parameter(:ResultValue)].new do
    blk.call(first.value, second.value)
  end
end

.sequence(lazies) ⇒ Object



83
84
85
# File 'lib/mayak/lazy.rb', line 83

def self.sequence(lazies)
  ::Mayak::Lazy[T::Array[T.type_parameter(:Value)]].new { lazies.map(&:value) }
end

Instance Method Details

#combine(another, &blk) ⇒ Object



68
69
70
71
72
# File 'lib/mayak/lazy.rb', line 68

def combine(another, &blk)
  ::Mayak::Lazy[T.type_parameter(:NewValue)].new do
    blk.call(value, another.value)
  end
end

#flat_map(&blk) ⇒ Object



53
54
55
# File 'lib/mayak/lazy.rb', line 53

def flat_map(&blk)
  ::Mayak::Lazy.new { blk.call(value).value }
end

#map(&blk) ⇒ Object



40
41
42
# File 'lib/mayak/lazy.rb', line 40

def map(&blk)
  ::Mayak::Lazy.new { blk.call(value) }
end

#valueObject



21
22
23
24
25
26
27
28
29
# File 'lib/mayak/lazy.rb', line 21

def value
  if @forced
    T.must(@value)
  else
    @forced = true
    @value = @thunk.call
    @value
  end
end