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

    Function head

    • Navigate from a node to its first child or throw an exception if the focused node is a Leaf. Unsafe version of tryHead.

      Type Parameters

      • A

        The underlying type of the tree.

      Parameters

      Returns Zipper.Zipper<A>

      An updated zipper pointing at a new focus.

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

      // ┬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.head(start)
      expect(Zipper.getValue(hop1)).toBe(2)

      const hop2 = Zipper.head(hop1)
      expect(Zipper.getValue(hop2)).toBe(3)

      // Out-of-bounds exception when nowhere left to go.
      expect(() => Zipper.head(hop2)).toThrow(/getOrThrow/)