import type { Curried } from '../../compositions/curry.js'; import { type Purried, purry } from '../../compositions/purry.js'; import { _isIterable } from '../../controls/_guards.js'; import type { Series, SyncSeries } from '../../controls/types.js'; function* _syncMap(input: SyncSeries, mapper: (value: I) => O): Generator { for (const value of input) { yield mapper(value); } } async function* _asyncMap( input: Series, mapper: (value: Awaited) => O | Promise, ): AsyncGenerator> { const awaited = await input; if (_isIterable(awaited)) { for (const value of awaited) { yield mapper(await value); } } else { for await (const value of awaited) { yield mapper(value); } } } export function mapSync( ...args: Parameters> ): ReturnType>; export function mapSync( ...args: Parameters>> ): ReturnType>>; export function mapSync( ...args: Parameters>> ): ReturnType>> { return purry(_syncMap)(...args); } export function mapAsync( ...args: Parameters> ): ReturnType>; export function mapAsync( ...args: Parameters>> ): ReturnType>>; export function mapAsync( ...args: Parameters>> ): ReturnType>> { return purry(_asyncMap)(...args); } /** Maps each element by the specified mapper. */ export namespace map { export const sync = mapSync; export const async = mapAsync; }