Author Archives: Great Lakes Consulting

Exploring Modules and Variable Scope in Julia Programming

By: Great Lakes Consulting

Re-posted from: https://blog.glcs.io/modules-variable-scope

Julia is a relatively new,
free, and open-source programming language.
It has a syntax
similar to that of other popular programming languages
such as MATLAB and Python,
but it boasts being able to achieve C-like speeds.

One way to organize Julia code
is to split functionality
into individual functions.
When enough functions exist,
it may become useful
to group the functions together,
along with any relevant global variables,
constants,
and type definitions.
Julia provides modules for this purpose.

Modules form the backbone of Julia packages,
helping to organize code
and minimize namespace collisions.

In this post,
we will learn about
modules in Julia,
and we will discuss how to create and use them.
Because modules each have their own global scope,
we will also learn about
scoping rules for variables.

This post assumes you already have
a basic understanding of variables and functions
in Julia.
You should also understand the difference
between functions and methods.
If you haven’t yet,
check out our earlier
post on variables and functions
as well as our post on multiple dispatch,
which explains the difference
between functions and methods.

Modules

The syntax for creating a module is

module ModuleName

# Code goes here.

end

Here’s an example module:

module MyModule

using Statistics

const A = "A global constant"
const B = [1, 2, 3]

func(x) = println("A: ", A, "\nmean(B): ", mean(B), "\nx: ", x)

export A, func

end

Let’s walk through this code.

  • First,
    the module loads another package:

    using Statistics
    

    Modules can load packages,
    just like we can do in the REPL.
    When a package is loaded in a module,
    the package is brought into the module’s namespace,
    meaning the loaded symbols
    (i.e., names referring to functions, types, constants, etc.)
    are not visible outside of the module.
    For example:

    julia> module StatsModule
           using Statistics
           end
    Main.StatsModule
    
    julia> mean([1, 2, 3])
    ERROR: UndefVarError: `mean` not defined
    
  • Next,
    the module defines its own data and functionality:

    const A = "A global constant"
    const B = [1, 2, 3]
    
    func(x) = println("A: ", A, "\nmean(B): ", mean(B), "\nx: ", x)
    

    This is the code
    we want to organize into a module.
    Typically,
    there are more lines of code,
    and they are saved in one or more separate files
    that are just included by the module via

    include("mycode.jl")
    
  • Finally,
    the module specifies some exports:

    export A, func
    

    Modules can export symbols
    that are then made available
    when the module is loaded with using.

Using Modules

When a module is created,
it can be referred to
by its name,
and any symbols in its namespace
can be accessed
by prepending the module name,
e.g., MyModule.func.
(This is called a qualified name.)

julia> MyModule
Main.MyModule

julia> MyModule.func(1)
A: A global constant
mean(B): 2.0
x: 1

If we want to make exported symbols
available without using a qualified name,
we can load the module with using:

julia> using .MyModule

julia> A
"A global constant"

(Here we note one difference
between packages and modules:
packages can be loaded with using PackageName,
whereas modules need their name
to be prepended with a period,
as seen above.)

After loading a module with using,
unexported symbols
are not made directly available,
but they can still be accessed
via a qualified name:

julia> B
ERROR: UndefVarError: `B` not defined

julia> MyModule.B
3-element Vector{Int64}:
 1
 2
 3

If we want to make an unexported symbol
available without using a qualified name,
we can explicitly load it:

julia> using .MyModule: B

julia> B
3-element Vector{Int64}:
 1
 2
 3

import Statements

import is another keyword
that can be used to load modules and packages.

import .MyModule will make available
just the name MyModule,
not any exported symbols:

julia> import .MyModule

julia> func(false) # Error, even though `func` is exported
ERROR: UndefVarError: `func` not defined

julia> MyModule.func(false) # Qualified names still work
A: A global constant
mean(B): 2.0
x: false

Using vs. import

import also allows methods
to be added to a module’s functions
without using a qualified name:

julia> import .MyModule: func

julia> func() = println("Method 2")
func (generic function with 2 methods)

julia> func()
Method 2

julia> func("MyModule.func")
A: A global constant
mean(B): 2.0
x: MyModule.func

For comparison,
below are two similar examples that use using:

  1. julia> using .MyModule: func
    
    julia> func() = println("Method 2")
    ERROR: error in method definition: function MyModule.func
    must be explicitly imported to be extended
    

    Here,
    we learn that we cannot add a method
    to a function from another module
    without importing the function,
    as we did earlier,
    or referring to the function
    with its qualified name,
    as shown below:

    julia> using .MyModule
    
    julia> MyModule.func() = println("Method 2")
    
    julia> func()
    Method 2
    
    julia> func("MyModule.func")
    A: A global constant
    mean(B): 2.0
    x: MyModule.func
    
  2. julia> using .MyModule
    
    julia> func() = println("Method 2")
    func (generic function with 1 method)
    
    julia> func()
    Method 2
    
    julia> func("MyModule.func")
    ERROR: MethodError: no method matching func(::String)
    
    julia> MyModule.func("MyModule.func")
    A: A global constant
    mean(B): 2.0
    x: MyModule.func
    

    Here,
    we see that,
    even though func is exported from MyModule,
    we created a different func function
    in the REPL
    because we did not import func
    or use a qualified name.
    As a result,
    future uses of func from MyModule
    must use its qualified name.

Adding methods to a module's function

Finally,
import enables renaming symbols:

julia> import .MyModule as MM

julia> MM.A
"A global constant"

julia> import .MyModule: B as NEWNAME

julia> NEWNAME
3-element Vector{Int64}:
 1
 2
 3

Common Modules

Now that we know how modules work,
let’s learn about a few modules
that every Julia programmer will come across:
Main, Base and Core.

  • Main:
    It turns out that all Julia code
    executes within a module.
    When Julia starts,
    a module named Main is created,
    and code that runs
    that isn’t explicitly contained in a module
    (e.g., code in the REPL)
    is executed within Main.
  • Base:
    Much of Julia’s basic functionality,
    including functions like
    +, print, and getindex,
    is defined in a module named Base.
    This module is automatically loaded
    into all modules
    (with few exceptions).
  • Core:
    Code that is considered built-in to Julia,
    i.e., code Julia needs to be able to function,
    lives in a module named Core.
    This module also is automatically loaded
    into all modules
    (with even fewer exceptions).

Variable Scope

Variable scope refers to where in code
a variable is accessible.
It therefore has implications
for when two pieces of code
can use the same variable name
without referring to the same thing.

There are three types of scopes in Julia:
global scope, hard local scope, and soft local scope.
We will discuss each of these in turn.

Global Scope

Symbols defined within a global scope
can be accessed within the global scope
and any local scopes
contained in the global scope.

Each module defines its own global scope.
Importantly,
there is no universal global scope,
meaning there is nowhere
we can define, e.g., x
and have x refer to the same thing everywhere,
even across modules.

Also note that global scopes do not nest,
in the sense that a nested module
cannot refer to a containing module’s global variable:

julia> module A
           a = 1
           module B
               b = a # `a` is undefined here, even though `B` is nested within `A`
           end
       end
ERROR: UndefVarError: `a` not defined

Hard Local Scope

Symbols defined within a hard local scope
can be accessed within the local scope
and any contained local scopes.

Functions, let blocks, and comprehensions
each introduce a hard local scope.

In a hard local scope,
variable assignment always assigns
to a local variable
unless the variable is explicitly declared as global
using the global keyword:

julia> let
           x = 1 # Assigns to a local variable `x`
       end;

julia> x
ERROR: UndefVarError: `x` not defined

julia> let
           global x
           x = 1 # Assigns to a global variable `x`
       end;

julia> x
1

Furthermore,
if there is a local variable, e.g., x,
in an outer local scope,
assignment to x in an inner local scope
will assign to the same x
in the outer local scope:

julia> let
           x = 0
           for i = 1:10
               s = x + i
               x = s # `x` is the existing local variable, not a new one
           end
           x # 55, not 0
       end
55

In particular,
it will not create a new local named x
unless x is explicitly declared local
in the inner scope.
This is called shadowing,
where names can be reused
to refer to different things:

julia> x = 1; # A global `x`

julia> let
           x = 2 # A local `x` shadowing the global `x`
           let
               local x
               x = 3 # An inner local `x` shadowing the outer local `x`
               @show x # Shows `x = 3`
           end
           @show x # Shows `x = 2`, not `x = 3`
       end;
x = 3
x = 2

julia> @show x; # Shows `x = 1`, not `x = 2` or `x = 3`
x = 1

Another example of shadowing:

julia> x = 1; # A global `x`

julia> function f(x)
           x + 1 # This `x` refers to the local `x`, not the global one
       end;

julia> f(3) # Computes `3 + 1`, not `1 + 1`
4

Soft Local Scope

for, while, and try blocks
each introduce a soft local scope.

Soft local scope is the same as hard local scope
except in interactive contexts
(e.g., when running code in the REPL)
and when assigning to a variable
(let’s call it x)
while the following conditions are met:

  1. x is not already a local variable.
  2. All enclosing local scopes are soft.
    (To illustrate,
    nested for loops within the REPL
    would satisfy this condition,
    while a for loop in a function
    would not.)
  3. A global variable x is defined.

In this case,
the global variable x is assigned
(as opposed to creating a new local variable x,
as would be done in a hard local scope).
(See the Julia documentation
for the rationale.)

Variable scope

Summary

In this post,
we learned how to create and use modules in Julia
for organizing code.
We also learned about Julia’s scoping rules
for global, hard local, and soft local scopes.

How do you use modules in your code?
Let us know in the comments below!

Additional Links

Harnessing the Power of Multiple Dispatch in Julia Programming

By: Great Lakes Consulting

Re-posted from: https://blog.glcs.io/multiple-dispatch

Julia is a relatively new,
free, and open-source programming language.
It has a syntax
similar to that of other popular programming languages
such as MATLAB and Python,
but it boasts being able to achieve C-like speeds.

One of the core design philosophies of Julia
that distinguishes it
from a lot of other languages
is multiple dispatch.
In essence,
multiple dispatch allows
writing multiple implementations
of a single function
based on different input types.
But unlike single dispatch
which relies on the static, compile-time types
of all but one of the inputs,
multiple dispatch chooses what implementation to call
using the runtime types.

It makes writing generic code a cinch,
allowing packages to work together seamlessly
and enabling a high degree of composability.

In this post,
we will learn about
multiple dispatch
in Julia.
We will discuss how to utilize it
and how it compares to single dispatch,
typically used
in object-oriented languages.

This post assumes you already have
a basic understanding
of Julia’s type system.
If you haven’t yet,
check out our earlier
post to learn more.

Functions and Methods

To understand multiple dispatch,
we first need to understand
the difference between functions and methods.

In Julia,
a function essentially is just a name.
This name is used to refer to the function
and to call the function.
What a function does when called
is determined by the methods
associated with the function.
A method is an implementation
of a function
for a given set of input types.

Let’s illustrate this concept with code.

First, we will define a function
with no methods,
just to show that it can be done:

julia> function no_methods end
no_methods (generic function with 0 methods)

When declaring a function, however,
we typically also define an associated method:

julia> function func(x, y) # Method 1
           x + y
       end
func (generic function with 1 method)

Here,
we created the function func
and defined a method
that adds the two function inputs.
Call this method 1.

Now let’s add another method,
method 2.
To do so,
we need to provide type annotations
to the input variables:

julia> function func(x::String, y::String) # Method 2
           x * y
       end
func (generic function with 2 methods)

Notice that we didn’t create
a new function with one method.
Because we used the same function name,
we added a method to the already existing function.

Let’s add one more method,
method 3:

julia> function func(x::String, y::Integer) # Method 3
           x^y
       end
func (generic function with 3 methods)

Methods can also have different numbers of arguments.
The previous methods all had two arguments,
so let’s create a method with three arguments,
method 4:

julia> function func(x::String, y::Integer, z::Integer) # Method 4
           func(x, y)[1:z]
       end
func (generic function with 4 methods)

Now let’s see multiple dispatch in action.

Multiple Dispatch

When func is called,
Julia uses the types of the input arguments
to determine which method to dispatch to.
In particular,
the input types of multiple arguments
(actually, all of them)
are considered,
hence the name multiple dispatch.
If the types of the inputs
match one of the method signatures,
that method will be called.

Multiple dispatch

But what about func("hi", "there")?
In this case,
both inputs are Strings,
so that matches method 2.
But String is a subtype of Any,
so the inputs also match method 1!
In cases like this
where multiple methods match,
the most specific method will be called.
So, in this case,
func("hi", "there") will dispatch to method 2
because String is a more specific type than Any.

If no methods match
or if there is not one single most specific method,
a MethodError will be thrown.

