Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Tree.map_branches #15

Open
pveber opened this issue Oct 26, 2023 · 0 comments
Open

add Tree.map_branches #15

pveber opened this issue Oct 26, 2023 · 0 comments

Comments

@pveber
Copy link
Member

pveber commented Oct 26, 2023

let map_branches t ~f =
  let root_data = function
    | Tree.Leaf l -> `Leaf l
    | Tree.Node n -> `Node n.data
  in
  let rec traverse_node = function
    | Tree.Leaf _ as l -> l
    | Node n ->
      Tree.node n.data (List1.map n.branches ~f:(traverse_branch n.data))
  and traverse_branch parent_data (Branch bi) =
    Tree.branch
      (f parent_data bi.data (root_data bi.tip))
      (traverse_node bi.tip)
  in
  traverse_node t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant