O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Programming Elixir 1.6

Book Description

This book is the introduction to Elixir for experienced programmers, completely updated for Elixir 1.6 and beyond. Explore functional programming without the academic overtones (tell me about monads just one more time). Create concurrent applications, butget them right without all the locking and consistency headaches. Meet Elixir, a modern, functional, concurrent language built on the rock-solid Erlang VM. Elixir's pragmatic syntax and built-in support for metaprogramming will make you productive and keep you interestedfor the long haul. Maybe the time is right for the Next Big Thing. Maybe it's Elixir.

Functional programming techniques help you manage the complexities of today's real-world, concurrent systems; maximize uptime; and manage security. Enter Elixir, with its modern, Ruby-like, extendable syntax, compile and runtime evaluation, hygienic macro system, and more. But, just as importantly, Elixir brings a sense of enjoyment to parallel,functional programming. Your applications become fun to work with, and the language encourages you to experiment.

Part 1 covers the basics of writing sequential Elixir programs. We'll look at the language, the tools, and the conventions. Part 2 uses these skills to start writing concurrent code-applications that use all the cores on your machine, or all the machines on your network! And we do it both with and without OTP. Part 3 looks at the more advanced features of the language, from DSLs and code generation to extending the syntax.

This edition is fully updated with all the new features of Elixir 1.6, with a new chapter on structuring OTP applications, and new sections on the debugger, code formatter, Distillery, and protocols.

What You Need:

You'll need a computer, a little experience with another high-levellanguage, and a sense of adventure. No functional programmingexperience is needed.

