<commit_before>module Shelike
  module Pipe
    def self.build_proc(proc, apply_proc, *args)
      case proc
      when Symbol, Method then -> { proc.to_proc.call(apply_proc.call, *args) }
      when Proc           then -> { proc.call(apply_proc.call, *args) }
      else raise ArgumentError
      end
    end

    refine Object do
      def |(arg)
        if arg.is_a? Array
          proc_or_method = arg.shift
          Shelike::Pipe.build_proc proc_or_method, -> { self }, *arg
        else
          Shelike::Pipe.build_proc arg, -> { self }
        end
      end
    end

    refine Array do
      def |(arg)
        if arg.is_a? Array
          proc_or_method = arg.shift
          Shelike::Pipe.build_proc proc_or_method, -> { self }, *arg
        else
          Shelike::Pipe.build_proc arg, -> { self }
        end
      end
    end

    refine Fixnum do
      def |(arg)
        if arg.is_a? Array
          proc_or_method = arg.shift
          Shelike::Pipe.build_proc proc_or_method, -> { self }, *arg
        else
          Shelike::Pipe.build_proc arg, -> { self }
        end
      end
    end

    refine Proc do
      def |(arg)
        if arg.is_a? Array
          proc_or_method = arg.shift
          Shelike::Pipe.build_proc proc_or_method, -> { call }, *arg
        else
          Shelike::Pipe.build_proc arg, -> { call }
        end
      end

      def ^(proc)
        call.tap { |r| raise Shelike::Pipe::Error::ResultNilError if r.nil? }
      rescue => e
        proc.call(e)
        nil
      end
    end

    refine NilClass do
      def |(_)
        nil
      end

      def ^(_)
        nil
      end
    end
  end
end<commit_msg>refactor.<commit_after>module Shelike
  module Pipe
    def self.build_proc(proc, apply_proc, *args)
      case proc
      when Symbol, Method then -> { proc.to_proc.call(apply_proc.call, *args) }
      when Proc           then -> { proc.call(apply_proc.call, *args) }
      else raise ArgumentError
      end
    end

    def self.pipe(object, arg)
      if arg.is_a? Array
        proc_or_method = arg.shift
        Shelike::Pipe.build_proc proc_or_method, -> { object }, *arg
      else
        Shelike::Pipe.build_proc arg, -> { object }
      end
    end

    refine Object do
      def |(arg)
        Shelike::Pipe.pipe(self, arg)
      end
    end

    refine Array do
      def |(arg)
        Shelike::Pipe.pipe(self, arg)
      end
    end

    refine Fixnum do
      def |(arg)
        Shelike::Pipe.pipe(self, arg)
      end
    end

    refine Proc do
      def |(arg)
        Shelike::Pipe.pipe(self, arg)
      end

      def ^(proc)
        call.tap { |r| raise Shelike::Pipe::Error::ResultNilError if r.nil? }
      rescue => e
        proc.call(e)
        nil
      end
    end

    refine NilClass do
      def |(_)
        nil
      end

      def ^(_)
        nil
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09714484679665739, 'fraction_numerical': 0.0, 'mean_word_length': 1.5859585958595859, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '3350500', 'n_tokens_mistral': 966, 'n_tokens_neox': 944, 'n_words': 239}