All files / src/ops order.ts

100% Statements 73/73
100% Branches 26/26
100% Functions 15/15
100% Lines 73/73

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 1821x 1x 1x 1x                           1x 1x 2x 2x                           1x 1x 2x 2x                           1x 1x 2x 2x                           1x 1x 2x 2x               1x 1x   1x               1x 1x   1x             1x 1x 15x 15x 15x 15x             1x 1x 15x             1x 1x 15x 15x 15x 15x 15x 15x 15x             1x 1x 15x 15x 15x 15x 15x 15x 15x               1x 511x   511x 511x 511x 635x 635x 635x 635x 635x 511x               1x 1x 1x 1x   635x 635x 635x 635x 635x  
import {type Tree, treeCata, type TreeFolder} from '#tree'
import * as TreeF from '#treeF'
import {Array, flow, identity, Option, Order, pipe, Tuple} from 'effect'
import {pair} from '#Pair'
 
/**
 * Return the smallest value in the tree according to the given order.
 * @example
 * import {from, of, minimumNode} from 'effect-tree'
 * import {Number} from 'effect'
 *
 * const tree = from(100, of(10), from(1_000, of(1)))
 *
 * expect(minimumNode(Number.Order)(tree)).toBe(1)
 * @category ops
 * @function
 */
export const minimumNode =
  <A>(Order: Order.Order<A>) =>
  (tree: Tree<A>): A =>
    pipe(tree, treeCata(minimumNodeFold(Order)))
 
/**
 * Return the largest value in the tree according to the given order.
 * @example
 * import {from, of, maximumNode} from 'effect-tree'
 * import {Number} from 'effect'
 *
 * const tree = from(100, of(10), from(1_000, of(1)))
 *
 * expect(maximumNode(Number.Order)(tree)).toBe(1_000)
 * @category ops
 * @function
 */
export const maximumNode =
  <A>(Order: Order.Order<A>) =>
  (tree: Tree<A>): A =>
    pipe(tree, treeCata(maximumNodeFold(Order)))
 
/**
 * Return the smallest leaf in the tree according to the given order.
 * @example
 * import {from, of, minimumLeaf} from 'effect-tree'
 * import {Number} from 'effect'
 *
 * const tree = from(100, of(1_000), from(1, of(10)))
 *
 * expect(minimumLeaf(Number.Order)(tree)).toBe(10)
 * @category ops
 * @function
 */
export const minimumLeaf =
  <A>(Order: Order.Order<A>) =>
  (tree: Tree<A>): A =>
    pipe(tree, treeCata(minimumLeafFold(Order)))
 
/**
 * Return the largest leaf in the tree according to the given order.
 * @example
 * import {from, of, maximumLeaf} from 'effect-tree'
 * import {Number} from 'effect'
 *
 * const tree = from(10, of(1), from(1_000, of(100)))
 *
 * expect(maximumLeaf(Number.Order)(tree)).toBe(100)
 * @category ops
 * @function
 */
export const maximumLeaf =
  <A>(Order: Order.Order<A>) =>
  (tree: Tree<A>): A =>
    pipe(tree, treeCata(maximumLeafFold(Order)))
 
/**
 * Just like `minimumLeaf` but returns the leaf tupled with its parent or `None`
 * if the tree if a leaf.
 * @category ops
 * @function
 */
export const minimumLeafAndParent = <A>(
  Order: Order.Order<A>,
): ((self: Tree<A>) => readonly [A, Option.Option<A>]) =>
  treeCata(minimumLeafParentFold(Order))
 
/**
 * Just like `maximumLeaf` but returns the leaf tupled with its parent or `None`
 * if the tree if a leaf.
 * @category ops
 * @function
 */
export const maximumLeafAndParent = <A>(
  Order: Order.Order<A>,
): ((self: Tree<A>) => readonly [A, Option.Option<A>]) =>
  treeCata(maximumLeafAndParentFold(Order))
 
/**
 * Find minimum node at tree level.
 * @category fold
 * @function
 */
export const minimumNodeFold: <A>(Order: Order.Order<A>) => TreeFolder<A, A> =
  Order => self =>
    Array.min(Order)([
      TreeF.getValue(self),
      ...(TreeF.isBranch(self) ? TreeF.getForest(self) : []),
    ])
 
/**
 * Find maximum node at tree level.
 * @category fold
 * @function
 */
export const maximumNodeFold: <A>(Order: Order.Order<A>) => TreeFolder<A, A> =
  Order => self =>
    Array.max(Order)([TreeF.getValue(self), ...TreeF.getForest(self)])
 
/**
 * Find minimum leaf at tree level.
 * @category fold
 * @function
 */
export const minimumLeafFold: <A>(Order: Order.Order<A>) => TreeFolder<A, A> =
  Order => treeF =>
    pipe(
      treeF,
      TreeF.match({
        onLeaf: identity,
        onBranch: (_, forest) => Array.min(Order)(forest),
      }),
    )
 
/**
 * Find minimum leaf at tree level.
 * @category fold
 * @function
 */
export const maximumLeafFold: <A>(Order: Order.Order<A>) => TreeFolder<A, A> =
  Order => treeF =>
    pipe(
      treeF,
      TreeF.match({
        onLeaf: identity,
        onBranch: (_, forest) => Array.max(Order)(forest),
      }),
    )
 
/**
 * Just like `minimumLeafFold` but result include the parent node of the
 * minimal leaf, or `None` if given tree is a leaf.
 * @category fold
 * @function
 */
export const minimumLeafParentFold = <A>(
  o: Order.Order<A>,
): TreeFolder<A, readonly [A, Option.Option<A>]> =>
  TreeF.match({
    onLeaf: pair.withSecond(Option.none<A>()),
    onBranch: (value, forest) =>
      pipe(
        forest,
        Array.min(parentOrder(o)),
        Tuple.mapSecond(Option.orElse<A>(() => Option.some(value))),
      ),
  })
 
/**
 * Just like `maximumLeafFold` but result include the parent node of the
 * maximal leaf, or `None` if given tree is a leaf.
 * @category fold
 * @function
 */
export const maximumLeafAndParentFold: typeof minimumLeafParentFold = flow(
  Order.reverse,
  minimumLeafParentFold,
)
 
function parentOrder<A>(
  o: Order.Order<A>,
): Order.Order<readonly [A, Option.Option<A>]> {
  return pipe(o, Order.mapInput(Tuple.getFirst))
}