All files / src/ops traverse.ts

100% Statements 24/24
100% Branches 10/10
100% Functions 10/10
100% Lines 24/24

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  1x 1x 1x 1x                               1x 3x                     1x                                                     1x 3x                                                       1x 3x             1x 1x 1x 62x 1x             1x 1x 1x 1x             1x 1x 1x 1x  
import type {Tree} from '#tree'
import {treeCata, type NonEmptyArrayTypeLambda, type TreeFolderK} from '#tree'
import * as TreeF from '#treeF'
import {Array, pipe} from 'effect'
import {levels} from './levels.js'
 
/**
 * @category ops
 */
export type GetNodes = <A>(self: Tree<A>) => Array.NonEmptyArray<A>
 
/**
 * Return tree nodes in breadth-first order.
 * @example
 * import {breadthOrderValues, binaryTree} from 'effect-tree'
 *
 * expect(breadthOrderValues(binaryTree(3))).toEqual([1, 2, 2, 3, 3, 3, 3])
 * @category ops
 * @function
 */
export const breadthOrderValues: GetNodes = self =>
  pipe(self, levels, Array.flatten)
 
/**
 * Get all tree leaves.
 * @example
 * import {allLeaves, binaryTree} from 'effect-tree'
 *
 * expect(allLeaves(binaryTree(3))).toEqual([3, 3, 3, 3])
 * @category ops
 * @function
 */
export const allLeaves: GetNodes = tree => pipe(tree, treeCata(allLeavesFold))
 
/**
 * Return all tree values in depth-first pre-order. Parents appear _before_
 * their children. For example:
 *
 * ```txt
 * // ┬1
 * // ├┬2
 * // │└┬3
 * // │ └─4
 * // └─5
 * const myTree = branch(
 *   1, [
 *     branch(2, [
 *       branch(3, [
 *         leaf(4),
 *       ]),
 *     ]),
 *     leaf(5),
 *   ]
 * )
 * const myValues = preOrderValues(myTree) // [1, 2, 3, 4, 5]
 * ```
 * @category ops
 * @function
 */
export const preOrderValues: GetNodes = tree =>
  pipe(tree, treeCata(preOrderFold))
 
/**
 * Return all tree values in depth-first post-order. Parents appear _after_
 * their children. For example:
 *
 * ```txt
 * // ┬1
 * // ├┬2
 * // │└┬3
 * // │ └─4
 * // └─5
 * const myTree = branch(
 *   1, [
 *     branch(2, [
 *       branch(3, [
 *         leaf(4),
 *       ]),
 *     ]),
 *     leaf(5),
 *   ]
 * )
 *
 * const myValues = postOrderValues(myTree) // [4, 3, 2, 5, 1]
 * ```
 * @category ops
 * @function
 */
export const postOrderValues: GetNodes = tree =>
  pipe(tree, treeCata(postOrderFold))
 
/**
 * Collect nodes in depth-first pre-order for a single tree level.
 * @category fold
 * @function
 */
export const preOrderFold: TreeFolderK<NonEmptyArrayTypeLambda> = TreeF.match({
  onLeaf: value => [value],
  onBranch: (value, forest) =>
    pipe(forest, Array.flatten, Array.prepend(value)),
})
 
/**
 * Collect nodes in depth-first post-order for a single tree level.
 * @category fold
 * @function
 */
export const postOrderFold: TreeFolderK<NonEmptyArrayTypeLambda> = TreeF.match({
  onLeaf: value => [value],
  onBranch: (value, forest) => pipe(forest, Array.flatten, Array.append(value)),
})
 
/**
 * Collect all _leaves_ from a single tree level.
 * @category fold
 * @function
 */
export const allLeavesFold: TreeFolderK<NonEmptyArrayTypeLambda> = TreeF.match({
  onLeaf: value => [value],
  onBranch: (_, forest) => Array.flatten(forest),
})