effect-tree - v1.0.36
    Preparing search index...

    Function tryDepthFirst

    • Navigate from a node to the next node in a depth-first pre-order traversal: parents are visited before their children, and both are visited before the next sibling of the parent. For example:

      For the tree:
                        ┌─┐
                        │1│
                   ╭────┴─┴─────╮
                 ┌─┴─┐        ┌─┴─┐
                 │1.1│        │1.2│
               ╭─┴───┴╮      ╭┴───┴─╮
            ┌──┴──┐┌──┴──┐┌──┴──┐┌──┴──┐
            │1.1.1││1.1.2││1.2.1││1.2.2│
            └─────┘└─────┘└─────┘└─────┘
      
          tryDepthFirst ┊ result of
            called on...┊ navigation
          ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┼┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈
                   “1”  ┊ some(“1.1”)
                 “1.1”  ┊ some(“1.2”)
               “1.1.1”  ┊ some(“1.1.2”)
               “1.1.2”  ┊ some(“1.2”)
                 “1.2”  ┊ some(“1.2.1”)
               “1.2.1”  ┊ some(“1.2.2”)
               “1.2.2”  ┊ none()
      

      Returns Option.none when the final node in the traversal has been reached.

      See depthFirst for an unsafe version.

      Type Parameters

      • A

        The underlying type of the tree.

      Parameters

      Returns Option<Zipper.Zipper<A>>

      An updated zipper pointing at a new focus or Option.none() if there is no next node in the depth-first traversal.

      import {Zipper, from, of} from 'effect-tree'
      import {pipe, Option} from 'effect'

      // ┬1
      // ├┬2
      // │├─3
      // │└─4
      // └┬5
      // ├─6
      // └─7
      const tree = from(1, from(2, of(3), of(4)), from(5, of(6), of(7)))

      const start = Zipper.fromTree(tree)

      const hop1 = Zipper.tryDepthFirst(start)
      const value1 = pipe(hop1, Option.map(Zipper.getValue))
      expect(value1, 'hop1').toEqual(Option.some(2))

      const hop2 = pipe(hop1, Option.flatMap(Zipper.tryDepthFirst))
      const value2 = pipe(hop2, Option.map(Zipper.getValue))
      expect(value2, 'hop2').toEqual(Option.some(3))