An Elegant Way of Drawing a Grid using Julia

An Elegant Way of Drawing a Grid using Julia

This blog is an implementation of an exercise question taken from the book Think Julia: How to Think Like a Computer Scientist authored by Ben Lauwens and Allen Downey.

You can find the free ebook here.

Cover Image Credits: Glenn Carstens-Peters on Unsplash

What is a grid?

If you have ever seen a graph paper, then you know what a grid is. A grid is an arrangement of lines in such a way that both horizontal and vertical lines are interconnected.

In this article, I will try to explain the simple, elegant procedure of drawing a grid in Julia programming language.

To draw a grid, we need to know some string operators such as, * and ^.

  • * is a concatenation operator.
  • ^ is a repetition operator.

Let's understand these operators in detail.

Concatenation operator

Concatenation is defined as taking two are more strings and joining them as one whole string.

hello = "Hello"
world = "World"

helloworld = hello * ", " * world * "!"
println(helloworld)

If we execute this script, we see the output as below,

Hello, World!

Repetition operator

Repetition is defined as repeating a string a certain specified number of times.

fours_ = "4 " ^ 4
threes_ = "Three " ^ 3

println(fours_)
println(threes_)

If we execute this script, we see the output as below,

4 4 4 4
Three Three Three

Now that we understand these fundamental string operators, we can deep dive into the problem statement and figure out a solution to draw the grid.

As already discussed, a grid is an interconnected horizontal and vertical lines. To accomplish this, we need to first draw horizontal lines, vertical lines and finally connect them all together.

Rows

We will code a simple function to draw rows as below,

function draw_row(ncols, gaps_=4)
    row_string = (("+ " * (("- ") ^ gaps_)) ^ ncols) * "+"
    return row_string
end

We have three parameters,

  1. nrows specifies the number of rows we need in the grid.
  2. ncols specifies the number of columns we need in the grid.
  3. gaps_ is an optional or default parameter. It specifies the number of - between each +.

Columns

Now we will code a simple function to draw columns as below,

function draw_col(ncols, col_length=3, gaps_=4)
    col_string = (((("| " * (("  ") ^ gaps_)) ^ ncols) * "|") * "\n") ^ col_length
    return col_string
end

We have four parameters,

  1. nrows specifies the number of rows we need in the grid.
  2. ncols specifies the number of columns we need in the grid.
  3. col_length is an optional or default parameter. It specifies the distance between each row.
  4. gaps_ is an optional or default parameter. It specifies the number of double spaces between each |.

Grid

Now it's time to connect these rows and columns as a grid.

function draw_grid(nrows, ncols)
    grid = ((draw_row(ncols) * "\n" * draw_col(ncols)) ^ nrows) * draw_row(ncols)
    return grid
end

We have two parameters,

  1. nrows specifies the number of rows we need in the grid.
  2. ncols specifies the number of columns we need in the grid.

We are passing nrows and ncols to populate how many rows and columns we need for a grid.

Finally, we need to execute the below code to see the beautiful grid,

rows = 4
cols = 4

println(draw_grid(rows, cols))

Output after executing the script,

+ - - - - + - - - - + - - - - + - - - - +
|         |         |         |         |
|         |         |         |         |
|         |         |         |         |
+ - - - - + - - - - + - - - - + - - - - +
|         |         |         |         |
|         |         |         |         |
|         |         |         |         |
+ - - - - + - - - - + - - - - + - - - - +
|         |         |         |         |
|         |         |         |         |
|         |         |         |         |
+ - - - - + - - - - + - - - - + - - - - +
|         |         |         |         |
|         |         |         |         |
|         |         |         |         |
+ - - - - + - - - - + - - - - + - - - - +

Summary

This article explains the detailed procedure of drawing grids in Julia without using any concepts of loops. We employed only string operators to find a solution to the given problem statement.

Thank you for reading and learning from this blog.

End