Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | 1x 1x 1x 1x 1x 1x 1x 916x 916x 916x 916x 916x 916x 916x 1x 1x 1x 1x 1x 86x 86x 86x 1x 1x 5245x 5245x 5245x 5245x 26058x 26058x 26058x 26058x 26058x 26058x 26058x 26058x 26058x 5245x 1x 1x 1x 1x 1x 1x 23x 23x 374x 374x 374x 374x 162x 162x 162x 162x 162x 162x 162x 374x 212x 212x 212x 212x 212x 191x 181x 212x 212x 374x 374x 1x 1x 13311x 13311x 13311x 13311x 13311x 13311x 13311x 13311x | import {
Applicative as AP,
Covariant as CO,
FlatMap as FL,
SemiApplicative as SE,
Traversable as TA,
} from '@effect/typeclass'
import {Traversable as ArrayTraversable} from '@effect/typeclass/data/Array'
import {Array, Equivalence, flow, Function, HKT, pipe} from 'effect'
import type {NonEmptyArray} from 'effect/Array'
import {branchF, leafF, match, treeF} from './index.js'
import type {TreeF, TreeFTypeLambda} from './types.js'
/**
* @category fold
* @function
*/
export const map: CO.Covariant<TreeFTypeLambda>['map'] = Function.dual(
2,
<A, C, D>(self: TreeF<A, C>, f: (c: C) => D): TreeF<A, D> =>
pipe(
self,
match({
onLeaf: leafF,
onBranch: (node, forest) => ({node, forest: Array.map(forest, f)}),
}),
),
)
/**
* @category fold
* @function
*/
export const imap = CO.imap<TreeFTypeLambda>(map)
/**
* @category fold
* @function
*/
export const flatMap: FL.FlatMap<TreeFTypeLambda>['flatMap'] = Function.dual(
2,
<_R2, _O2, A, C, _R1, _O1, _E1, D>(
self: TreeF<A, C>,
f: (c: C) => TreeF<A, D>,
): TreeF<A, D> =>
pipe(self, map(f), match({onLeaf: leafF<A>, onBranch: branchF})),
)
/**
* @category fold
* @function
*/
export const traverse: TA.Traversable<TreeFTypeLambda>['traverse'] = <
F extends HKT.TypeLambda,
>(
F: AP.Applicative<F>,
) =>
Function.dual(
2,
<_R, _O, A, C, R, O, E, D>(
self: TreeF<A, C>,
f: (a: C) => HKT.Kind<F, R, O, E, D>,
): HKT.Kind<F, R, O, E, TreeF<A, D>> =>
pipe(
self,
match({
onLeaf: flow(leafF, F.of),
onBranch: (node, forest) => treeFK(F)(node, Array.map(forest, f)),
}),
),
)
/**
* Covariant instance for {@link TreeF}.
* @category fold
* @function
*/
export const Covariant: CO.Covariant<TreeFTypeLambda> = {map, imap}
/**
* FlatMap instance for {@link TreeF}.
* @category fold
* @function
*/
export const FlatMap: FL.FlatMap<TreeFTypeLambda> = {flatMap}
/**
* Traversable instance for {@link TreeF}.
* @category fold
* @function
*/
export const Traversable: TA.Traversable<TreeFTypeLambda> = {traverse}
/**
* Build an equivalence for {@link TreeF} from an equivalence of the tree type
* and an equivalence of the carrier type.
* @category fold
* @function
*/
export const getEquivalence =
<A>(equalsA: Equivalence.Equivalence<A>) =>
<C>(
equalsC: Equivalence.Equivalence<C>,
): Equivalence.Equivalence<TreeF<A, C>> =>
(self, that) =>
pipe(
self,
match({
onLeaf: selfNode =>
pipe(
that,
match({
onLeaf: thatNode => equalsA(selfNode, thatNode),
onBranch: Function.constFalse,
}),
),
onBranch: (selfNode, selfForest) =>
pipe(
that,
match({
onLeaf: Function.constFalse,
onBranch: (thatNode, thatForest) =>
selfNode === thatNode &&
Array.getEquivalence(equalsC)(selfForest, thatForest),
}),
),
}),
)
/**
* @category fold
* @function
*/
const treeFK =
<F extends HKT.TypeLambda>(F: AP.Applicative<F>) =>
<A, C, E = unknown, O = unknown, R = never>(
node: A,
forest: NonEmptyArray<HKT.Kind<F, R, O, E, C>>,
): HKT.Kind<F, R, O, E, TreeF<A, C>> =>
pipe(
node,
F.of,
SE.lift2(F)(treeF<A, C>)(TA.sequence(ArrayTraversable)(F)(forest)),
)
|