Module: DataModeler::Dataset::IteratingBasedOnNext

Included in:
Accessor, Generator
Defined in:
lib/data_modeler/dataset/helper.rb

Overview

Provides ‘#each` (which can return an `Iterator`) and `#to_a` based on `#next`

Instance Method Summary collapse

Instance Method Details

#eachnil|Iterator

Yields on each [inputs, targets] pair.

Returns:

  • (nil|Iterator)

    ‘block_given? ? nil : Iterator`



42
43
44
45
46
47
# File 'lib/data_modeler/dataset/helper.rb', line 42

def each
  reset_iteration
  return enum_for(:each) unless block_given?
  loop { yield self.next }
  nil
end

#mapArray|Iterator Also known as: collect

Yields on each ‘[inputs, targets]` pair, collecting the input.

Returns:

  • (Array|Iterator)

    ‘block_given? ? nil : Iterator`



51
52
53
54
55
# File 'lib/data_modeler/dataset/helper.rb', line 51

def map
  reset_iteration
  return enum_for(:collect) unless block_given?
  [].tap { |ret| loop { ret << yield(self.next) } }
end

#to_aArray

Returns:

  • (Array)


61
62
63
# File 'lib/data_modeler/dataset/helper.rb', line 61

def to_a
  each.to_a
end