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{ toArray } from './toarray';\nimport { ReduceOptions } from './reduceoptions';\nimport { throwIfAborted } from '../aborterror';\n\n/**\n * Applies an accumulator function over an async-iterable sequence from the end, returning the result of the aggregation as a\n * single element in the result sequence. The seed value, if specified, is used as the initial accumulator value.\n * For aggregation behavior with incremental intermediate results, scan.\n *\n * @export\n * @template T The type of the elements in the source sequence.\n * @template R The type of the result of the aggregation.\n * @param {AsyncIterable<T>} source An async-iterable sequence to aggregate over from the right.\n * @param {ReduceOptions<T, R>} options The options which contains a callback, with optional seed and an optional abort signal for cancellation.\n * @returns {Promise<R>} A promise with the final accumulator value.\n */\nexport async function reduceRight<T, R = T>(\n source: AsyncIterable<T>,\n options: ReduceOptions<T, R>\n): Promise<R> {\n const { ['seed']: seed, ['signal']: signal, ['callback']: callback } = options;\n const hasSeed = options.hasOwnProperty('seed');\n throwIfAborted(signal);\n const array = await toArray(source, signal);\n let hasValue = false;\n let acc = seed as T | R;\n for (let offset = array.length - 1; offset >= 0; offset--) {\n const item = array[offset];\n if (hasValue || (hasValue = hasSeed)) {\n acc = await callback(<R>acc, item, offset, signal);\n } else {\n acc = item;\n hasValue = true;\n }\n }\n\n if (!(hasSeed || hasValue)) {\n throw new Error('Sequence contains no elements');\n }\n\n return acc as R;\n}\n"]}
\No newline at end of file