So,
the methods we defined for func
will be called in the following cases:

  • Method 1:
    When two inputs are given
    and no more specific methods exist.
    Examples:

    julia> func(1, 2)
    3
    
    julia> func([3, 4], [5, 6])
    2-element Vector{Int64}:
      8
     10
    
  • Method 2:
    When two String inputs are given.
    Example:

    julia> func("hi ", "there")
    "hi there"
    
  • Method 3:
    When two inputs are given:
    first a String and then an Integer.
    (Remember, though, that abstract types,
    like Integer,
    cannot be instantiated,
    so when we say the second input is an Integer,
    we really mean it is of a type
    that is a subtype of Integer.
    In other words,
    y isa Integer must evaluate to true.)
    Examples:

    julia> func("eat ", 3)
    "eat eat eat "
    
    julia> func("speak ", UInt32(2))
    "speak speak "
    
  • Method 4:
    When three inputs are given:
    first a String, then an Integer,
    and then another Integer.
    (Note that y and z
    do not have to be of the same type,
    i.e., y could be an Int64,
    while z could be a Bool.)
    Examples:

    julia> func("repeat", 5, 6)
    "repeat"
    
    julia> func("first letter", 1, true)
    "f"
    

Now, you may be thinking
that this just looks like method overloading.
And you would be correct
that the above examples
haven’t really demonstrated
the power of multiple dispatch.
So,
let’s compare multiple dispatch
to single dispatch.

Multiple Dispatch vs Single Dispatch

Having many method definitions
is also possible
with single dispatch.
However,
with single dispatch,
the compile-time types of the inputs
(except the first)
are used to decide which method to call.

Single dispatch uses the runtime type
of just the first function argument,
while multiple dispatch uses the runtime types
of all the function arguments.

Consider the following Java example:

abstract public class A
{
    public void func(A a)
    {
        System.out.println("Method AA");
    }
}

public class B extends A
{
    public void func(A a)
    {
        System.out.println("Method BA");
    }

    public void func(B b)
    {
        System.out.println("Method BB");
    }
}

public class Main
{
    public static void main(String[] args)
    {
        A ab = new B();
        B b = new B();

        ab.func(ab); // Method BA
        b.func(b); // Method BB
    }
}

In this example,
even though both ab and b
are instantiated as B objects,
b.func(ab) prints Method BA,
whereas b.func(b) prints Method BB.
This difference is because
ab and b have different compile-time types
(also called declared or static types):
ab is of type A,
whereas b is of type B.

Now let’s compare this to Julia:

# From package PkgA.jl
abstract type A end
func(a1::A, a2::A) = println("Method AA")

# From package PkgB.jl
import PkgA: A, func
struct B <: A end # `B <: A` means `B` is a subtype of `A`
func(b::B, a::A) = println("Method BA")
func(b1::B, b2::B) = println("Method BB")

# In the REPL or in a .jl file
using PkgA, PkgB

ab::A = B()
b::B = B()

func(ab, ab) # Method BB
func(b, b) # Method BB

In this case,
both func(ab, ab) and func(b, b) print Method BB,
despite annotating ab to be of type A,
because both ab and b
are of type B at runtime.

Again,
multiple dispatch uses the runtime types
of all input arguments
to determine the method to call.

One of the main resulting benefits
is that composability is easy in Julia.

Composability

Composability is the ability
to use different pieces of code together
to provide new functionality.

Puzzle pieces

First,
we will see an instance
where composability is difficult to achieve.

Let’s add to our Java example.
We will add a class C
that depends on A
but is independent of B:

public class C
{
    public static void run(A a1, A a2)
    {
        a1.func(a2);
    }
}

And we will add the following lines to main:

C.run(ab, ab); // Method BA
C.run(b, b); // Method BA - `C.run` cannot call Method BB!

Notice that,
even though b has a runtime type of B,
C.run(b, b) sill prints out Method BA.
This is because in class C,
the static type
of the second input to run is A, not B.
There are two ways to get run to print out Method BB:

  1. The author of class C changes their code
    to add better support for using objects of type B,
    e.g., by adding additional methods to run.
  2. The author of class Main reimplements run
    to take into account the existence of class B.

In either case,
code needs to be modified
for class B and class C
to compose well.
That’s not ideal for composability.

Now let’s see how this works out in Julia.
Suppose someone writes a package PkgC.jl
that contains the following:

using PkgA
run(a1::A, a2::A) = func(a1, a2)

Then we can update our example:

# In the REPL or in a .jl file
using PkgA, PkgB, PkgC

ab::A = B()
b::B = B()

