Skip to content

Commit

Permalink
Merge pull request #453 from exercism/fix-zipper-inspect-impl
Browse files Browse the repository at this point in the history
Fix `Inspect` implementation for zipper
  • Loading branch information
devonestes authored Mar 20, 2019
2 parents 1da2a7a + 1ec3028 commit 678c1b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
22 changes: 13 additions & 9 deletions exercises/zipper/example.exs
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
defmodule BinTree do
import Inspect.Algebra

@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

@type t :: %BinTree{value: any, left: t() | nil, right: t() | nil}

defstruct [:value, :left, :right]
end

defimpl Inspect, for: BinTree do
import Inspect.Algebra

# A custom inspect instance purely for the tests, this makes error messages
# much more readable.
#
# BT[value: 3, left: BT[value: 5, right: BT[value: 6]]] becomes (3:(5::(6::)):)
def inspect(%BinTree{value: v, left: l, right: r}, opts) do
# %BinTree{value: 3, left: %BinTree{value: 5, right: %BinTree{value: 6}}} becomes (3:(5::(6::)):)
def inspect(%BinTree{value: value, left: left, right: right}, opts) do
concat([
"(",
to_doc(v, opts),
to_doc(value, opts),
":",
if(l, do: to_doc(l, opts), else: ""),
if(left, do: to_doc(left, opts), else: ""),
":",
if(r, do: to_doc(r, opts), else: ""),
if(right, do: to_doc(right, opts), else: ""),
")"
])
end
Expand Down
10 changes: 7 additions & 3 deletions exercises/zipper/zipper.exs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
defmodule BinTree do
import Inspect.Algebra

@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}

@type t :: %BinTree{value: any, left: t() | nil, right: t() | nil}

defstruct [:value, :left, :right]
end

defimpl Inspect, for: BinTree do
import Inspect.Algebra

# A custom inspect instance purely for the tests, this makes error messages
# much more readable.
Expand Down

0 comments on commit 678c1b8

Please sign in to comment.