<commit_before>module HashDiff
  class Comparison
    def initialize(left, right)
      @left  = left
      @right = right
    end

    attr_reader :left, :right

    def diff
      @diff ||= find_differences { |l, r| [l, r] }
    end

    def left_diff
      @left_diff ||= find_differences { |_, r| r }
    end

    def right_diff
      @right_diff ||= find_differences { |l, _| l }
    end

    protected

    def find_differences(&reporter)
      combined_keys.each_with_object({ }, &comparison_strategy(reporter))
    end

    private

    def comparison_strategy(reporter)
      lambda do |key, diff|
        diff[key] = report_difference(key, reporter) unless equal?(key)
      end
    end

    def combined_keys
      (left.keys + right.keys).uniq
    end

    def equal?(key)
      value_with_default(left, key) == value_with_default(right, key)
    end

    def hash?(value)
      value.is_a?(Hash)
    end

    def comparable?(key)
      hash?(left[key]) && hash?(right[key])
    end

    def report_difference(key, reporter)
      if comparable?(key)
        self.class.new(left[key], right[key]).find_differences(&reporter)
      else
        reporter.call(
          value_with_default(left, key),
          value_with_default(right, key)
        )
      end
    end

    def value_with_default(obj, key)
      obj.fetch(key, NO_VALUE)
    end
  end
end<commit_msg>add ability to compare deep within arrays (#9)<commit_after>module HashDiff
  class Comparison
    def initialize(left, right)
      @left  = left
      @right = right
    end

    attr_reader :left, :right

    def diff
      @diff ||= find_differences { |l, r| [l, r] }
    end

    def left_diff
      @left_diff ||= find_differences { |_, r| r }
    end

    def right_diff
      @right_diff ||= find_differences { |l, _| l }
    end

    protected

    def find_differences(&reporter)
      combined_keys.each_with_object({ }, &comparison_strategy(reporter))
    end

    private

    def comparison_strategy(reporter)
      lambda do |key, diff|
        diff[key] = report_difference(key, reporter) unless equal?(key)
      end
    end

    def combined_keys
      if hash?(left) && hash?(right) then
        (left.keys + right.keys).uniq.sort
      elsif array?(left) && array?(right) then
        (0..[left.size, right.size].max).to_a
      else
        raise ArgumentError, "Don't know how to extract keys. Neither arrays nor hashes given"
      end
    end

    def equal?(key)
      value_with_default(left, key) == value_with_default(right, key)
    end

    def hash?(value)
      value.is_a?(Hash)
    end

    def array?(value)
      value.is_a?(Array)
    end

    def comparable_hash?(key)
      hash?(left[key]) && hash?(right[key])
    end

    def comparable_array?(key)
      array?(left[key]) && array?(right[key])
    end

    def report_difference(key, reporter)
      if comparable_hash?(key)
        self.class.new(left[key], right[key]).find_differences(&reporter)
      elsif comparable_array?(key)
        self.class.new(left[key], right[key]).find_differences(&reporter)        
      else
        reporter.call(
          value_with_default(left, key),
          value_with_default(right, key)
        )
      end
    end

    def value_with_default(obj, key)
      obj[key] || NO_VALUE
    end
  end
end

{'fraction_non_alphanumeric': 0.10920060331825038, 'fraction_numerical': 0.0006033182503770739, 'mean_word_length': 2.432712215320911, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1178561', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1081, 'n_words': 248}