@jsoldi/hkt
Version:
Higher kinded types for typescript and a few utility monads.
65 lines • 4.22 kB
TypeScript
import { KRoot } from "../core/hkt.js";
import { IFold } from "../classes/fold.js";
import { IMonadPlus } from "../classes/monadPlus.js";
import { IUnfold } from "../classes/unfold.js";
import { Maybe } from "./maybe.js";
import { ITask, KTask, TaskLike } from "./task.js";
/** Alias for JavaScript's `AsyncGenerator` */
export type AsyncGen<T> = AsyncGenerator<T, void, void>;
/** Alias for JavaScript's `Generator` */
export type SyncGen<T> = Generator<T, void, void>;
/** Alias for a function that returns an `AsyncGenerator` */
export type Async<T> = () => AsyncGen<T>;
/** Alias for a function that returns a `Generator` */
export type Sync<T> = () => SyncGen<T>;
/** A value that can be awaited */
export type Awaitable<T> = T | Promise<T>;
/** A type that can be converted to an `Async` */
export type AsyncLike<T, A extends any[] = []> = T[] | Promise<T[]> | ((...a: A) => SyncGen<T>) | ((...a: A) => AsyncGen<T>) | ((...a: A) => AsyncLike<T>) | never;
/** The higher-kinded type of `Async` — a function that returns an `AsyncGenerator` */
export interface KAsync extends KRoot {
readonly 0: unknown;
readonly body: Async<this[0]>;
}
/** Provides a set of functions to work with `Async` instances — functions that return an `AsyncGenerator` */
export interface IAsync extends IMonadPlus<KAsync>, IFold<KAsync, KTask>, IUnfold<KAsync, KTask> {
/** The task monad, used as the underlying monad for folds and unfolds */
readonly scalar: ITask;
/** Creates a function that returns an `Async` from a function that returns a `Generator` */
fun<T, A extends any[]>(asyncLike: (...a: A) => SyncGen<T>): (...args: A) => Async<T>;
/** Creates a function that returns an `Async` from a function that returns an `AsyncGenerator` */
fun<T, A extends any[]>(asyncLike: (...a: A) => AsyncGen<T>): (...args: A) => Async<T>;
/** Creates a function that returns an `Async` from a function that returns an `AsyncLike` */
fun<T, A extends any[]>(asyncLike: (...a: A) => AsyncLike<T>): (...args: A) => Async<T>;
/** Creates a function that returns an `Async` from an `AsyncLike` */
fun<T, A extends any[]>(asyncLike: AsyncLike<T, A>): (...args: A) => Async<T>;
/** Creates an `Async` from an `AsyncLike` and optionally a set of arguments */
from<T, A extends any[]>(asyncLike: AsyncLike<T, A>, ...args: A): Async<T>;
/** Monad's `bind` overload for `Async`, allowing for a mapping function that returns an `AsyncLike` */
bind<A, B>(fa: Async<A>, f: AsyncLike<B, [A]>): Async<B>;
/** Monad's `flatMap` overload for `Async`, allowing for a mapping function that returns an `AsyncLike` */
flatMap<A, B>(f: AsyncLike<B, [A]>): (fa: Async<A>) => Async<B>;
/** Async specific `unfold`, allowing the given function to return a `TaskLike` */
unfold<A, B>(alg: (b: B) => TaskLike<Maybe<[A, B]>, [B]>): (b: B) => Async<A>;
/** Takes the first `n` elements of the `Async` */
take(n: number): <T>(fa: Async<T>) => Async<T>;
/** Filters the items of an Async using a predicate function. */
filter<T, S extends T>(pred: (a: T) => a is S): (fa: Async<T>) => Async<S>;
/** Filters the items of an Async using a predicate function. */
filter<T>(pred: (a: T) => unknown): (fa: Async<T>) => Async<T>;
/** Takes items from the Async while the predicate is true. */
takeWhile<T>(pred: (a: T) => unknown): (fa: Async<T>) => Async<T>;
/** Skips items from the Async while the predicate is true. */
skipWhile<T>(pred: (a: T) => unknown): (fa: Async<T>) => Async<T>;
/** Removes duplicate items from an Async using a key function. */
distinctBy<T, K>(key: (a: T) => K): (fa: Async<T>) => Async<T>;
/** Removes duplicate items from an Async. */
distinct<T>(fa: Async<T>): Async<T>;
/** Split the Async into chunks of the given size. */
chunks<T>(size: number): (fa: Async<T>) => Async<T[]>;
/** Zips two Asyncs into an Async of pairs. */
zip<A, B>(fa: Async<A>, fb: Async<B>): Async<[A, B]>;
}
/** The `Async` module, providing a set of functions to work with `Async` instances, which are functions that return an `AsyncGenerator` */
export declare const async: IAsync;
//# sourceMappingURL=async.d.ts.map