Table of Contents

  1.  Foreword
  2.  A Vain Attempt at a Justification, Take Two
    1. Acknowledgments
  3. 1. Take the Red Pill
    1. Programming Should Be About Transforming Data
    2. Installing Elixir
    3. Running Elixir
    4. Suggestions for Reading the Book
    5. Exercises
    6. Think Different(ly)
  4. Part I. Conventional Programming
    1. 2. Pattern Matching
      1. Assignment: I Do Not Think It Means What You Think It Means.I Do Not Think It Means What You Think It Means.
      2. More Complex Matches
      3. Ignoring a Value with _ (Underscore)
      4. Variables Bind Once (per Match)
      5. Another Way of Looking at the Equals Sign
    2. 3. Immutability
      1. You Already Have (Some) Immutable Data
      2. Immutable Data Is Known Data
      3. Performance Implications of Immutability
      4. Coding with Immutable Data
    3. 4. Elixir Basics
      1. Built-in Types
      2. Value Types
      3. System Types
      4. Collection Types
      5. Maps
      6. Binaries
      7. Dates and Times
      8. Names, Source Files, Conventions, Operators, and So On
      9. Variable Scope
      10. End of the Basics
    4. 5. Anonymous Functions
      1. Functions and Pattern Matching
      2. One Function, Multiple Bodies
      3. Functions Can Return Functions
      4. Passing Functions as Arguments
      5. Functions Are the Core
    5. 6. Modules and Named Functions
      1. Compiling a Module
      2. The Function’s Body Is a Block
      3. Function Calls and Pattern Matching
      4. Guard Clauses
      5. Default Parameters
      6. Private Functions
      7. The Amazing Pipe Operator: |>
      8. Modules
      9. Module Attributes
      10. Module Names: Elixir, Erlang, and Atoms
      11. Calling a Function in an Erlang Library
      12. Finding Libraries
    6. 7. Lists and Recursion
      1. Heads and Tails
      2. Using Head and Tail to Process a List
      3. Using Head and Tail to Build a List
      4. Creating a Map Function
      5. Reducing a List to a Single Value
      6. More Complex List Patterns
      7. The List Module in Action
      8. Get Friendly with Lists
    7. 8. Maps, Keyword Lists, Sets, and Structs
      1. How to Choose Between Maps, Structs, and Keyword Lists
      2. Keyword Lists
      3. Maps
      4. Pattern Matching and Updating Maps
      5. Updating a Map
      6. Structs
      7. Nested Dictionary Structures
      8. Sets
      9. With Great Power Comes Great Temptation
    8. 9. An Aside—What Are Types?An Aside—What Are Types?
    9. 10. Processing Collections—Enum and StreamProcessing Collections—Enum and Stream
      1. Enum—Processing Collections
      2. Streams—Lazy Enumerables
      3. The Collectable Protocol
      4. Comprehensions
      5. Moving Past Divinity
    10. 11. Strings and Binaries
      1. String Literals
      2. The Name “strings”
      3. Single-Quoted Strings—Lists of Character Codes
      4. Binaries
      5. Double-Quoted Strings Are Binaries
      6. Binaries and Pattern Matching
      7. Familiar Yet Strange
    11. 12. Control Flow
      1. if and unless
      2. cond
      3. case
      4. Raising Exceptions
      5. Designing with Exceptions
      6. Doing More with Less
    12. 13. Organizing a Project
      1. The Project: Fetch Issues from GitHub
      2. Step 1: Use Mix to Create Our New Project
      3. Transformation: Parse the Command Line
      4. Write Some Basic Tests
      5. Refactor: Big Function Alert
      6. Transformation: Fetch from GitHub
      7. Step 2: Use Libraries
      8. Transformation: Convert Response
      9. Transformation: Sort Data
      10. Transformation: Take First n Items
      11. Transformation: Format the Table
      12. Step 3: Make a Command-Line Executable
      13. Step 4: Add Some Logging
      14. Step 5: Create Project Documentation
      15. Coding by Transforming Data
    13. 14. Tooling
      1. Debugging with IEx
      2. Testing
      3. Code Dependencies
      4. Server Monitoring
      5. Source-Code Formatting
      6. Inevitably, There’s More
  5. Part II. Concurrent Programming
    1. 15. Working with Multiple Processes
      1. A Simple Process
      2. Process Overhead
      3. When Processes Die
      4. Parallel Map—The “Hello, World” of Erlang
      5. A Fibonacci Server
      6. Agents—A Teaser
      7. Thinking in Processes
    2. 16. Nodes—The Key to Distributing ServicesNodes—The Key to Distributing Services
      1. Naming Nodes
      2. Naming Your Processes
      3. Input, Output, PIDs, and Nodes
      4. Nodes Are the Basis of Distribution
    3. 17. OTP: Servers
      1. Some OTP Definitions
      2. An OTP Server
      3. GenServer Callbacks
      4. Naming a Process
      5. Tidying Up the Interface
      6. Making Our Server into a Component
    4. 18. OTP: Supervisors
      1. Supervisors and Workers
      2. Worker Restart Options
      3. Supervisors Are the Heart of Reliability
    5. 19. A More Complex Example
      1. Introduction to Duper
      2. The Duper Application
      3. But Does It Work?
      4. Planning Your Elixir Application
      5. Next Up
    6. 20. OTP: Applications
      1. This Is Not Your Father’s Application
      2. The Application Specification File
      3. Turning Our Sequence Program into an OTP Application
      4. Supervision Is the Basis of Reliability
      5. Releasing Your Code
      6. Distillery—The Elixir Release Manager
      7. OTP Is Big—Unbelievably Big
    7. 21. Tasks and Agents
      1. Tasks
      2. Agents
      3. A Bigger Example
      4. Agents and Tasks, or GenServer?
  6. Part III. More Advanced Elixir
    1. 22. Macros and Code Evaluation
      1. Implementing an if Statement
      2. Macros Inject Code
      3. Using the Representation as Code
      4. Using Bindings to Inject Values
      5. Macros Are Hygienic
      6. Other Ways to Run Code Fragments
      7. Macros and Operators
      8. Digging Deeper
      9. Digging Ridiculously Deep
    2. 23. Linking Modules: Behavio(u)rs and use
      1. Behaviours
      2. use and __using__
      3. Putting It Together—Tracing Method Calls
      4. Use use
    3. 24. Protocols—Polymorphic FunctionsProtocols—Polymorphic Functions
      1. Defining a Protocol
      2. Implementing a Protocol
      3. The Available Types
      4. Protocols and Structs
      5. Built-in Protocols
      6. Protocols Are Polymorphism
    4. 25. More Cool Stuff
      1. Writing Your Own Sigils
      2. Multi-app Umbrella Projects
      3. But Wait! There’s More!
  7. A1. Exceptions: raise and try, catch and throw
    1. Raising an Exception
    2. catch, exit, and throw
    3. Defining Your Own Exceptions
    4. Now Ignore This Appendix
  8. A2. Type Specifications and Type Checking
    1. When Specifications Are Used
    2. Specifying a Type
    3. Defining New Types
    4. Specs for Functions and Callbacks
    5. Using Dialyzer
  9.  Bibliography