Forelem: A Versatile Optimization Framework For Tuple-Based Computations