Optimized callback ready bubble sort
@param array $list
@param callable|boolean $callback
@return array
543117
public static function byBubbleCallback($list, $callback = false)
    {
        $size = count($list);
        $sortedList = $list;

        if ($size > 0) {
            $normalizedList = [];
            // normalize list
            foreach ($list as $item) {
                $normalizedList[] = $item;
            }
            // optimized bubble sort
            while ($size > 0) {
                $newSize = 0;
                for ($i = 1; $i <= $size-1; $i++) {
                    // compare: a > b using callback
                    if ($callback) {
                        $swap = call_user_func_array($callback, ['a' => $normalizedList[$i-1], 'b' => $normalizedList[$i]]);
                    } else {
                        $swap = $normalizedList[$i-1] > $normalizedList[$i];
                    }
                    if ($swap) {
                        $itemA = $normalizedList[$i-1];
                        $itemB = $normalizedList[$i];
                        $normalizedList[$i-1] = $itemB;
                        $normalizedList[$i] = $itemA;
                        // swap
                        $newSize = $i;
                    }
                }
                $size = $newSize;
            }

            $sortedList = $normalizedList;
        }

        return $sortedList;
    }
392921
public static function sort($array, Closure $callback)
    {
        $results = [];

        foreach ($array as $key => $value) {
            $results[$key] = $callback($value);
        }

        return $results;
    }
948867
public function sort( $callback ) {
		$elements = $this->elements;
		usort( $elements, $callback );
		return $this->new_from_trusted( $elements );
	}
866077
public static function map(array $array, callable $callback)
    {
        $newArray = [];

        foreach ($array as $key => $item) {
            $result = $callback($item, $key);

            $newArray = is_array($result) ?
                array_replace_recursive($array, $result) :
                array_merge_recursive($array, (array) $result);
        }

        return $newArray;
    }
657886
public function sortable(\Closure $callback = null)
    {
        return tap($this, function ($element) use ($callback) {
            app('scaffold.module')->addSortable(
                $element->id(),
                $callback
            );
        });
    }
514487
public function chunk( $count, callable $callback )
	{
		$results = $this->forPage( $page = 1, $count )->get();

		while ( count( $results ) > 0 )
		{
			// On each chunk result set, we will pass them to the callback and then let the
			// developer take care of everything within the callback, which allows us to
			// keep the memory low for spinning through large result sets for working.
			if ( call_user_func( $callback, $results ) === false )
			{
				return false;
			}

			$page++;

			$results = $this->forPage( $page, $count )->get();
		}

		return true;
	}
256751
static public function callback($array, $callback)
	{
	    if (is_array($array)) foreach ($array as $k=>$v) {
	        if (is_array($v)) {
	            $array[$k] = static::callback($v, $callback);
	        } else {
	            $array[$k] = $callback($v);
	        }
	    }
	    return $array;
	}
534124
protected function filterByKey(array $array, callable $callback)
    {
        $newArray = [];
        foreach ($array as $key => $value) {
            if (true === $callback($key, $value)) {
                $newArray[$key] = $value;
            }
        }

        return $newArray;
    }
1236483
private function getCallback($callback)
    {
        if (is_callable($callback)) {
            if (is_array($callback)) {
                $callback = function (Edge $edge) use ($callback) {
                    return call_user_func($callback, $edge);
                };
            }
            return $callback;
        }

        static $methods = array(
            self::ORDER_WEIGHT => 'getWeight',
            self::ORDER_CAPACITY => 'getCapacity',
            self::ORDER_CAPACITY_REMAINING => 'getCapacityRemaining',
            self::ORDER_FLOW => 'getFlow'
        );

        if (!is_int($callback) || !isset($methods[$callback])) {
            throw new InvalidArgumentException('Invalid callback given');
        }

        $method = $methods[$callback];

        return function (Edge $edge) use ($method) {
            return $edge->$method();
        };
    }
92950
public function each(callable $callback = null)
	{
		foreach($this->list as $item)
		{
			if(call_user_func($callback, $item) === false)
				return $this;
		}
		
		return $this;
	}
1112824
public static function mergeSort(&$array, \Closure $callback)
    {
        // Arrays of size < 2 require no action.
        if (count($array) < 2) {
            return;
        }

        // Split the array in half
        $halfway = count($array) / 2;
        $array1 = array_slice($array, 0, $halfway);
        $array2 = array_slice($array, $halfway);

        // Recurse to sort the two halves
        self::mergeSort($array1, $callback);
        self::mergeSort($array2, $callback);

        // If all of $array1 is <= all of $array2, just append them.
        if (call_user_func($callback, end($array1), $array2[0]) < 1) {
            $array = array_merge($array1, $array2);

            return;
        }

        // Merge the two sorted arrays into a single sorted array
        $array = [];
        $ptr1 = $ptr2 = 0;

        while ($ptr1 < count($array1) && $ptr2 < count($array2)) {
            if ($callback($array1[$ptr1], $array2[$ptr2]) < 1) {
                $array[] = $array1[$ptr1++];
            } else {
                $array[] = $array2[$ptr2++];
            }
        }
        // Merge the remainder
        while ($ptr1 < count($array1)) {
            $array[] = $array1[$ptr1++];
        }

        while ($ptr2 < count($array2)) {
            $array[] = $array2[$ptr2++];
        }
    }
464696
protected function prepareCallbacks(callable $callback/*, ... */) {
		list($init, $done, $then) = $this->getCallbacks();
		
		$context = $init();
		foreach (func_get_args() as $cb) {
			$then($context, $cb);
		}
		
		return array($context, function($result) use ($context, $done) {
			$done($context, $result);
		});
	}
2580
public function sort(callable $callback = null) : Stack
    {
        $values = $this->_sort($callback);
        $this->setValues($values);
        return $this;
    }
432021
public function filter($callback, $in_place = false)
    {
        if (!is_callable($callback)) {
            throw new \InvalidArgumentException('ItemList::filter() expects a callable argument.');
        }
        $filtered = array_values(array_filter($this->list, $callback));

        if ($in_place) {
            $this->list = $filtered;

            return $this;
        }

        return new ItemList($filtered);
    }
1104347
public function uksort(callable $callback): Collection
    {
        $items = $this->items;
        $keys = array_combine(array_keys($items), range(1, count($items)));

        uksort($items, function ($a, $b) use ($callback, $keys) {
            $result = call_user_func($callback, $a, $b);

            return $result === 0 ? $keys[$a] - $keys[$b] : $result;
        });

        return new static($items);
    }
797045
public function filter( $callback ) {

			$result = [];

			if ( is_callable( $callback ) ) {

				for ( $i=0; $i<$this->len; $i++ ) {
					if ( call_user_func( $callback, $this->items[$i], $i, $this ) ) {
						$result[] = &$this->items[$i];
					}
				}

			}

			$myName = get_class();

			return new $myName( $result );

		}
133030
protected function prepareListOrder(array &$list)
    {
        foreach ($list as &$item) {
            $this->setItemOrderNew($item, $this->order_history);
            $this->setItemTotalFormatted($item, $this->price);
        }
    }