UNPKG

@jsoldi/hkt

Version:

Higher kinded types for typescript and a few utility monads.

77 lines 4.53 kB
import { $, $B2, $Q, KRoot } from "../core/hkt.js"; import { IMonad } from "../classes/monad.js"; import { IMonadPlus } from "../classes/monadPlus.js"; import { IMonadTrans, ITransformer } from "../classes/transformer.js"; import { ITraversable } from "../classes/traversable.js"; import { IFold } from "../classes/fold.js"; import { IFoldable } from "../classes/foldable.js"; import { Left, Right } from "./either.js"; import { Lazy } from "./lazy.js"; /** Represents the lack of a value. */ export type Nothing = Left<void>; /** Represents an existing value. */ export type Just<T> = Right<T>; /** Represents a value that may or may not exist. */ export type Maybe<T> = Just<T> | Nothing; /** The higher-kinded type of `Maybe`. */ export interface KMaybe extends KRoot { readonly 0: unknown; readonly body: Maybe<this[0]>; } /** The maybe interface, providing a set of functions for working with `Maybe` values. */ export interface IMaybe extends IMonadPlus<KMaybe>, IFoldable<KMaybe>, ITraversable<KMaybe>, ITransformer<$<$Q, KMaybe>> { /** Creates a `Just` value, representing an existing value. */ just<A>(a: A): Just<A>; /** The `Nothing` value, representing the lack of a value. */ readonly nothing: Nothing; /** Checks if the given value is a `Just`. */ isJust<A>(fa: Maybe<A>): fa is Just<A>; /** Checks if the given value is a `Nothing`. */ isNothing<A>(fa: Maybe<A>): fa is Nothing; /** Applies one of two functions to the value of a `Maybe`. */ either<A, B, C = B>(onLeft: () => B, onRight: (b: A) => C): (fa: Maybe<A>) => B | C; /** Filters a `Maybe` value based on a predicate. */ filter<T, S extends T>(predicate: (item: T) => item is S): (items: Maybe<T>) => Maybe<S>; /** Filters a `Maybe` value based on a predicate. */ filter<T>(predicate: (item: T) => unknown): (items: Maybe<T>) => Maybe<T>; /** Returns the first value in a list, or `Nothing` if the list is empty. */ fromList<A>(fa: A[]): Maybe<A>; /** Converts a `Maybe` to an empty or single-element list. */ toList<A>(fa: Maybe<A>): [] | [A]; /** Converts `null` or `undefined` to `Nothing` or wraps the value in a `Just`. */ fromNullable<A>(a: A): Maybe<NonNullable<A>>; /** Returns `Nothing` if the condition is false, otherwise returns just null. */ if(cond: unknown): Maybe<null>; /** Returns `fa` if it has a value, otherwise returns `b`. */ or<B>(b: B): <A>(fa: Maybe<A>) => Maybe<A> | B; /** Returns `b` if `fa` has a value, otherwise returns `fa`. */ and<B>(b: B): <A>(fa: Maybe<A>) => Maybe<A> | B; /** Returns the value of `fa` if it has a value, otherwise returns the value of `b`. */ else<B>(b: Lazy<B>): <A>(fa: Maybe<A>) => A | B; /** Produces a monad transformer having `Maybe` as the inner monad. */ transform<M>(base: IMonad<M>): IMaybeTrans<M>; /** Lifts the maybe monoid into a monad. */ liftMonoid<M>(base: IMonad<M>): IMaybeTrans<M>; } /** The maybe transformer interface, providing a set of functions for working with `Maybe` values within a monad. */ export interface IMaybeTrans<M> extends IMonadTrans<$<$Q, KMaybe>, M>, IFold<$B2<M, KMaybe>, M>, IMonadPlus<$B2<M, KMaybe>> { /** Applies one of two functions to the value of a `Maybe` within a monad. */ either<A, B, C = B>(onLeft: () => $<M, B>, onRight: (b: A) => $<M, C>): (fa: $<M, Maybe<A>>) => $<M, B | C>; /** Returns the first value in a list, or `Nothing` if the list is empty. */ fromList<A>(fa: $<M, A[]>): $<M, Maybe<A>>; /** Converts a `Maybe` to an empty or single-element list. */ toList<A>(fa: $<M, Maybe<A>>): $<M, [] | [A]>; /** Converts `null` or `undefined` to `Nothing` or wraps the value in a `Just`. */ fromNullable<A>(a: $<M, A>): $<M, Maybe<NonNullable<A>>>; /** Returns `Nothing` if the condition is false, otherwise returns just null. */ if(cond: $<M, unknown>): $<M, Maybe<null>>; /** Returns `fa` if it has a value, otherwise returns `b`. */ or<B>(b: $<M, B>): <A>(fa: $<M, Maybe<A>>) => $<M, Maybe<A> | B>; /** Returns `b` if `fa` has a value, otherwise returns `fa`. */ and<B>(b: $<M, B>): <A>(fa: $<M, Maybe<A>>) => $<M, Maybe<A> | B>; /** Returns the value of `fa` if it has a value, otherwise returns the value of `b`. */ else<B>(b: Lazy<$<M, B>>): <A>(fa: $<M, Maybe<A>>) => $<M, A | B>; } /** The maybe module, providing a set of functions for working with `Maybe` values. */ export declare const maybe: IMaybe; //# sourceMappingURL=maybe.d.ts.map