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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 126x 509x 509x 509x 509x 509x 509x 509x 509x 509x 126x 383x 383x 383x 383x 383x 509x 509x 509x 509x 509x 1x 1x 4x 1x 1x 388x 1x 1x 7x 7x 33x 4x 29x 1x 392x 392x 392x 392x 647x 392x 1x 388x 388x 388x 846x 388x | import {apply, constFalse, constTrue} from '#Function'
import {orderToEqual} from '#Order'
import {
leaf,
tree,
treeCata,
treeCataEffect,
type Tree,
type TreeEffectFolder,
type TreeFolder,
} from '#tree'
import * as TreeF from '#treeF'
import {
Array,
Effect,
Equivalence,
Option,
Order,
Predicate,
identity,
pipe,
} from 'effect'
import {minimumLeafParentFold} from './order.js'
/**
* True if `needle` is found in the tree.
*
* Will short-circuit and return immediately if the needle is found.
* @example
* import {includes, from, of} from 'effect-tree'
* import {Number} from 'effect'
*
* const tree = from(1, of(2), from(4, of(5)))
*
* const hasNumber = includes(Number.Equivalence)
*
* expect(hasNumber(3)(tree), 'not found').toBe(false)
* expect(hasNumber(5)(tree), 'found').toBe(true)
* @category ops
* @function
*/
//:
export const includes =
<A>(
equals: Equivalence.Equivalence<A>,
): ((a: A) => Predicate.Predicate<Tree<A>>) =>
a =>
tree =>
pipe(
a,
includesFold(equals),
treeCataEffect,
apply(tree),
Effect.match({
onFailure: constTrue,
onSuccess: constFalse,
}),
Effect.runSync,
)
/**
* Filter out the minimal leaf for the given order and return a tuple with:
*
* 1. The given tree with its minimal leaf removed.
* 2. The minimal leaf that was removed.
* 3. The minimal leaf parent, or `None` if the tree is a leaf.
* @category ops
* @function
*/
export const filterMinimumLeaf =
<A>(order: Order.Order<A>) =>
(
t: Tree<A>,
): [filtered: Tree<A>, minLeaf: A, maybeParent: Option.Option<A>] => {
const [minLeaf, maybeParent]: readonly [A, Option.Option<A>] = pipe(
order,
minimumLeafParentFold,
treeCata,
)(t)
return [
(Option.isNone(maybeParent)
? identity
: pipe(
minLeaf,
orderToEqual.curried(order),
Predicate.not,
filterLeaves,
))(t),
minLeaf,
maybeParent,
]
}
/**
* Filter nodes so that only those satisfying the given predicate remain.
* @category ops
* @function
*/
export const filterNodes: <A>(
predicate: Predicate.Predicate<Tree<A>>,
) => (self: Tree<A>) => Tree<A> = predicate =>
pipe(predicate, filterNodesFold, treeCata)
/**
* Filter leaves so that only those satisfying the given predicate remain.
* @example
* import {filterLeaves, drawTree, from, of} from 'effect-tree'
*
* // ┬1
* // ├┬2
* // │├─3
* // │├─4
* // │└─5
* // ├┬6
* // │├─7
* // │├─8
* // │└┬11
* // │ └─9
* // └─10
* const tree = from(
* 1,
* from(2, of(3), of(4), of(5)),
* from(6, of(7), of(8), from(11, of(9))),
* of(10),
* )
*
* const filtered = filterLeaves((n: number) => n % 2 === 0)(tree)
* expect(drawTree.number(filtered)).toEqual([
* '┬1 ',
* '├┬2 ',
* '│└─4',
* '├┬6 ',
* '│└─8',
* '└─10',
* ])
* @category ops
* @function
*/
export const filterLeaves: <A>(
predicate: Predicate.Predicate<A>,
) => (self: Tree<A>) => Tree<A> = predicate =>
pipe(predicate, filterLeavesFold, treeCata)
/**
* True if `needle` is found in a tree level.
*
* Searches the entire tree but will short-circuit if the needle is found.
* @category fold
* @function
*/
//
export const includesFold =
<A>(equals: Equivalence.Equivalence<A>) =>
(needle: A): TreeEffectFolder<A, void, undefined> =>
treeF =>
equals(TreeF.getValue(treeF), needle)
? Effect.fail(void {})
: Effect.succeed({})
/**
* Filter nodes at a tree level.
* @category fold
* @function
*/
export const filterNodesFold = <A>(
predicate: Predicate.Predicate<Tree<A>>,
): TreeFolder<A, Tree<A>> =>
TreeF.match({
onLeaf: leaf,
onBranch: (value, forest) =>
tree(value, pipe(forest, Array.filter(predicate))),
})
/**
* Filter leaves at a level algebra.
* @category fold
* @function
*/
export const filterLeavesFold = <A>(
predicate: Predicate.Predicate<A>,
): TreeFolder<A, Tree<A>> =>
filterNodesFold(
({unfixed}) =>
TreeF.length(unfixed) !== 0 || pipe(unfixed, TreeF.getValue, predicate),
)
|