General purpose programming with a taste of data science Erik Engheim M A N N I N G Number Real Complex Abstract Abstract Rational Integer irrational float Irrational Float64 Float32 Bool Signed Unsigned Int32 Int64 UInt64 UInt8 Type hierarchy for numbers in Julia, showing abstract and concrete number types in dark gray and light gray, respectively AbstractArray AbstractRange DenseArray OrdinalRange Array Abstract StepRange UnitRange UnitRange Type hierarchy for arrays and ranges in Julia. The dark gray types are abstract while the light gray are concrete types. Julia as a Second Language Julia as a Second Language GENERAL PURPOSE PROGRAMMING WITH A TASTE OF DATA SCIENCE ERIK ENGHEIM MANNING SHELTER ISLAND For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: [email protected] ©2023 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. The author and publisher have made every effort to ensure that the information in this book was correct at press time. The author and publisher do not assume and hereby disclaim any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from negligence, accident, or any other cause, or from any usage of the information herein. Manning Publications Co. Development editor: Marina Michaels 20 Baldwin Road Technical development editor: Milan C´urcˇic´ PO Box 761 Review editor: Adriana Sabo Shelter Island, NY 11964 Production editor: Kathleen Rossland Copy editor: Christian Berk Proofreader: Jason Everett Technical proofreader: Maurizio Tomasi Typesetter: Dennis Dalinnik Cover designer: Marija Tudor ISBN: 9781617299711 Printed in the United States of America To my grandparents, who paid for my first computer: an Amiga 1000. brief contents PART 1 BASICS..........................................................................1 1 ■ Why Julia? 3 2 ■ Julia as a calculator 16 3 ■ Control flow 34 4 ■ Julia as a spreadsheet 59 5 ■ Working with text 86 6 ■ Storing data in dictionaries 104 PART 2 TYPES.......................................................................119 7 ■ Understanding types 121 8 ■ Building a rocket 141 9 ■ Conversion and promotion 160 10 ■ Representing unknown values 176 PART 3 COLLECTIONS...........................................................187 11 ■ Working with strings 189 12 ■ Understanding Julia collections 217 vii viii BRIEF CONTENTS 13 ■ Working with sets 242 14 ■ Working with vectors and matrices 260 PART 4 SOFTWARE ENGINEERING...........................................275 15 ■ Functional programming in Julia 277 16 ■ Organizing and modularizing your code 299 PART 5 GOING IN DEPTH.......................................................321 17 ■ Input and output 323 18 ■ Defining parametric types 335