public static function optimize($operations)
    {
        $newOperations = [];
        /** @var $outOperation Operation */
        foreach ($operations as $outIndex => $outOperation) {
            $inOperations = array_slice($operations, $outIndex + 1);

            if ($inOperations) {
                foreach ($inOperations as $inIndex => $inOperation) {
                    // get how many items to fetch
                    $length = $inIndex;

                    $inBetween = array_slice($operations, $outIndex + 1, $length);

                    $result = $outOperation->reduce($inOperation, $inBetween);

                    if ($result) {
                        // add the result of the two merging
                        $newOperations = array_merge($newOperations, $result);
                        // add points that fell in between those that merged
                        $newOperations = array_merge($newOperations, $inBetween);
                        // add points that come after
                        $newOperations = array_merge(
                            $newOperations,
                            array_slice($operations, $outIndex + $inIndex + 2)
                        );

                        return $newOperations;
                    }
                }
                $newOperations[] = $outOperation;
            } else {
                $newOperations[] = $outOperation;
            }
        }

        return $newOperations;
    }