Elixir is an excellent language if you want to learn about functional programming, and with this hands-on introduction, you’ll discover just how powerful and fun Elixir can be. This language combines the robust functional programming of Erlang with a syntax similar to Ruby, and includes powerful features for metaprogramming. This book shows you how to write simple Elixir programs by teaching one skill at a time. Once you pick up pattern matching, process-oriented programming, and other concepts, you’ll understand why Elixir makes it easier to build concurrent and resilient programs that scale up and down with ease. * Get comfortable with IEx, Elixir’s command line interface * Discover atoms, pattern matching, and guards: the foundations of your program structure * Delve into the heart of Elixir with recursion, strings, lists, and higher-order functions * Create processes, send messages among them, and apply pattern matching to incoming messages * Store and manipulate structured data with Erlang Term Storage and the Mnesia database * Build resilient applications with Erlang’s Open Telecom Platform * Define macros with Elixir’s metaprogramming tools Copyright 4 Table of Contents 5 Preface 9 Who This Book Is For 9 Who This Book Is Not For 9 What This Book Will Do For You 10 How This Book Works 10 Other Resources 11 Elixir Will Change You 12 Conventions Used in This Book 12 Using Code Examples 13 Help This Book Grow 14 Please Use It For Good 14 Safari庐 Books Online 14 How to Contact Us 15 Acknowledgments 15 Chapter聽1.聽Getting Comfortable 17 Installation 17 Installing Erlang 17 Installing Elixir 18 Firing It Up 18 First Steps 18 Moving Through Text and History 19 Moving Through Files 19 Doing Something 20 Calling Functions 21 Numbers in Elixir 22 Working with Variables in the Shell 24 Chapter聽2.聽Functions and Modules 27 Fun with fn 27 And the & 29 Defining Modules 29 From Module to Free-Floating Function 32 Splitting Code Across Modules 33 Combining Functions with the Pipe Operator 34 Importing Functions 35 Default Values for Arguments 36 Documenting Code 37 Documenting Functions 38 Documenting Modules 39 Chapter聽3.聽Atoms, Tuples, and Pattern Matching 41 Atoms 41 Pattern Matching with Atoms 41 Atomic Booleans 43 Guards 44 Underscoring That You Don鈥檛 Care 47 Adding Structure: Tuples 49 Pattern Matching with Tuples 49 Processing Tuples 50 Chapter聽4.聽Logic and Recursion 53 Logic Inside of Functions 53 Evaluating Cases 53 Adjusting to Conditions 56 If, or else 57 Variable Assignment in case and if Constructs 58 The Gentlest Side Effect: IO.puts 59 Simple Recursion 60 Counting Down 61 Counting Up 62 Recursing with Return Values 63 Chapter聽5.聽Communicating with Humans 67 Strings 67 Multiline Strings 69 Unicode 70 Character Lists 70 String Sigils 71 Asking Users for Information 72 Gathering Characters 72 Reading Lines of Text 74 Chapter聽6.聽Lists 77 List Basics 77 Splitting Lists into Heads and Tails 79 Processing List Content 80 Creating Lists with Heads and Tails 82 Mixing Lists and Tuples 84 Building a List of Lists 84 Chapter聽7.聽Name-Value Pairs 89 Keyword Lists 89 Lists of Tuples with Multiple Keys 91 Hash Dictionaries 92 From Lists to Maps 93 Creating Maps 93 Updating Maps 94 Reading Maps 94 From Maps to Structs 94 Setting Up Structs 95 Creating and Reading Structs 95 Pattern Matching Against Structs 96 Using Structs in Functions 96 Adding Behavior to Structs 98 Adding to Existing Protocols 100 Chapter聽8.聽Higher-Order Functions and List Comprehensions 103 Simple Higher-Order Functions 103 Creating New Lists with Higher-Order Functions 105 Reporting on a List 106 Running List Values Through a Function 106 Filtering List Values 107 Beyond List Comprehensions 108 Testing Lists 108 Splitting Lists 109 Folding Lists 110 Chapter聽9.聽Playing with Processes 113 The Shell Is a Process 113 Spawning Processes from Modules 115 Lightweight Processes 118 Registering a Process 118 When Processes Break 120 Processes Talking Amongst Themselves 121 Watching Your Processes 123 Watching Messages Among Processes 125 Breaking Things and Linking Processes 126 Chapter聽10.聽Exceptions, Errors, and Debugging 135 Flavors of Errors 135 Rescuing Code from Runtime Errors as They Happen 136 Logging Progress and Failure 138 Tracing Messages 139 Watching Function Calls 141 Writing Unit Tests 142 Chapter聽11.聽Storing Structured Data 147 Records: Structured Data Before structs 147 Setting Up Records 148 Creating and Reading Records 149 Using Records in Functions 150 Storing Data in Erlang Term Storage 152 Creating and Populating a Table 154 Simple Queries 158 Overwriting Values 159 ETS Tables and Processes 160 Next Steps 162 Storing Records in Mnesia 162 Starting up Mnesia 162 Creating Tables 163 Reading Data 167 Chapter聽12.聽Getting Started with OTP 169 Creating Services with gen_server 170 A Simple Supervisor 175 Packaging an Application with Mix 178 Chapter聽13.聽Using Macros to Extend Elixir 183 Functions versus Macros 183 A Simple Macro 184 Creating New Logic 186 Creating Functions Programatically 187 When (Not) to Use Macros 189 Sharing the Gospel of Elixir 189 Appendix聽A.聽An Elixir Parts Catalog 191 Shell Commands 191 Reserved Words 192 Operators 192 Guard Components 195 Common Functions 195 Datatypes for Documentation and Analysis 198 Appendix聽B.聽Generating Documentation with ExDoc 199 Using ExDoc with mix 199 Index 203 About the Authors 209 Elixir is an excellent language if you want to learn about functional programming, and with this hands-on introduction, you’ll discover just how powerful and fun Elixir can be. This language combines the robust functional programming of Erlang with a syntax similar to Ruby, and includes powerful features for metaprogramming.This book shows you how to write simple Elixir programs by teaching one skill at a time. Once you pick up pattern matching, process-oriented programming, and other concepts, you’ll understand why Elixir makes it easier to build concurrent and resilient programs that scale up and down with ease.Get comfortable with IEx, Elixir’s command line interfaceDiscover atoms, pattern matching, and guards: the foundations of your program structureDelve into the heart of Elixir with recursion, strings, lists, and higher-order functionsCreate processes, send messages among them, and apply pattern matching to incoming messagesStore and manipulate structured data with Erlang Term Storage and the Mnesia databaseBuild resilient applications with Erlang’s Open Telecom PlatformDefine macros with Elixir’s metaprogramming tools Annotation Elixir is an excellent language if you want to learn about functional programming, and with this hands-on introduction, youll discover just how powerful and fun Elixir can be. This language combines the robust functional programming of Erlang with a syntax similar to Ruby, and includes powerful features for metaprogramming. This book shows you how to write simple Elixir programs by teaching one skill at a time. Once you pick up pattern matching, process-oriented programming, and other concepts, youll understand why Elixir makes it easier to build concurrent and resilient programs that scale up and down with ease. Get comfortable with IEx, Elixirs command line interfaceDiscover atoms, pattern matching, and guards: the foundations of your program structureDelve into the heart of Elixir with recursion, strings, lists, and higher-order functionsCreate processes, send messages among them, and apply pattern matching to incoming messagesStore and manipulate structured data with Erlang Term Storage and the Mnesia databaseBuild resilient applications with Erlangs Open Telecom PlatformDefine macros with Elixirs metaprogramming tools