PkgC.run(ab, ab) # Method BB
PkgC.run(b, b) # Method BB

Notice that PkgC.run(b, b) prints Method BB,
even though PkgC.jl
knows nothing about PkgB.jl!
No code changes were necessary
for PkgB.jl and PkgC.jl to compose.
That’s the power of multiple dispatch!

More Tips, Tricks, and General Advice

Now we will discuss various additional aspects
of multiple dispatch in Julia.

More Fine-Tuned Dispatching

Previously,
we saw that the method
func(x::String, y::Integer, z::Integer)
allowed y and z to be of different types
(as long as both were Integers).

What if we want a method
where y and z need to be
of the same type?
We can do so
by introducing a type parameter:

julia> function func(x::String, y::T, z::T) where T<:Integer # Method 5
           println(x, ": y and z are both of type ", T)
       end
func (generic function with 5 methods)

julia> func("welcome", 1, true) # Calls method 4
"w"

julia> func("welcome", 1, 1) # Calls method 5
welcome: y and z are both of type Int64

This new method will be called
when y and z are both Integers
of the same type.

Type parameters are also useful
for constraining the types
contained in container types.
For example,
suppose we want a method
that operates on an Array
that contains only real numbers.
The following method signature
accomplishes this:

another_func(a::Array{T}) where T<:Real

If T isn’t used anywhere else,
the following method signature
works as well:

another_func(a::Array{<:Real})

Method Ambiguities

When adding different methods,
it’s possible to introduce
method ambiguities,
where, for a given set of input types,
there is no single most specific method.

Recall that method 3 of func
had the following method signature:

func(x::String, y::Integer)

Adding the following method
will introduce a method ambiguity
when calling func
with a String and an Int:

julia> function func(x, y::Int)
           println(x, ": ", y)
       end
func (generic function with 6 methods)

julia> func("hello", 2)
ERROR: MethodError: func(::String, ::Int64) is ambiguous.

Candidates:
  func(x::String, y::Integer)
    @ Main REPL[4]:1
  func(x, y::Int64)
    @ Main REPL[16]:1

Possible fix, define
  func(::String, ::Int64)

Notice the list of candidate methods
in the error message;
neither is more specific than the other.

  • For the first argument,
    String is more specific than Any,
    so the first method is more specific.
  • For the second argument,
    Int64 is more specific than Integer,
    so the second method is more specific.

Method ambiguity

To overcome this issue,
either remove one of the offending methods
or add the method suggested
in the error message.

Beware of Type Piracy

Another thing to be careful about
when defining methods
is type piracy.
Type piracy occurs when
you add a method to a function
that isn’t yours
(i.e., that you didn’t originally define)
using types that aren’t yours.

For example,
defining simple_func(x::String)
is fine because simple_func
is a new function,
so it’s fine to use String,
a type that we didn’t define,
in the method signature.

Similarly,
defining Base.sum(x::MyNewType)
is fine because we defined
(in this hypothetical scenario)
MyNewType ourselves,
so it’s fine to add this method
to Julia’s sum function.

But what’s wrong with adding a method like
Base.sum(x::Array)?
The problem occurs when someone else
tries to use your code.
If they call sum on any Arrays,
your new method will be called
instead of Julia’s built-in sum!
That’s a nasty bug waiting to happen.

(If you want to live on the edge,
try defining
Base.:+(a::Int, b::Int) = error("pirate invasion")
and then watch as Julia crashes fantastically.
It turns out that being able
to correctly add two integers
is necessary for Julia to work properly!)

Summary

In this post,
we learned about
multiple dispatch
in Julia.
We discussed how to utilize it
and how it compares to single dispatch,
typically used
in object-oriented languages.

Do you have any further questions
about multiple dispatch?
Let us know in the comments below!

Additional Links

Basic Data Structures Explained

By: Great Lakes Consulting

Re-posted from: https://blog.glcs.io/basic-data-structures

Julia is a relatively new,
free, and open-source programming language.
It has a syntax
similar to that of other popular programming languages
such as MATLAB and Python,
but it boasts being able to achieve C-like speeds.

Julia provides several useful data structures
for storing and manipulating data.
Some of these data structures,
like arrays and dictionaries,
are ubiquitous in Julia code
because of their usefulness
and wide applicability.
Others, like sets,
have more limited uses
but nevertheless
still are useful data structures.

In this post,
we will learn about
arrays, dictionaries, and sets
in Julia.
We will discuss how to construct them
and describe various functions
for working with and manipulating them.

