Tree overview
Module that implements a Tree<A,B> where the value of a non-leaf node is of type A and the value of a leaf node is of type B.
A node may have no leaf.
A Tree may be composed of a single leaf. If this situation does not correspond to your need, you may work with the type NonLeaf<A,B> instead. In all the provided functions, self can be a Tree<A,B> or a NonLeaf<A,B>
Table of contents
Constructors
unfold
Non recursive function that builds a (possibly infinite) tree from a seed value and an unfold function. A cycle is reported when a seed has a value equal to itself (in terms of Equal.equals) as a direct parent. In that case, function f
receives as cyclicalRef
argument a some
of the value of the non-leaf that was created from that seed.
Signature
export declare const unfold: <S, A, B>(
f: (seed: S, cyclicalRef: Option.Option<A>) => Either.Either<MTypes.Pair<A, ReadonlyArray<S>>, B>
) => MTypes.OneArgFunction<S, Type<A, B>>
unfoldAndFold
Non recursive function that builds a (possibly infinite) tree from a seed value and an unfold function, then applies a function f to the value of each node and the result of applying f to the children of the node (see fold below).A cycle is reported when a seed has a value equal to itself (in terms of Equal.equals) as a direct parent. In that case, function f
receives as cyclicalRef
argument a some
of the value of the non-leaf that was created from that seed.
Signature
export declare const unfoldAndFold: <A, B, S = A, C = B>({
unfold,
foldNonLeaf,
foldLeaf
}: {
readonly unfold: (seed: S, cyclicalRef: Option.Option<A>) => Either.Either<MTypes.Pair<A, ReadonlyArray<S>>, B>
readonly foldNonLeaf: (value: A, children: ReadonlyArray<C>) => C
readonly foldLeaf: (value: B) => C
}) => MTypes.OneArgFunction<S, C>
Destructors
value
Returns the value
property of self
Signature
export declare const value: <A, B>(self: Type<A, B>) => A | B
Equivalences
equivalence
Equivalence based on Equal.equals
Signature
export declare const equivalence: Equivalence.Equivalence<Type<unknown, unknown>>
getEquivalence
Returns an equivalence based on an equivalence of the subtypes
Signature
export declare const getEquivalence: <A, B>(
aEquivalence: Equivalence.Equivalence<A>,
bEquivalence: Equivalence.Equivalence<B>
) => Equivalence.Equivalence<Type<A, B>>
Guards
has
Type guard
Signature
export declare const has: (u: unknown) => u is Type<unknown, unknown>
isLeaf
Type guard
Signature
export declare const isLeaf: <A, B>(u: Type<A, B>) => u is Leaf.Type<B>
Models
Forest (namespace)
Namespace of a Forest
Type (type alias)
Type of a Forest
Signature
export type Type<A, B> = ReadonlyArray<_Type<A, B>>
Leaf (namespace)
Namespace of a Leaf
Type (interface)
Typeof a Leaf node
Signature
export interface Type<out B> extends Equal.Equal, Inspectable.Inspectable, Pipeable.Pipeable {
/** Identifier of a Leaf */
readonly _tag: "Leaf"
/** Value of a Leaf node */
readonly value: B
/** @internal */
readonly [_TypeId]: {
readonly _A: Types.Covariant<never>
readonly _B: Types.Covariant<B>
}
}
NonLeaf (namespace)
Namespace of a NonLeaf
Type (interface)
Typeof a NonLeaf
Signature
export interface Type<out A, out B> extends Equal.Equal, Inspectable.Inspectable, Pipeable.Pipeable {
/** Identifier of a NonLeaf */
readonly _tag: "NonLeaf"
/** The value of a NonLeaf */
readonly value: A
/** The children of a NonLeaf */
readonly forest: Forest.Type<A, B>
/** @internal */
readonly [_TypeId]: {
readonly _A: Types.Covariant<A>
readonly _B: Types.Covariant<B>
}
}
Type (type alias)
Type of a Tree
Signature
export type Type<A, B> = Leaf.Type<B> | NonLeaf.Type<A, B>
moduleTag
Module tag
Signature
export declare const moduleTag: "@parischap/effect-lib/Tree/"
Utils
extendDown
Returns a new tree in which the value of each node is replaced by the result of a function that takes the node as parameter in top-down order. More powerful than map which takes only the value of the node as parameter
Signature
export declare const extendDown: <A, B, C, D>({
fNonLeaf,
fLeaf
}: {
readonly fNonLeaf: (tree: Type<NoInfer<A>, NoInfer<B>>, level: number) => C
readonly fLeaf: (leaf: Leaf.Type<NoInfer<B>>, level: number) => D
}) => MTypes.OneArgFunction<Type<A, B>, Type<C, D>>
extendUp
Returns a new tree in which the value of each node is replaced by the result of a function that takes the node as parameter in bottom-up order. More powerful than map which takes only the value of the node as parameter
Signature
export declare const extendUp: <A, B, C, D>({
fNonLeaf,
fLeaf
}: {
readonly fNonLeaf: (tree: Type<NoInfer<A>, NoInfer<B>>, level: number) => C
readonly fLeaf: (leaf: Leaf.Type<NoInfer<B>>, level: number) => D
}) => MTypes.OneArgFunction<Type<A, B>, Type<C, D>>
fold
Folds a tree into a “summary” value in bottom-up order with.
For each Leaf in the tree, applies fLeaf
. For each NonLeaf in the tree, applies fNonLeaf
to the value
property and the result of applying fNonLeaf
or fLeaf
to each node in the forest
property.
This is also known as the catamorphism on trees.
Signature
export declare const fold: <A, B, C>({
fNonLeaf,
fLeaf
}: {
readonly fNonLeaf: (a: NoInfer<A>, bs: ReadonlyArray<C>, level: number) => C
readonly fLeaf: (a: NoInfer<B>, level: number) => C
}) => MTypes.OneArgFunction<Type<A, B>, C>
map
Maps a tree
Signature
export declare const map: <A, B, C, D>({
fNonLeaf,
fLeaf
}: {
readonly fNonLeaf: (a: NoInfer<A>, level: number) => C
readonly fLeaf: (b: NoInfer<B>, level: number) => D
}) => MTypes.OneArgFunction<Type<A, B>, Type<C, D>>
mapAccum
Maps a tree with an accumulator
Signature
export declare const mapAccum: <S, A, B, C, D>({
accum,
fNonLeaf,
fLeaf
}: {
readonly accum: S
readonly fNonLeaf: (s: S, a: NoInfer<A>, level: number) => MTypes.Pair<S, C>
readonly fLeaf: (s: S, b: NoInfer<B>, level: number) => D
}) => MTypes.OneArgFunction<Type<A, B>, Type<C, D>>
reduce
Top-down reduction - Children are processed from left to right
Signature
export declare const reduce: <A, B, Z>({
z,
fNonLeaf,
fLeaf
}: {
readonly z: Z
readonly fNonLeaf: (z: Z, a: NoInfer<A>, level: number) => Z
readonly fLeaf: (z: Z, b: NoInfer<B>, level: number) => Z
}) => MTypes.OneArgFunction<Type<A, B>, Z>
reduceRight
Top-down reduction - Children are processed from right to left
Signature
export declare const reduceRight: <A, B, Z>({
z,
fNonLeaf,
fLeaf
}: {
readonly z: Z
readonly fNonLeaf: (z: Z, a: NoInfer<A>, level: number) => Z
readonly fLeaf: (z: Z, b: NoInfer<B>, level: number) => Z
}) => MTypes.OneArgFunction<Type<A, B>, Z>