/** * Multi-way trees (aka rose trees) and forests, where a forest is * * ```ts * type Forest = Array> * ``` * * @since 2.0.0 */ import { Applicative1 } from './Applicative.js'; import { Apply1 } from './Apply.js'; import { Chain1 } from './Chain.js'; import { Comonad1 } from './Comonad.js'; import { Eq } from './Eq.js'; import { Foldable1 } from './Foldable.js'; import { Functor1 } from './Functor.js'; import { HKT, Kind, Kind2, Kind3, Kind4, URIS, URIS2, URIS3, URIS4 } from './HKT.js'; import { Monad as MonadHKT, Monad1, Monad2, Monad2C, Monad3, Monad3C, Monad4 } from './Monad.js'; import { Monoid } from './Monoid.js'; import { Pointed1 } from './Pointed.js'; import { Predicate } from './Predicate.js'; import { Show } from './Show.js'; import { PipeableTraverse1, Traversable1 } from './Traversable.js'; /** * @category model * @since 2.0.0 */ export type Forest = Array>; /** * @category model * @since 2.0.0 */ export interface Tree { readonly value: A; readonly forest: Forest; } /** * @category constructors * @since 2.0.0 */ export declare function make(value: A, forest?: Forest): Tree; /** * @category instances * @since 2.0.0 */ export declare function getShow(S: Show): Show>; /** * @category instances * @since 2.0.0 */ export declare function getEq(E: Eq): Eq>; /** * Neat 2-dimensional drawing of a forest * * @since 2.0.0 */ export declare function drawForest(forest: Forest): string; /** * Neat 2-dimensional drawing of a tree * * @example * import { make, drawTree } from 'fp-ts/Tree' * * const fa = make('a', [ * make('b'), * make('c'), * make('d', [make('e'), make('f')]) * ]) * * assert.strictEqual(drawTree(fa), `a * ├─ b * ├─ c * └─ d * ├─ e * └─ f`) * * * @since 2.0.0 */ export declare function drawTree(tree: Tree): string; /** * Build a (possibly infinite) tree from a seed value in breadth-first order. * * @category constructors * @since 2.0.0 */ export declare function unfoldTree(b: B, f: (b: B) => [A, Array]): Tree; /** * Build a (possibly infinite) forest from a list of seed values in breadth-first order. * * @category constructors * @since 2.0.0 */ export declare function unfoldForest(bs: Array, f: (b: B) => [A, Array]): Forest; /** * Monadic tree builder, in depth-first order * * @category constructors * @since 2.0.0 */ export declare function unfoldTreeM(M: Monad4): (b: B, f: (b: B) => Kind4]>) => Kind4>; export declare function unfoldTreeM(M: Monad3): (b: B, f: (b: B) => Kind3]>) => Kind3>; export declare function unfoldTreeM(M: Monad3C): (b: B, f: (b: B) => Kind3]>) => Kind3>; export declare function unfoldTreeM(M: Monad2): (b: B, f: (b: B) => Kind2]>) => Kind2>; export declare function unfoldTreeM(M: Monad2C): (b: B, f: (b: B) => Kind2]>) => Kind2>; export declare function unfoldTreeM(M: Monad1): (b: B, f: (b: B) => Kind]>) => Kind>; export declare function unfoldTreeM(M: MonadHKT): (b: B, f: (b: B) => HKT]>) => HKT>; /** * Monadic forest builder, in depth-first order * * @category constructors * @since 2.0.0 */ export declare function unfoldForestM(M: Monad4): (bs: Array, f: (b: B) => Kind4]>) => Kind4>; export declare function unfoldForestM(M: Monad3): (bs: Array, f: (b: B) => Kind3]>) => Kind3>; export declare function unfoldForestM(M: Monad3C): (bs: Array, f: (b: B) => Kind3]>) => Kind3>; export declare function unfoldForestM(M: Monad2): (bs: Array, f: (b: B) => Kind2]>) => Kind2>; export declare function unfoldForestM(M: Monad2C): (bs: Array, f: (b: B) => Kind2]>) => Kind2>; export declare function unfoldForestM(M: Monad1): (bs: Array, f: (b: B) => Kind]>) => Kind>; export declare function unfoldForestM(M: MonadHKT): (bs: Array, f: (b: B) => HKT]>) => HKT>; /** * Fold a tree into a "summary" value in depth-first order. * * For each node in the tree, apply `f` to the `value` and the result of applying `f` to each `forest`. * * This is also known as the catamorphism on trees. * * @example * import { fold, make } from 'fp-ts/Tree' * import { concatAll } from 'fp-ts/Monoid' * import { MonoidSum } from 'fp-ts/number' * * const t = make(1, [make(2), make(3)]) * * const sum = concatAll(MonoidSum) * * // Sum the values in a tree: * assert.deepStrictEqual(fold((a: number, bs: Array) => a + sum(bs))(t), 6) * * // Find the maximum value in the tree: * assert.deepStrictEqual(fold((a: number, bs: Array) => bs.reduce((b, acc) => Math.max(b, acc), a))(t), 3) * * // Count the number of leaves in the tree: * assert.deepStrictEqual(fold((_: number, bs: Array) => (bs.length === 0 ? 1 : sum(bs)))(t), 2) * * @category folding * @since 2.6.0 */ export declare function fold(f: (a: A, bs: Array) => B): (tree: Tree) => B; /** * @since 2.0.0 */ export declare const ap: (fa: Tree) => (fab: Tree<(a: A) => B>) => Tree; /** * @category sequencing * @since 2.14.0 */ export declare const flatMap: { (f: (a: A) => Tree): (ma: Tree) => Tree; (ma: Tree, f: (a: A) => Tree): Tree; }; /** * @since 2.0.0 */ export declare const extend: (f: (wa: Tree) => B) => (wa: Tree) => Tree; /** * @since 2.0.0 */ export declare const duplicate: (wa: Tree) => Tree>; /** * @category sequencing * @since 2.0.0 */ export declare const flatten: (mma: Tree>) => Tree; /** * `map` can be used to turn functions `(a: A) => B` into functions `(fa: F) => F` whose argument and return types * use the type constructor `F` to represent some computational context. * * @category mapping * @since 2.0.0 */ export declare const map: (f: (a: A) => B) => (fa: Tree) => Tree; /** * @category folding * @since 2.0.0 */ export declare const reduce: (b: B, f: (b: B, a: A) => B) => (fa: Tree) => B; /** * @category folding * @since 2.0.0 */ export declare const foldMap: (M: Monoid) => (f: (a: A) => M) => (fa: Tree) => M; /** * @category folding * @since 2.0.0 */ export declare const reduceRight: (b: B, f: (a: A, b: B) => B) => (fa: Tree) => B; /** * @category Extract * @since 2.6.2 */ export declare const extract: (wa: Tree) => A; /** * @category traversing * @since 2.6.3 */ export declare const traverse: PipeableTraverse1; /** * @category traversing * @since 2.6.3 */ export declare const sequence: Traversable1['sequence']; /** * @category constructors * @since 2.7.0 */ export declare const of: (a: A) => Tree; /** * @category type lambdas * @since 2.0.0 */ export declare const URI = "Tree"; /** * @category type lambdas * @since 2.0.0 */ export type URI = typeof URI; declare module './HKT.js' { interface URItoKind { readonly [URI]: Tree; } } /** * @category instances * @since 2.7.0 */ export declare const Functor: Functor1; /** * @category mapping * @since 2.10.0 */ export declare const flap: (a: A) => (fab: Kind<"Tree", (a: A) => B>) => Kind<"Tree", B>; /** * @category instances * @since 2.10.0 */ export declare const Pointed: Pointed1; /** * @category instances * @since 2.10.0 */ export declare const Apply: Apply1; /** * Combine two effectful actions, keeping only the result of the first. * * @since 2.0.0 */ export declare const apFirst: (second: Tree) => (first: Kind<"Tree", A>) => Kind<"Tree", A>; /** * Combine two effectful actions, keeping only the result of the second. * * @since 2.0.0 */ export declare const apSecond: (second: Tree) => (first: Kind<"Tree", A>) => Kind<"Tree", B>; /** * @category instances * @since 2.7.0 */ export declare const Applicative: Applicative1; /** * @category instances * @since 2.10.0 */ export declare const Chain: Chain1; /** * @category instances * @since 2.7.0 */ export declare const Monad: Monad1; /** * Composes computations in sequence, using the return value of one computation to determine the next computation and * keeping only the result of the first. * * @since 2.0.0 */ export declare const chainFirst: (f: (a: A) => Tree) => (first: Tree) => Tree; /** * @category instances * @since 2.7.0 */ export declare const Foldable: Foldable1; /** * @category instances * @since 2.7.0 */ export declare const Traversable: Traversable1; /** * @category instances * @since 2.7.0 */ export declare const Comonad: Comonad1; /** * @category do notation * @since 2.9.0 */ export declare const Do: Tree<{}>; /** * @category do notation * @since 2.8.0 */ export declare const bindTo: (name: N) => (fa: Kind<"Tree", A>) => Kind<"Tree", { readonly [K in N]: A; }>; declare const let_: (name: Exclude, f: (a: A) => B) => (fa: Kind<"Tree", A>) => Kind<"Tree", { readonly [K in keyof A | N]: K extends keyof A ? A[K] : B; }>; export { /** * @category do notation * @since 2.13.0 */ let_ as let }; /** * @category do notation * @since 2.8.0 */ export declare const bind: (name: Exclude, f: (a: A) => Kind<"Tree", B>) => (ma: Kind<"Tree", A>) => Kind<"Tree", { readonly [K in keyof A | N]: K extends keyof A ? A[K] : B; }>; /** * @category do notation * @since 2.8.0 */ export declare const apS: (name: Exclude, fb: Tree) => (fa: Kind<"Tree", A>) => Kind<"Tree", { readonly [K in keyof A | N]: K extends keyof A ? A[K] : B; }>; /** * @since 2.0.0 */ export declare function elem(E: Eq): (a: A, fa: Tree) => boolean; /** * @since 2.11.0 */ export declare const exists: (predicate: Predicate) => (ma: Tree) => boolean; /** * Alias of `flatMap`. * * @category legacy * @since 2.0.0 */ export declare const chain: (f: (a: A) => Tree) => (ma: Tree) => Tree; /** * This instance is deprecated, use small, specific instances instead. * For example if a function needs a `Functor` instance, pass `T.Functor` instead of `T.tree` * (where `T` is from `import T from 'fp-ts/Tree'`) * * @category zone of death * @since 2.0.0 * @deprecated */ export declare const tree: Monad1 & Foldable1 & Traversable1 & Comonad1;