This post assumes you already have Julia installed.
If you haven’t yet,
check out our earlier
post on how to install Julia.

Arrays

One of the most basic and ubiquitous data structures
is the array.
Arrays are used for storing values,
iterating through values,
and even representing mathematical vectors and matrices.

The basic array type in Julia is Array{T,N},
where T is the type of the elements in the array
(or an abstract supertype of the elements
if not all elements are of the same type),
and N is the number of array dimensions.
For example,
a list of strings would be of type Array{String,1},
while a matrix of numbers would be of type Array{Float64,2}.

Constructing Arrays

There are various ways
to construct arrays.
One common way
is to construct an array
directly from the values
it will contain:

julia> ["some", "strings"]
2-element Vector{String}:
 "some"
 "strings"

julia> [1 2; 3 4]
2x2 Matrix{Int64}:
 1  2
 3  4

(Note that Vector{T} is equivalent to Array{T,1}
and that Matrix{T} is equivalent to Array{T,2}.)

Example arrays

Another common way
to construct arrays
is using array comprehensions.
An array comprehension
creates an array
by looping through a collection of values
and computing an array element
for each value.
For example,
the following creates an array
containing the squares
of the first five natural numbers:

julia> [x^2 for x = 1:5]
5-element Vector{Int64}:
  1
  4
  9
 16
 25

Multidimensional comprehensions also exist:

julia> [(x - 2)^2 + (y - 3)^2 <= 1 for x = 1:3, y = 1:5]
3x5 Matrix{Bool}:
 0  0  1  0  0
 0  1  1  1  0
 0  0  1  0  0

We can also create uninitialized arrays,
either by passing undef to the array constructor
or by calling similar:

julia> Array{Int,1}(undef, 1)
1-element Vector{Int64}:
 6303840

julia> similar([1.0, 2.0])
2-element Vector{Float64}:
 6.94291544947797e-310
 6.94291610129443e-310

(Note that the seemingly random numbers above
come from whatever bits happen to be set
in the memory allocated for the arrays.)

To create an array of zeros,
call zeros:

julia> zeros(2, 3)
2x3 Matrix{Float64}:
 0.0  0.0  0.0
 0.0  0.0  0.0

Inspecting Arrays

Information about an array
can be obtained using various functions.

length gives the number of elements
in an array:

julia> length([1, 2, 3])
3

julia> length(zeros(2, 3))
6

size(x) gives the size of x,
while size(x, d) gives the size
of the dth dimension:

julia> size([1, 2, 3])
(3,)

julia> size(zeros(2, 3))
(2, 3)

julia> size(zeros(2, 3), 2)
3

ndims gives the number of dimensions
of an array:

julia> ndims(zeros(1, 2, 3, 4, 5, 6))
6

And eltype gives the type
of the elements of an array:

julia> eltype(["two", "strings"])
String

julia> eltype([2, "different types"])
Any

Array Operations

Accessing array elements
is achieved using brackets:

julia> a = [10, 20, 30];

julia> a[2]
20

(Note that arrays use one-based indexing
in Julia.)

A similar syntax is used
to modify the contents of an array:

julia> a[1] = 0
0

julia> a
3-element Vector{Int64}:
  0
 20
 30

Use commas (,) to separate indexes
for different dimensions,
and use a colon (:)
to select all the values
along a dimension:

julia> m = [1 2; 3 4]
2x2 Matrix{Int64}:
 1  2
 3  4

julia> m[1,2]
2

julia> m[:,1]
2-element Vector{Int64}:
 1
 3

Multiple indexes can be provided:

julia> a[[1, 3]]
2-element Vector{Int64}:
  0
 30

To assign a single value
to multiple array locations,
use broadcasting:

julia> a[2:3] .= 0
2-element view(::Vector{Int64}, 2:3) with eltype Int64:
 0
 0

julia> a
3-element Vector{Int64}:
 0
 0
 0

Arrays are also iterable,
meaning we can loop through
the values of an array:

julia> words = ["this", "is", "a", "sentence"];

julia> for w in words
           println(w)
       end
this
is
a
sentence

Arrays as Stacks/Queues/Dequeues

Julia also provides some functions
that allow arrays to be used
in a similar way
as stacks, queues, and dequeues.
For example,
push!(array, x) inserts x
at the end of an array,
and pop!(array) removes the last element
of an array.
Similarly,
pushfirst! and popfirst
act on the beginning of an array.

