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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | 1x 1x 1x 1x 1x 1x 1x 1x 12x 12x 12x 12x 184x 12x 1x 271x 271x 271x 271x 271x 271x 271x 60x 60x 60x 60x 271x 271x 271x 1x 31x 31x 31x 31x 31x 31x 11x 11x 11x 11x 11x 11x 11x 11x 11x 31x 31x 1x 1x 14x 14x 14x 14x 14x 14x 14x 12x 12x 12x 14x 14x 14x 14x 14x 4x 14x 14x 14x 1x 184x 142x 42x 42x 42x 42x 42x 30x 30x 30x 30x 42x 42x 1x 5x 1x 1x 11x 11x 1x 3x 1x 2x 1x 1x 102x 102x 1x 1x 2x 12x 1x 2x 1x 7x 7x 7x 7x 1x 1x 6x 1x | import {
byParentUnfold,
fixTree,
getValue,
map,
match,
treeAna,
treeCata,
type Tree,
type TreeFold,
type TreeFolder,
type TreeUnfold,
type TreeUnfolder,
} from '#tree'
import * as TreeF from '#treeF'
import {transpose, type NonEmptyArray, type NonEmptyArray2} from '#Array'
import {K, type EndoOf} from '#Function'
import {pair} from '#Pair'
import {fromNumber} from '#String'
import {Array, flow, pipe} from 'effect'
/**
* Settings for the `unfolds.levelTree` unfold.
* @category ops
* @function
*/
export interface LevelTreeSettings {
/** Requested depth of unfolded tree. */
depth: number
/**
* Function to be run at each node to determine its child count, also called
* _degree_. The function will be called with the node _depth_.
*/
degree?: (depth: number) => number
}
/**
* Unfold a single layer of a numeric level tree.
* @category unfold
* @function
*/
export const levelTreeUnfold = ({
depth,
degree = () => 1,
}: LevelTreeSettings): TreeUnfolder<number, number> =>
byParentUnfold((n: number): number[] =>
n >= depth ? [] : Array.replicate(n + 1, degree(n)),
)
/**
* Annotate nodes at a tree level with their depth.
* @category unfold
* @function
*/
export const annotateDepthUnfold = <A>([tree, previousDepth]: [
Tree<A>,
number,
]): TreeF.TreeF<readonly [A, number], [Tree<A>, number]> => {
const depth = previousDepth + 1
return pipe(
tree,
match({
onLeaf: flow(pair.withSecond(depth), TreeF.leafF),
onBranch: (value, nodes) =>
TreeF.branchF(
[value, depth] as const,
pipe(nodes, Array.map(pair.withSecond(depth))),
),
}),
)
}
/**
* Group a level of the tree by _depth_: number of levels between the node and
* the tree root.
* @category fold
* @function
*/
export const levelsFold = <A>(
self: TreeF.TreeF<A, NonEmptyArray2<A>>,
): NonEmptyArray2<A> =>
pipe(
self,
TreeF.match({
onLeaf: value => [[value]],
onBranch: (value, forest): NonEmptyArray2<A> => {
return [
[value],
...(pipe(
forest as NonEmptyArray<NonEmptyArray2<A>>,
transpose,
Array.map(flow(Array.getSomes, Array.flatten)),
) as NonEmptyArray2<A>),
]
},
}),
)
/**
* Label a string tree with a level index. A level index is a string that looks like:
* `1.3.11.2`. The last number is the index of the node in its parent, the number
* before that is the index of the parent in its parent, and so on, so that they
* form the unique path between the node and the tree root.
*
* {@link drill} will _get_ a child from a tree at a path.
* @category unfold
* @function
*/
export const annotateLevelLabelsUnfold: TreeUnfolder<
string,
[string, Tree<string>]
> = ([prefix, self]: [string, Tree<string>]): TreeF.TreeF<
string,
[string, Tree<string>]
> => {
const prefixValue: EndoOf<string> = flow(
pair.withFirst(prefix),
Array.join(' '),
),
prefixLeaf = flow(prefixValue, TreeF.leafF),
prefixBranch = (node: Tree<string>, i: number): [string, Tree<string>] => [
prefix + (i + 1).toString() + '.',
node,
]
return pipe(
self,
match({
onLeaf: prefixLeaf,
onBranch: (value, forest) =>
TreeF.branchF(prefixValue(value), Array.map(forest, prefixBranch)),
}),
)
}
/**
* Crop all nodes from a tree that are below the given depth, for a single level
* of the tree.
* @category unfold
* @function
*/
export const cropDepthUnfold = <A>([depth, self]: readonly [
number,
Tree<A>,
]): TreeF.TreeF<A, readonly [number, Tree<A>]> =>
depth === 1
? pipe(self, getValue, TreeF.leafF)
: pipe(
self,
match<A, TreeF.TreeF<A, [number, Tree<A>]>>({
onLeaf: TreeF.leafF,
onBranch: (value, forest) =>
TreeF.branchF(
value,
pipe(forest, Array.map(pair.withFirst(depth - 1))),
),
}),
)
/**
* Returns tree nodes grouped by level.
* @category ops
* @function
*/
export const levels: <A>(self: Tree<A>) => NonEmptyArray2<A> = self =>
pipe(self, treeCata(levelsFold))
/**
* Unfold a perfectly balanced tree from the given settings.
*
* The numeric argument for the `TreeUnfold<number,number>` returned, is the
* _lower_ limit for the depth and you would usually want the default `1`.
*
* When the limit is `1` and the depth is `7`, then `7` levels will be build. If
* the depth is `7` but the limit is `3`, only `4` levels will be built, and the
* root node value will be `4`.
*
*
* In a _level tree_, the value of every node is its depth. This, for example,
* is a level tree:
*
* ```txt
* ┬1
* ├─2
* ├─2
* ├┬2
* │├─3
* │└─3
* └─2
* ```
* @example
* import {unfoldLevelTree, Draw} from 'effect-tree'
*
* const tree = unfoldLevelTree({depth: 4, degree: depth => 3 - depth})()
*
* expect(Draw.drawTree.number(tree)).toEqual([
* '┬1 ',
* '├┬2 ',
* '│└┬3 ',
* '│ └─4',
* '└┬2 ',
* ' └┬3 ',
* ' └─4',
* ])
* @category ops
* @function
*/
export const unfoldLevelTree =
(settings: LevelTreeSettings) =>
(lowerLimit = 1): Tree<number> =>
pipe(settings, levelTreeUnfold, treeAna)(lowerLimit)
/**
* Returns tree nodes paired with their hop count from root.
* @category ops
* @function
*/
export const annotateDepth = <A>(self: Tree<A>): Tree<readonly [A, number]> =>
treeAna(annotateDepthUnfold<A>)([self, 0])
/**
* Annotate a string tree with label that indicate the node depth and index
* in their parent node.
* @category ops
* @function
*/
export const addLevelLabels: (self: Tree<string>) => Tree<string> = tree =>
treeAna(annotateLevelLabelsUnfold)(['1.', tree])
/**
* Crop all nodes from a tree that are deeper than the given depth. For example:
*
* ```ts
* import {leaf, branch, cropDepth} from 'effect-tree'
*
* // ┬1 A tree of depth=4
* // └┬2 with 3 branches and
* // └┬3 a single leaf.
* // └─4
* //
* const depth4: Tree<string> = branch(1, [branch(2, [branch(3, [leaf(4)])])]])
*
* const depth2: Tree<string> = pipe(depth4, cropDepth(2))
* //
* // ┬1 Has been cropped to a tree of depth=2
* // └─2 depth2 = branch(1, [leaf(2)])
* ```
* @category ops
* @function
*/
export const cropDepth =
(depth: number) =>
<A>(self: Tree<A>): Tree<A> =>
pipe(self, pair.withFirst(depth), treeAna(cropDepthUnfold<A>))
/**
* Grow all leaves according to the given `grow` function for a single level of
* the tree.
* @category fold
* @function
*/
export const growLeavesFold =
<A>(grow: TreeUnfold<A, A>): TreeFolder<A, Tree<A>> =>
self =>
TreeF.isBranch(self) ? fixTree(self) : grow(TreeF.getValue(self))
/**
* Grows the tree at its leaves.
*
* Given a function of type `(a: A) ⇒ Tree<A>` replacing a value of type `A`
* with a `Tree<A>`, grow the tree by running all leaves through this function,
* replacing the leaves with the function results.
* @category ops
* @function
*/
export const growLeaves = <A>(grow: TreeUnfold<A, A>): TreeFold<A, Tree<A>> =>
pipe(grow, growLeavesFold, treeCata)
/**
* Create an N-ary level tree with the given degree at the given depth. In a
* _level tree_, the value of each node is set to its depth. An N-ary tree is
* one where all nodes, except the leaves, have the same child count.
*
* At the key `string` you will find a version that returns the tree where nodes
* have been formatted as strings instead of number.
* @example
* import {nAryTree, drawTree} from 'effect-tree'
*
* const tree = nAryTree.string({depth: 2, degree: 4})
* expect(drawTree(tree)).toEqual([
* '┬1 ',
* '├─2',
* '├─2',
* '├─2',
* '└─2',
* ])
* @param settings The child count for all nodes except leaves, and the tree depth requested.
* @returns An N-ary level tree of the given depth..
* @category ops
* @function
*/
export const nAryTree = ({
degree,
depth,
}: {
degree: number
depth: number
}): Tree<number> => unfoldLevelTree({depth, degree: K(degree)})(1)
nAryTree.string = flow(nAryTree, map(fromNumber))
/**
* Create a binary level tree at the given depth. In a _level tree_, the value
* of each node is set to its depth.
*
* At the key `string` you will find a version that returns the tree where nodes
* have been formatted as strings instead of number.
*
* ```ts
* import {binaryTree, drawTree, type Tree} from 'effect-tree'
*
* const tree: Tree<number> = binaryTree(3)
*
* console.log(drawTree(tree).join('\n'))
*
* // prints:
* // ┬1
* // ├┬2
* // │├─3
* // │└─3
* // └┬2
* // ├─3
* // └─3
* ```
* @param depth Tree depth requested. Tree returned is perfectly balanced. When
* depth is zero returns a leaf.
* @returns A binary level tree of the given depth..
* @category ops
* @function
*/
export const binaryTree = (depth: number): Tree<number> =>
nAryTree({degree: 2, depth})
binaryTree.string = flow(binaryTree, map(fromNumber))
|