<commit_before><?php

namespace Amp {
    use React\Promise\PromiseInterface as ReactPromise;

    /**
     * Wraps the callback in a promise/coroutine-aware function that automatically upgrades Generators to coroutines and
     * calls rethrow() on the returned promises (or the coroutine created).
     *
     * @param callable (...$args): \Generator|\Amp\Promise|mixed $callback
     *
     * @return callable(...$args): void
     */
    function wrap(callable $callback): callable {
        return function (...$args) use ($callback) {
            $result = $callback(...$args);

            if ($result instanceof \Generator) {
                $result = new Coroutine($result);
            }

            if ($result instanceof Promise || $result instanceof ReactPromise) {
                Promise\rethrow($result);
            }
        };
    }

    /**
     * Returns a new function that wraps $worker in a promise/coroutine-aware function that automatically upgrades
     * Generators to coroutines. The returned function always returns a promise when invoked. If $worker throws,
     * a failed promise is returned.
     *
     * @param callable (mixed ...$args): mixed $worker
     *
     * @return callable(mixed ...$args): \Amp\Promise
     */
    function coroutine(callable $worker): callable {
        return function (...$args) use ($worker): Promise {
            try {
                $result = $worker(...$args);
            } catch (\Throwable $exception) {
                return new Failure($exception);
            }

            if ($result instanceof \Generator) {
                return new Coroutine($result);
            }

            if ($result instanceof Promise) {
                return $result;
            }

            if ($result instanceof ReactPromise) {
                return Promise\adapt($result);
            }

            return new Success($result);
        };
    }

    /**
     * Calls the given function, always returning a promise. If the function returns a Generator, it will be run as a
     * coroutine. If the function throws, a failed promise will be returned.
     *
     * @param callable (mixed ...$args): mixed $functor
     * @param array ...$args Arguments to pass to the function.
     *
     * @return \Amp\Promise
     */
    function call(callable $functor, ...$args): Promise {
        try {
            $result = $functor(...$args);
        } catch (\Throwable $exception) {
            return new Failure($exception);<commit_msg>Document wrap and coroutine better by explicitly naming the differences<commit_after><?php

namespace Amp {
    use React\Promise\PromiseInterface as ReactPromise;

    /**
     * Wraps the callback in a promise/coroutine-aware function that automatically upgrades Generators to coroutines and
     * calls `rethrow()` on the returned promises (or the coroutine created).
     *
     * Use this function to create a coroutine-aware callable for a non-promise-aware callback caller. Errors are
     * automatically handled by `rethrow()`.
     *
     * @param callable(...$args): \Generator|\Amp\Promise|mixed $callback
     *
     * @return callable(...$args): void
     */
    function wrap(callable $callback): callable {
        return function (...$args) use ($callback) {
            Promise\rethrow(call($callback, ...$args));
        };
    }

    /**
     * Returns a new function that wraps $worker in a promise/coroutine-aware function that automatically upgrades
     * Generators to coroutines. The returned function always returns a promise when invoked. If $callback throws,
     * a failed promise is returned.
     *
     * Use this function to create a coroutine-aware callable for a promise-aware callback caller. Errors have to be
     * handled by the caller.
     *
     * @param callable(mixed ...$args): mixed $callback
     *
     * @return callable(mixed ...$args): \Amp\Promise
     */
    function coroutine(callable $callback): callable {
        return function (...$args) use ($callback): Promise {
            return call($callback, ...$args);
        };
    }

    /**
     * Calls the given function, always returning a promise. If the function returns a Generator, it will be run as a
     * coroutine. If the function throws, a failed promise will be returned.
     *
     * @param callable(mixed ...$args): mixed $callback
     * @param array ...$args Arguments to pass to the function.
     *
     * @return \Amp\Promise
     */
    function call(callable $callback, ...$args): Promise {
        try {
            $result = $callback(...$args);
        } catch (\Throwable $exception) {
            return new Failure($exception);
{'fraction_non_alphanumeric': 0.11008781323623902, 'fraction_numerical': 0.0, 'mean_word_length': 2.1575388776200137, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4378968', 'n_tokens_mistral': 1221, 'n_tokens_neox': 1198, 'n_words': 426}