Ranges

Ranges are another useful type of array,
often used for looping
and array indexing.

The simplest syntax
for creating a range
is a:b,
which creates a range
that starts at a
and includes all values
a + 1, a + 2, etc.,
as long as a + n <= b.
For example,
1:5 contains 1, 2, 3, 4, and 5,
while 1.0:2.5 contains 1.0 and 2.0.

A step size, s, can also be specified,
as in a:s:b.
In this case, the spacing between values
in the range
is s instead of 1.

To create a range of N points
between a and b, inclusive,
use range(a, b, N).

Unlike Arrays,
ranges are immutable,
meaning their elements
can’t be modified.
If modifying an element of a range
is necessary,
it must first be converted
into an Array
by calling collect:

julia> r = 1:2
1:2

julia> r[1] = 10
ERROR: CanonicalIndexError: setindex! not defined for UnitRange{Int64}

julia> r_arr = collect(r)
2-element Vector{Int64}:
 1
 2

julia> r_arr[1] = 10; r_arr
2-element Vector{Int64}:
 10
  2

That concludes our discussion of arrays,
so now let’s move on to dictionaries.

Dictionaries

Another very common data structure
is the dictionary.
A dictionary is a mapping
from keys to values:
give a dictionary a key,
and it will return
the value associated with that key
(if present).

In Julia,
dictionaries are of type Dict{K,V},
where K is the type of the keys,
and V is the type of the values.

Dictionaries are constructed
by providing key-value pairs:

julia> d = Dict("key1" => 1, "key2" => 2, "key3" => 3)
Dict{String, Int64} with 3 entries:
  "key2" => 2
  "key3" => 3
  "key1" => 1

(Note that a => b
creates a Pair in Julia.)

Indexing a dictionary
uses the same syntax
as indexing an array,
just using keys
instead of array indexes:

julia> d["key2"]
2

Accessing a dictionary

Use haskey to check
for the presence of a key:

julia> haskey(d, "key3")
true

julia> haskey(d, "nope")
false

Dictionaries can also be updated:

julia> d["key1"] = 9999
9999

julia> d["newkey"] = -9999
-9999

julia> d
Dict{String, Int64} with 4 entries:
  "key2"   => 2
  "key3"   => 3
  "key1"   => 9999
  "newkey" => -9999

Use delete!(dict, key)
to delete the mapping
for the given key,
if present.

We can also iterate
through the keys and/or values
of a dictionary:

  • Iterating keys: for k in keys(dict)
  • Iterating values: for v in values(dict)
  • Iterating both: for (k, v) in dict

That wraps up our discussion of dictionaries,
so now we will move on to sets.

Sets

A set is a collection of unique elements.
In Julia,
sets are of type Set{T},
where T is the type
of the elements of the set.
Sets are useful
for their efficient set operations,
such as membership testing,
union, and intersect.

Create an empty set of Float64 values as follows:

julia> s = Set{Float64}()
Set{Float64}()

Use push! to add values
to the set,
noticing that the set changes
only if the value does not already exist
in the set:

julia> push!(s, 1.0);

julia> push!(s, 1.2);

julia> push!(s, 3.14)
Set{Float64} with 3 elements:
  1.2
  3.14
  1.0

julia> push!(s, 1.0)
Set{Float64} with 3 elements:
  1.2
  3.14
  1.0

Use union to take the union
of two sets:

julia> t = Set([1.0, 2.0])
Set{Float64} with 2 elements:
  2.0
  1.0

julia> r = s  t # type \cup<tab> to get the union symbol
Set{Float64} with 4 elements:
  1.2
  2.0
  3.14
  1.0

(Note that s t == union(s, t).)

Use intersect to take the intersection
of two sets:

julia> r  t # type \cap<tab> to get the intersection symbol
Set{Float64} with 2 elements:
  2.0
  1.0

(Note that s t == intersect(s, t).)

Finally,
we can check if an element
belongs to a set
with in:

julia> 1.0  r # type \in<tab> to get the "is an element of" symbol
true

(Note that and in are interchangeable here.)

And with that,
we conclude our overview
of some important Julia data structures.

Summary

In this post,
we learned about a few data structures
that Julia provides:
arrays, dictionaries, and sets.
We learned how to construct them
and how to work with and manipulate them.

What are the most useful data structures
you have used?
Let us know in the comments below!

Additional Links