Module: Roast::SystemCogs::Map::InputContext
- Included in:
- CogInputContext
- Defined in:
- lib/roast/system_cogs/map.rb
Overview
@requires_ancestor: Roast::CogInputContext
Instance Method Summary collapse
-
#collect(map_cog_output, &block) ⇒ Object
Collect the results from all ‘map` cog iterations into an array.
-
#reduce(map_cog_output, initial_value = nil, &block) ⇒ Object
Reduce the results from all ‘map` cog iterations to a single value.
Instance Method Details
#collect(map_cog_output, &block) ⇒ Object
Collect the results from all ‘map` cog iterations into an array
Extracts the final output from each iteration that ran. When called without a block, returns an array of the final outputs directly. When called with a block, executes the block in the context of each iteration’s input context, receiving the final output, the original item value, and the iteration index as arguments.
Iterations that did not run (due to ‘break!`) will be represented as `nil` in the returned array.
#### Usage “‘ruby # Get all final outputs directly results = collect(map!(:process_items))
# Transform each output with access to the original item and index results = collect(map!(:process_items)) do |output, item, index|
{ item: item, result: output, position: index }
end
# Access other cog outputs from within each iteration results = collect(map!(:process_items)) do |output, item, index|
inner_cog!(:some_step)
end “‘
#### See Also
-
‘reduce`
-
‘Roast::SystemCogs::Map::Output`
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/roast/system_cogs/map.rb', line 375 def collect(map_cog_output, &block) ems = map_cog_output.instance_variable_get(:@execution_managers) raise CogInputContext::ContextNotFoundError if ems.nil? return ems.map do |em| next unless em scope_value = em.instance_variable_get(:@scope_value) scope_index = em.instance_variable_get(:@scope_index) final_output = em.final_output em.cog_input_context.instance_exec(final_output, scope_value, scope_index, &block) end if block_given? ems.map { |em| em&.final_output } end |
#reduce(map_cog_output, initial_value = nil, &block) ⇒ Object
Reduce the results from all ‘map` cog iterations to a single value
Processes each iteration’s output sequentially, combining them into an accumulator value. The block receives the current accumulator value, the final output from the iteration, the original item value, and the iteration index. The block should return the new accumulator value.
If the block returns ‘nil`, the accumulator will __not__ be updated (preserving any previous non-nil value). This prevents accidental overwrites with `nil` values.
Iterations that did not run (due to ‘break!`) are skipped.
#### Usage “‘ruby # Sum all outputs total = reduce(map!(:calculate_scores), 0) do |sum, output, item, index|
sum + output
end
# Build a hash from outputs results = reduce(map!(:process_items), {}) do |hash, output, item, index|
hash.merge(item => output)
end
# Collect with conditional accumulation valid_results = reduce(map!(:validate_items), []) do |acc, output, item, index|
output.valid? ? acc + [output] : acc
end “‘
#### See Also
-
‘collect`
-
‘Roast::SystemCogs::Map::Output`
: [A] (Roast::SystemCogs::Map::Output, ?A?) untyped) -> A -> A?
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 |
# File 'lib/roast/system_cogs/map.rb', line 426 def reduce(map_cog_output, initial_value = nil, &block) ems = map_cog_output.instance_variable_get(:@execution_managers) raise CogInputContext::ContextNotFoundError if ems.nil? accumulator = initial_value ems.compact.each do |em| next unless em scope_value = em.instance_variable_get(:@scope_value) scope_index = em.instance_variable_get(:@scope_index) final_output = em.final_output new_accumulator = em.cog_input_context.instance_exec(accumulator, final_output, scope_value, scope_index, &block) case new_accumulator when nil # do not overwrite a non-nil value in the accumulator with a nil value, # even if one is returned from the block else accumulator = new_accumulator #: as A end end accumulator end |