All files / src/codec/paths fold.ts

100% Statements 18/18
100% Branches 4/4
100% Functions 3/3
100% Lines 18/18

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 351x 1x 1x 1x                               1x 1015x   1015x 1015x 1015x 1015x 1015x 478x 478x 478x 478x 478x 1015x 1015x  
import {type NonEmptyArray2TypeLambda, type TreeFolderK} from '#tree'
import * as TreeF from '#treeF'
import {type NonEmptyArray2} from '#Array'
import {Array, pipe} from 'effect'
 
/**
 * Collect all leaf paths from a tree at a level. For example:
 *
 * ```ts
 * const tree = make('A', [of('B'), make('C', [of('D', 'E')])])
 *
 * const paths = treeCata(pathListFold)(tree)
 * // [['A', 'B'], ['A', 'C', 'D'], ['A', 'C', 'E']]
 *
 * ```
 * @category fold
 * @category codec
 * @function
 */
export const pathListFold: TreeFolderK<NonEmptyArray2TypeLambda> = <A>(
  tree: TreeF.TreeF<A, NonEmptyArray2<A>>,
): NonEmptyArray2<A> =>
  pipe(
    tree,
    TreeF.match({
      onLeaf: node => [[node]] as NonEmptyArray2<A>,
      onBranch: (node, forest) =>
        pipe(
          forest,
          Array.flatten,
          Array.map(xs => [node, ...xs]),
        ),
    }),
  )