<commit_before>require 'lou/version'

module Lou
  def self.extended(base)
    base.class_eval do
      @forward_transforms = []
      @backward_transforms = []
    end
  end

  def transform(mapping)
    @forward_transforms << mapping.fwd
    @backward_transforms << mapping.bwd
    self
  end

  def apply(input)
    output = deep_clone(input)
    @forward_transforms.each do |t|
      output = t.call(output)
    end
    output
  end

  def undo(output)
    input = deep_clone(output)
    @backward_transforms.reverse_each do |t|
      input = t.call(input)
    end
    input
  end

  def deep_clone(obj)
    Marshal.load(Marshal.dump(obj))
  end

  def forward(&block)
    Transformer.new(&block)
  end

  class Transformer
    def initialize(&block)
      forward(&block)
    end

    def forward(&block)
      @forward = block
      self
    end

    def backward(&block)
      @backward = block
      self
    end

    def fwd
      @forward
    end

    def bwd
      @backward
    end
  end
end
<commit_msg>Refactored transforms so that only one internal list is maintained<commit_after>require 'lou/version'

module Lou
  def self.extended(base)
    base.class_eval do
      @transforms = []
    end
  end

  def transform(mapping)
    @transforms << mapping
    self
  end

  def apply(input)
    output = deep_clone(input)
    @transforms.each do |t|
      output = t.apply(output)
    end
    output
  end

  def undo(output)
    input = deep_clone(output)
    @transforms.reverse_each do |t|
      input = t.undo(input)
    end
    input
  end

  def deep_clone(obj)
    Marshal.load(Marshal.dump(obj))
  end

  def forward(&block)
    Transformer.new(&block)
  end

  class Transformer
    def initialize(&block)
      forward(&block)
    end

    def forward(&block)
      @forward = block
      self
    end

    def backward(&block)
      @backward = block
      self
    end

    def apply(input)
      @forward.nil? ? input : @forward.call(input)
    end

    def undo(output)
      @backward.nil? ? output : @backward.call(output)
    end
  end
end

{'fraction_non_alphanumeric': 0.0893719806763285, 'fraction_numerical': 0.0, 'mean_word_length': 2.329581993569132, 'pattern_counts': {'":': 0, '<': 9, '<?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': '3157262', 'n_tokens_mistral': 702, 'n_tokens_neox': 675, 'n_words': 170}