<commit_before>defmodule BinTree do
  @moduledoc """
  A node in a binary tree.

  `value` is the value of a node.
  `left` is the left subtree (nil if no subtree).
  `right` is the right subtree (nil if no subtree).
  """
  @type t :: %BinTree{  value: any,
                        left: BinTree.t | nil,
                        right: BinTree.t | nil }
  defstruct value: nil, left: nil, right: nil
end

defmodule Zipper do
  # if trail is [], the position of zipper is root
  defstruct value: nil, left: nil, right: nil, trail: []

  def from_tree(bt) do
    %Zipper{value: bt.value, left: bt.left, right: bt.right}
  end

  def to_tree(%Zipper{trail: []} = z) do
    %BinTree{value: z.value, left: z.left, right: z.right}
  end
  def to_tree(%Zipper{trail: _} = z), do: z |> up |> to_tree

  def value(z), do: z.value

  def left(%Zipper{left: nil}), do: nil
  def left(%Zipper{left: focus}=z) do
    %Zipper{value: focus.value, left: focus.left, right: focus.right,
            trail: [{:left, z.value, z.right} | z.trail]}
  end

  def right(%Zipper{right: nil}), do: nil
  def right(%Zipper{right: focus}=z) do
    %Zipper{value: focus.value, left: focus.left, right: focus.right,
            trail: [{:right, z.value, z.left} | z.trail]}
  end

  def up(%Zipper{trail: []}), do: nil
  def up(%Zipper{trail: [{:left, val, right} | trail]} = z) do
    left = %BinTree{value: z.value, left:  z.left, right: z.right}
    %Zipper{value: val,
            left: left,
            right: right,
            trail: trail}
  end
  def up(%Zipper{trail: [{:right, val, left}|trail]} = z) do
    right = %BinTree{value: z.value, left:  z.left, right: z.right}
    %Zipper{value: val,
            left: left,
            right: right,
            trail: trail}
  end
<commit_msg>refactor<commit_after>defmodule BinTree do
  @moduledoc """
  A node in a binary tree.

  `value` is the value of a node.
  `left` is the left subtree (nil if no subtree).
  `right` is the right subtree (nil if no subtree).
  """
  @type t :: %BinTree{  value: any,
                        left: BinTree.t | nil,
                        right: BinTree.t | nil }
  defstruct value: nil, left: nil, right: nil
end

defmodule Zipper do
  # if trail is [], the position of zipper is root
  defstruct value: nil, left: nil, right: nil, trail: []

  def from_tree(bt) do
    %Zipper{value: bt.value, left: bt.left, right: bt.right}
  end

  def to_tree(%Zipper{trail: []} = z) do
    %BinTree{value: z.value, left: z.left, right: z.right}
  end
  def to_tree(%Zipper{trail: _} = z), do: z |> up |> to_tree

  def value(z), do: z.value

  def left(%Zipper{left: nil}), do: nil
  def left(%Zipper{left: left}=z) do
    %Zipper{value: left.value, left: left.left, right: left.right,
            trail: [{:left, z.value, z.right} | z.trail]}
  end

  def right(%Zipper{right: nil}), do: nil
  def right(%Zipper{right: right}=z) do
    %Zipper{value: right.value, left: right.left, right: right.right,
            trail: [{:right, z.value, z.left} | z.trail]}
  end

  def up(%Zipper{trail: []}), do: nil
  def up(%Zipper{trail: [{:left, val, right} | trail]} = z) do
    left = %BinTree{value: z.value, left:  z.left, right: z.right}
    %Zipper{value: val, left: left, right: right, trail: trail}
  end
  def up(%Zipper{trail: [{:right, val, left}|trail]} = z) do
    right = %BinTree{value: z.value, left:  z.left, right: z.right}
    %Zipper{value: val, left: left, right: right, trail: trail}
  end

{'fraction_non_alphanumeric': 0.1557542543986155, 'fraction_numerical': 0.0, 'mean_word_length': 2.781897491821156, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4131105', 'n_tokens_mistral': 1288, 'n_tokens_neox': 1273, 'n_words': 398}