ebook img

Programming Elixir 1.3 PDF

341 Pages·2016·1.848 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Elixir 1.3

Programming Elixir 1.3 Dave Thomas The Pragmatic Bookshelf Copyright © 2016 The Pragmatic Programmers, LLC. Printed in the United States of America. ISBN-13: 978-1-68050-200-8 Encoded using the finest acid-free high-entropy binary digits. Book version: B1.3—October 13, 2016 Contents Changes in the Betas . . . . . . . . . . . ix Foreword . . . . . . . . . . . . . . xi A Vain Attempt at a Justification . . . . . . . xiii 1. Take the Red Pill . . . . . . . . . . . . 1 Programming Should Be About Transforming Data 1 Installing Elixir 4 Running Elixir 4 Suggestions for Reading the Book 9 Exercises 10 Think Different(ly) 10 Part I — Conventional Programming 2. Pattern Matching . . . . . . . . . . . 13 Assignment: I Do Not Think It Means What You Think It Means. 13 More Complex Matches 14 Ignoring a Value with _ (Underscore) 16 Variables Bind Once (per Match) 16 Another Way of Looking at the Equals Sign 18 3. Immutability . . . . . . . . . . . . 19 You Already Have (Some) Immutable Data 19 Immutable Data Is Known Data 20 Performance Implications of Immutability 21 Coding with Immutable Data 22 4. Elixir Basics . . . . . . . . . . . . . 23 Built-in Types 23 Value Types 24 System Types 26 Collection Types 26 Maps 29 Binaries 30 Dates and Times 31 Names, Source Files, Conventions, Operators, and So On 32 Variable Scope 34 End of the Basics 36 5. Anonymous Functions . . . . . . . . . . 37 Functions and Pattern Matching 38 One Function, Multiple Bodies 39 Functions Can Return Functions 41 Passing Functions As Arguments 43 Functions Are the Core 46 6. Modules and Named Functions . . . . . . . . 47 Compiling a Module 47 The Function’s Body Is a Block 48 Function Calls and Pattern Matching 49 Guard Clauses 52 Default Parameters 53 Private Functions 57 The Amazing Pipe Operator: |> 58 Modules 59 Module Attributes 62 Module Names: Elixir, Erlang, and Atoms 63 Calling a Function in an Erlang Library 63 Finding Libraries 64 7. Lists and Recursion . . . . . . . . . . . 65 Heads and Tails 65 Using Head and Tail to Process a List 66 Using Head and Tail to Build a List 68 Creating a Map Function 69 Keeping Track of Values During Recursion 70 More Complex List Patterns 73 The List Module in Action 76 Get Friendly with Lists 77 8. Maps, Keyword Lists, Sets, and Structs . . . . . . 79 How to Choose Between Maps and Keyword Lists 79 Keyword Lists 79 Maps 80 Pattern Matching and Updating Maps 81 Updating a Map 83 Structs 83 Nested Dictionary Structures 85 Sets 90 With Great Power Comes Great Temptation 91 9. An Aside—What Are Types? . . . . . . . . . 93 10. Processing Collections—Enum and Stream . . . . . 95 Enum—Processing Collections 95 Streams—Lazy Enumerables 99 The Collectable Protocol 106 Comprehensions 107 Moving Past Divinity 111 11. Strings and Binaries . . . . . . . . . . 113 String Literals 113 The Name “strings” 116 Single-Quoted Strings—Lists of Character Codes 117 Binaries 119 Double-Quoted Strings Are Binaries 120 Binaries and Pattern Matching 126 Familiar Yet Strange 127 12. Control Flow . . . . . . . . . . . . 129 if and unless 129 cond 130 case 133 Raising Exceptions 134 Designing with Exceptions 134 Doing More with Less 135 13. Organizing a Project . . . . . . . . . . 137 The Project: Fetch Issues from GitHub 137 Task: Use Mix to Create Our New Project 138 Transformation: Parse the Command Line 141 Step: Write Some Basic Tests 142 Transformation: Fetch from GitHub 144 Task: Use Libraries 145 Transformation: Convert Response 150 Transformation: Sort Data 152 Transformation: Take First n Items 154 Transformation: Format the Table 154 Task: Make a Command-Line Executable 157 Task: Add Some Logging 158 Task: Create Project Documentation 160 Coding by Transforming Data 161 14. Tooling . . . . . . . . . . . . . 165 Testing 165 Code Dependencies 178 Server Monitoring 179 Inevitably, There’s More 182 Part II — Concurrent Programming 15. Working with Multiple Processes . . . . . . . 185 A Simple Process 186 Process Overhead 191 When Processes Die 194 Parallel Map—The “Hello, World” of Erlang 198 A Fibonacci Server 199 Agents—A Teaser 204 Thinking in Processes 205 16. Nodes—The Key to Distributing Services . . . . . 207 Naming Nodes 207 Naming Your Processes 210 I/O, PIDs, and Nodes 214 Nodes Are the Basis of Distribution 216 17. OTP: Servers . . . . . . . . . . . . 217 Some OTP Definitions 217 An OTP Server 218 GenServer Callbacks 225 Naming a Process 227 Tidying Up the Interface 227 18. OTP: Supervisors . . . . . . . . . . . 231 Supervisors and Workers 231 Supervisors Are the Heart of Reliability 239 19. OTP: Applications . . . . . . . . . . . 241 This Is Not Your Father’s Application 241 The Application Specification File 242 Turning Our Sequence Program into an OTP Application 242 Supervision Is the Basis of Reliability 245 Releasing Your Code 245 EXRM—The Elixir Release Manager 246 OTP Is Big—Unbelievably Big 254 20. Tasks and Agents . . . . . . . . . . . 257 Tasks 257 Agents 258 A Bigger Example 261 Agents and Tasks, or GenServer? 263 Part III — More Advanced Elixir 21. Macros and Code Evaluation . . . . . . . . 267 Implementing an if Statement 267 Macros Inject Code 269 Using the Representation As Code 271 Using Bindings to Inject Values 276 Macros Are Hygienic 277 Other Ways to Run Code Fragments 278 Macros and Operators 279 Digging Deeper 280 Digging Ridiculously Deep 280 22. Linking Modules: Behavio(u)rs and Use . . . . . . 283 Behaviours 283 use and using 285 Putting It Together—Tracing Method Calls 286 Use use 289 23. Protocols—Polymorphic Functions . . . . . . . 291 Defining a Protocol 291 Implementing a Protocol 292 The Available Types 293 Protocols and Structs 294 Built-In Protocols 295 Protocols Are Polymorphism 302 24. More Cool Stuff . . . . . . . . . . . 305 Writing Your Own Sigils 305 Multi-app Umbrella Projects 309 But Wait! There’s More! 313 A1. Exceptions: raise and try, catch and throw . . . . . 315 Raising an Exception 315 catch, exit, and throw 317 Defining Your Own Exceptions 318 Now Ignore This Appendix 319 A2. Type Specifications and Type Checking . . . . . . 321 When Specifications Are Used 321 Specifying a Type 322 Defining New Types 324 Specs for Functions and Callbacks 325 Using Dialyzer 326 Bibliography . . . . . . . . . . . . 333 Index . . . . . . . . . . . . . . 335 Changes in the Betas B1.3 — October 13, 2016 • Production is complete. • Heading to layout and the printer. B1.2 — August 23, 2016 • Fixed errata. • Heading to production. B1.1 — August 11, 2016 • Updated the Issues app to remove convert_list_to_map. This is no longer needed as we’ve switched to the poison library for JSON parsing, and this already returns maps. • Fixed known errata. B1.0 – August 3, 2016 • Created a new Tooling chapter, with deeper coverage of testing, server monitoring, and the new mix dependency stuff. • You can no longer “assign” to a variable in the body of an if or case and then access that changed value outside the block. For example, in this code name = "Dave" if (use_formal_names) do name = "David" end IO.puts name the use of name outside the body of the if will give the warning: warning: the variable "name" is unsafe as it has been set inside a case/cond/receive/if/&&/||. Please explicitly return the variable value instead. • Mention the new ~D, ~T, and ~N date and time sigils, and briefly document the Date, Time, DateTime, and NaiveDateTime types. • Document the new Access module filter functions for get_in and friends. • Update function names in String: valid_character → valid? strip → trim lstrip → trim_leading rstrip → trim_trailing ljust → pad_trailing rjust → pad_leading All errata from the last printing of the original book have been fixed.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.