All pastes #2061777 Raw Edit

Unnamed

public text v1 · immutable
#2061777 ·published 2011-05-16 19:55 UTC
rendered paste body
#!/usr/bin/env python

ZEROINT = 0

def minus1(x):
  return x-1

def plus1(x):
  return x+1

def new(cls, *args, **kwargs):
  return cls(*args, **kwargs)

def applyFunction(function, *args, **kwargs):
  return function(*args, **kwargs)

def returnfunction(item):
  function = item
  return function

class xNotIntError(Exception):
  pass

class yNotIntError(Exception):
  pass

def recursiveAddClassFactory(addfunction, minusfunction):

  global ZEROINT

  class AddClass(object):

    def __init__(self):
      self.addfunction = addfunction
      self.minusfunction = minusfunction

    def __call__(self, x, y):
      if isinstance(x, int):
        if isinstance(y, int):
          if y == ZEROINT:
            return x
          xadd1 = applyFunction(self.addfunction, x)
          yminus1 = applyFunction(self.minusfunction, y)
          newresult = applyFunction(self, xadd1, yminus1)
          return newresult
        else:
          raise yNotIntError
      else:
        raise xNotIntError

  return new(AddClass)

ADDFUNCTION = plus1
MINUSFUNCTION = minus1

adder = recursiveAddClassFactory(ADDFUNCTION, MINUSFUNCTION)

@returnfunction
def add_pos(x, y):
  try:
    result = applyFunction(adder, x, y)
  except xNotIntError:
    return x + y
  except yNotIntError:
    return x + y
  finally:
    return x + y