Member-only story

Implement IO.Inspect in Elixir

Dunya Kirkali
2 min readDec 7, 2021

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

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Already have an account? Sign in

Dunya Kirkali
Dunya Kirkali

Written by Dunya Kirkali

I'm an engineering manager passionate about empowering engineers to deliver exceptional work through collaboration and innovation.

No responses yet

Write a response