<commit_before>module CouchRB
  class ViewContext
    attr_reader :results

    def initialize(db, functions = {})
      @db = db
      @map = functions[:map]
      @reduce = functions[:reduce]
      @results = []
    end

    def run_map
      @db.docs.each{|doc| yield(doc.value) }
    end

    def run_reduce
      keys, values = [], []

      @results.each do |result|
        keys << result['key']
        values << result['value']
      end

      @results = []
      results << {'value' => yield(keys, values)}
    end

    def run
      if @map
        run_map(&instance_eval(@map))
        if @reduce
          run_reduce(&instance_eval(@reduce))
        end
      end

      self
    end<commit_msg>Don't let the view modify the original document<commit_after>module CouchRB
  class ViewContext
    attr_reader :results

    def initialize(db, functions = {})
      @db = db
      @map = functions[:map]
      @reduce = functions[:reduce]
      @results = []
    end

    def run_map
      @db.docs.each{|doc| yield(doc.value.dup) }
    end

    def run_reduce
      keys, values = [], []

      @results.each do |result|
        keys << result['key']
        values << result['value']
      end

      @results = []
      results << {'value' => yield(keys, values)}
    end

    def run
      if @map
        run_map(&instance_eval(@map))
        if @reduce
          run_reduce(&instance_eval(@reduce))
        end
      end

      self
    end
{'fraction_non_alphanumeric': 0.12225274725274725, 'fraction_numerical': 0.0, 'mean_word_length': 1.9198396793587174, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '6171860', 'n_tokens_mistral': 448, 'n_tokens_neox': 432, 'n_words': 110}