Member-only story
Implement IO.Inspect in Elixir
I’ve been joining the Advent Of Code every year and every year there is a puzzle that requires us to draw a map to do some calculations on it (eg. finding the shortest path from A to B).
How to represent such a puzzle in Elixir is quite straightforward (using a Map to store the coordinates where the key is a a tuple {x, y}) but debugging it is quite challenging. Especially when the puzzle input gets quite large.
Recently, José Valim did a live stream on Twitch where he solved Day 4. After solving it he went on to explain how he would go about implementing IO.Inspect for a struct.
In this post, I will implement the same for a chess board so that I get a chance to train his teachings.
Context
Let’s imagine that we’re building a chess game and that we already have a Board

If we would go and inspect it we would see the following

What we get is not incorrect but it’s definitely not easy to reason about.
So how would we go about implementing IO.inspect?
Implementation
One could implement IO.Inspect as follows

Which would mean that if you would inspect a Chess.Board you would see something like this
