How To Use Modules In Julia. A quick lesson on how modules and... | by Emmett Boudreau | Towards Data Science

Video for this article:
Github:
github.com
While the things we have gone over in the past Julia tutorials; functions, constructors, and types are certainly valuable, there is no possible way that one programmer could build an entire ecosystem from scratch. That being said, programmers often use packages to perform arithmetic that their package is not necessarily targeted towards, but requires. Packages are repositories of code that can be re-used as dependencies for other packages to iterate and build on.
We have already taken a broad overview of Pkg and how it can be used to install packages for the Julia programming language. Today we are going to be working with a module called Compose.jl to get some practice with programming in Julia. I chose this library because it is a functional and declarative library that follows the same programming methodologies and principles we have been covering in these tutorials. Additionally, being a graphics package means that one can see the direct result of their code in very visual output — which could be handy to a new programmer. If you’d like to learn more about the module, you can check it out on Juliahub:
juliahub.com
In order to add a package in Julia, we will use Pkg. You can do this by either importing Pkg into Julia with using, or just using the Pkg REPL that can be accessed by pressing ] in the Julia REPL. Adding is relatively straight forward and can be done using either the Pkg.add() method or the add command in the Pkg REPL. Of course, today we are going to be working with Compose.jl, so you might want to go ahead and add it:
julia> ]pkg> add Compose
This will add Compose to our global package environment. Some Julia developers would strongly advise against this, but I am one to disagree — I think that having dependencies on your machine is perfectly alright so long as they are frequently used dependencies. Furthermore, one can always remove their packages with the rm command.
julia> ]pkg> rm Compose
If you’d like to learn more about why Pkg is cool, you can check out an article I did a while back on it here! :
towardsdatascience.com
In order to get a module into our Julia runtime environment, we have a series of different options to import it. First and foremost is using. Using just means that the package is going to be pre-compiled and called from within the run-time and not be mutable from within the runtime. We can use the Compose package with
using Compose
In the Julia language, it is quite common to work with exported functions and structs. All that this means is that these functions will not need to be called via their parents, but instead can be called directly when the module is imported. We can avoid using exports by directly importing the types or functions that we want to use in Julia:
using Compose: compose
A direct import will load the code we are targeting without including all of the other ingredients inside of the package. Directly importing can also save compile times, and in registered software it is definitely recommended for dependencies where only one or two different types or functions are to be used in your software.
On top of using, in the Julia language we also have access to the import keyword. Importing code will do precisely the opposite of using and will allow packages to be extended. Typically things do not need to be explicitly imported unless they are to be extended.
import Compose# Direct explicit import:import Compose: compose
With the basics of utilizing modules in Julia out of the way, we can now look at the module we are going to be working with today:
Compose.jl
Compose is a declarative vector graphics library written in pure Julia that makes it incredibly easy to functionally draw graphics using only a few parameters and nodes. You can view the Compose documentation on Juliahub here:
juliahub.com
Speaking of which, if you are new to Julia and using Julia modules, it certainly might be worth it to check out JuliaHub. JuliaHub is an all-in-one package destination for Julia programmers with the documentation readily included and integrated automatically inside the website. If you would like to learn more, I have a whole article on how cool it is!:
towardsdatascience.com
Pulling directly from the Compose documentation, our first example is simple but gets the point across:
composition = compose(context(), rectangle(), fill("tomato"))
Looking more into this statement, the compose() method is responsible for returning a composition and takes parameters. These parameters are types called Compose forms and nodes. Inside of the method call, we see the arguments as context(), rectangle(), and fill(). Context is the root node, so it can essentially be ignored in this particular instance. Next in the arguments is rectangle. This is of course the type that is on display in the rendered image. The last argument is fill(), which takes a string that represents the title of the desired color for this entire context().
Fortunately, we can use the ?() method on any of these arguments to learn more about them. Let’s create more rectangles! We will start by seeing what kind of data we can provide whenever we create the type:
Rectangle is both capable of taking 0 arguments, or four. Four arguments in this regard will be positional arguments, the x position of our form, the y position of our form, and then the width and height in that order. With this newly found information, let’s modify our old code to change the size of the rectangle:
composition = compose(context(), rectangle(.5, .5, 5px, 5px), fill("tomato"))
And adding more rectangles:
composition = compose(context(), rectangle(.5, .5, 20px, 20px),    rectangle(.5, .2, 20px, 20px),    rectangle(.5, .8, 20px, 20px),    fill("tomato"))
A concept that might be confusing is the idea of using import vs. using to import modules into Julia. This is understandable, as many languages don’t typically involve this feature; and most that do use using to take classes and functions out of namespace. That being said, the most distinguishable difference between using and import that can be felt by the average Julia programmer is the ability to extend modules. This was touched on earlier in the article, but to elaborate with code, dispatching a rectangle to only take two positional arguments instead of one or two is not possible like this:
using Compose: rectanglerectangle(x::Float64, y::Float64) = rectangle(x, y, 30px, 30px)
However, it is possible if we use import instead of using:
import Compose: rectanglerectangle(x::Float64, y::Float64) = rectangle(x, y, 30px, 30px)
No programmer could possibly write the entire world’s software, and fortunately they don’t have to. This is because millions of open-source developers are at work on fantastic modules that are available for every programmer to use. In Julia, we can add some of these packages using Pkg. The most important things to take away about modules from today are:
The ?() method
JuliaHub documentation
Adding packages
The difference between import and using
Mastering these concepts will make you a true master of the Julia language, and you are on your way there!