Book Image

Julia 1.0 Programming - Second Edition

By : Ivo Balbaert
Book Image

Julia 1.0 Programming - Second Edition

By: Ivo Balbaert

Overview of this book

The release of Julia 1.0 is now ready to change the technical world by combining the high productivity and ease of use of Python and R with the lightning-fast speed of C++. Julia 1.0 programming gives you a head start in tackling your numerical and data problems. You will begin by learning how to set up a running Julia platform, before exploring its various built-in types. With the help of practical examples, this book walks you through two important collection types: arrays and matrices. In addition to this, you will be taken through how type conversions and promotions work. In the course of the book, you will be introduced to the homo-iconicity and metaprogramming concepts in Julia. You will understand how Julia provides different ways to interact with an operating system, as well as other languages, and then you'll discover what macros are. Once you have grasped the basics, you’ll study what makes Julia suitable for numerical and scientific computing, and learn about the features provided by Julia. By the end of this book, you will also have learned how to run external programs. This book covers all you need to know about Julia in order to leverage its high speed and efficiency for your applications.
Table of Contents (17 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Index

Sets


Array elements are ordered, but can contain duplicates, that is, the same value can occur at different indices. In a dictionary, keys have to be unique, but the values do not, and the keys are not ordered. If you want a collection where order does not matter, but where the elements have to be unique, then use a Set. Creating a set is as easy as this:

// code in Chapter 5\sets.jl: 
s = Set([11, 14, 13, 7, 14, 11])

The Set() function creates an empty set Set(Any[]). The preceding line returns Set([7, 14, 13, 11]), where the duplicates have been eliminated.

Operations from the set theory are also defined for s1 = Set([11, 25]) and s2 = Set([25, 3.14]) as follows:

  • union(s1, s2) produces Set([3.14,25,11])
  • intersect(s1, s2) produces Set([25])
  • setdiff(s1, s2) produces Set{Any}([11]), whereas setdiff(s2, s1) produces Set([ 3.14])
  • issubset(s1, s2) produces false, but issubset(s1, Set([11, 25, 36])) produces true

To add an element to a set is easy: push!(s1, 32) adds 32 to set s1. Adding an existing...