# frozen_string_literal: true

require 'list_math/converter_factory'

module ListMath
  # Math library for lists of strings
  class Math
    class << self
      def sum(list = [''])
        convert(list).sum
      end

      def subtract(list = [''])
        sum(convert(list).first(1)) - sum(convert(list).drop(1))
      end

      def product(list = [''])
        return 0 if list.empty?

        convert(list).reduce(1) { |a, c| a * c }
      end

      private

      def convert(list = [''])
        list.map { |s| ConverterFactory.for(s) }
            .map(&:call)
      end
    end
  end
end
