Book Image

Julia 1.0 Programming Complete Reference Guide

By : Ivo Balbaert, Adrian Salceanu
Book Image

Julia 1.0 Programming Complete Reference Guide

By: Ivo Balbaert, Adrian Salceanu

Overview of this book

Julia offers the high productivity and ease of use of Python and R with the lightning-fast speed of C++. There’s never been a better time to learn this language, thanks to its large-scale adoption across a wide range of domains, including fintech, biotech and artificial intelligence (AI). You will begin by learning how to set up a running Julia platform, before exploring its various built-in types. This Learning Path walks you through two important collection types: arrays and matrices. You’ll be taken through how type conversions and promotions work, and in further chapters you'll study how Julia interacts with operating systems and other languages. You’ll also learn about the use of macros, what makes Julia suitable for numerical and scientific computing, and how to run external programs. Once you have grasped the basics, this Learning Path goes on to how to analyze the Iris dataset using DataFrames. While building a web scraper and a web app, you’ll explore the use of functions, methods, and multiple dispatches. In the final chapters, you'll delve into machine learning, where you'll build a book recommender system. By the end of this Learning Path, you’ll be well versed with Julia and have the skills you need to leverage its high speed and efficiency for your applications. This Learning Path includes content from the following Packt products: • Julia 1.0 Programming - Second Edition by Ivo Balbaert • Julia Programming Projects by Adrian Salceanu
Table of Contents (18 chapters)

Type unions

In geometry, a two-dimensional point and a vector are not the same, even if they both have an x and y component. In Julia, we can also define them as different types, as follows:

# see the code in Chapter 6\unions.jl
mutable struct Point
x::Float64
y::Float64
end

mutable struct Vector2D
x::Float64
y::Float64
end

Here are the two objects:

  • p = Point(2, 5) that returns Point(2.0, 5.0)

  • v = Vector2D(3, 2) that returns Vector2D(3.0, 2.0)

Suppose we want to define the sum for these types as a point which has coordinates as the sum of the corresponding coordinates:

+(p, v)

This results in an ERROR: MethodError: `+` has no method matching +(::Point, ::Vector2D) error message.

To define a + method here, first do an import Base.+

Even after defining the following, +(p, v) still returns the same error because of multiple dispatch. Julia has no way of knowing...