@function __before-callback($arguments...) {
    $n: __this('n');
    $function: __this('function');
    $result: null;
    $n: $n - 1;
    $_: __this(n, $n);

    @if ($n > 0) {
        $result: __call($function, __this(), $arguments...);
    } @else {
        $_: __this('function', null);
    }

    @return $result;
}


@function __before($n, $function) {
    @if not (__function-exists($function)) {
        @if (__function-exists($n)) {
            $temp: $n;
            $n: $function;
            $function: $temp;
        } @else {
            @error 'FUNC_ERROR_TEXT';
        }
    }

    $_: __scope((
        'function': $function,
        'n': $n
    ));
    $after-callback: __bind('__before-callback');
    $_: __scope(false);

    @return $after-callback;
}


/// Creates a function that invokes `$function`, with the `_this` binding and arguments
/// of the created function, while it is called less than `$n` times. Subsequent
/// calls to the created function return the result of the last `$function` invocation.
///
///
/// @access public
/// @group Function
/// @param {number} $n The number of calls at which `$function` is no longer invoked.
/// @param {Function} $func The function to restrict.
/// @returns {Function} Returns the new restricted function.
/// @example scss
///     @function limit-mixin() { @return true; }
///
///     $limit-mixin: _before(5, 'limit-mixin');
///
///     @mixin my-mixin() {
///         @if _exec($limit-mixin) {
///             // ... include mixin
///         } @else {
///             @extend %placeholder; // extend after mixin used 4 times
///         }
///     }
@function _before($args...) {
    @return call(get-function('__before'), $args...);
}
