Hacker News new | ask | show | jobs
by Svetlitski 725 days ago
You can implement pipes via operator overloading in Python. Whether or not you should is a different question:

  class Pipe:
      __slots__ = ("value",)
  
      def __init__(self):
          self.value = None
  
      def __or__(self, x):
          return x(self.value)
  
      def __ror__(self, x):
          self.value = x
          return self
  
  v = Pipe()
  
  def double(x):
      return x + x
  
  def triple(x):
      return x + x + x
  
  pipeline_result = 3 + 7 |v| double |v| triple |v| double
  normal_result = double(triple(double(3+7)))
  assert pipeline_result == normal_result