Sha256: a1f49becc926320c332fee58b5602f61da564078b5a4854b1d52f0f8c28b5ab5

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module RedAmber
  # mix-in for the class DataFrame
  module Helper
    private

    def pl(num)
      num > 1 ? 's' : ''
    end

    def out_of_range?(indeces)
      indeces.max >= size || indeces.min < -size
    end

    def integers?(enum)
      enum.all?(Integer)
    end

    def booleans?(enum)
      enum.all? { |e| e.is_a?(TrueClass) || e.is_a?(FalseClass) || e.is_a?(NilClass) }
    end

    def create_dataframe_from_vector(key, vector)
      DataFrame.new(key => vector.data)
    end

    def parse_to_vector(args, vsize: size)
      a = args.reduce([]) do |accum, elem|
        accum.concat(normalize_element(elem, vsize: vsize))
      end
      Vector.new(a)
    end

    def normalize_element(elem, vsize: size)
      case elem
      when NilClass
        [nil]
      when Range
        bg = elem.begin
        en = elem.end
        if [bg, en].any?(Integer)
          bg += vsize if bg&.negative?
          en += vsize if en&.negative?
          en -= 1 if en.is_a?(Integer) && elem.exclude_end?
          if bg&.negative? || (en && en >= vsize)
            raise DataFrameArgumentError, "Index out of range: #{elem} for 0..#{vsize - 1}"
          end

          Array(0...vsize)[elem]
        elsif bg.nil? && en.nil?
          Array(0...vsize)
        else
          Array[elem]
        end
      when Enumerator
        elem.to_a
      else
        Array[elem]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
red_amber-0.2.2 lib/red_amber/helper.rb