You are previewing Programming Erlang, 2nd Edition.
O'Reilly logo
Programming Erlang, 2nd Edition

Book Description

A multi-user game, web site, cloud application, or networked database can have thousands of users all interacting at the same time. You need a powerful, industrial-strength tool to handle the really hard problems inherent in parallel, concurrent environments. You need Erlang. In this second edition of the bestselling Programming Erlang, you'll learn how to write parallel programs that scale effortlessly on multicore systems.

Using Erlang, you'll be surprised at how easy it becomes to deal with parallel problems, and how much faster and more efficiently your programs run. That's because Erlang uses sets of parallel processes-not a single sequential process, as found in most programming languages.

Joe Armstrong, creator of Erlang, introduces this powerful language in small steps, giving you a complete overview of Erlang and how to use it in common scenarios. You'll start with sequential programming, move to parallel programming and handling errors in parallel programs, and learn to work confidently with distributed programming and the standard Erlang/Open Telecom Platform (OTP) frameworks.

You need no previous knowledge of functional or parallel programming. The chapters are packed with hands-on, real-world tutorial examples and insider tips and advice, and finish with exercises for both beginning and advanced users.

The second edition has been extensively rewritten. New to this edition are seven chapters covering the latest Erlang features: maps, the type system and the Dialyzer, WebSockets, programming idioms, and a new stand-alone execution environment. You'll write programs that dynamically detect and correct errors, and that can be upgraded without stopping the system. There's also coverage of rebar (the de facto Erlang build system), and information on how to share and use Erlang projects on github, illustrated with examples from cowboy and bitcask.

Erlang will change your view of the world, and of how you program.

Table of Contents

  1. Programming Erlang, Second Edition
  2. For the Best Reading Experience...
  3. Table of Contents
  4. Early Praise for Programming Erlang, Second Edition
  5. Introduction
    1. What’s This Book About?
    2. Who Is This Book For?
    3. New in This Edition
    4. Road Map
    5. The Code in This Book
    6. Help! It Doesn’t Work
    7. Acknowledgments
  6. Part 1: Why Erlang?
  7. Chapter 1: Introducing Concurrency
    1. Modeling Concurrency
    2. Benefits of Concurrency
    3. Concurrent Programs and Parallel Computers
    4. Sequential vs. Concurrent Programming Languages
  8. Chapter 2: A Whirlwind Tour of Erlang
    1. The Shell
    2. Processes, Modules, and Compilation
    3. Hello, Concurrency
  9. Part 2: Sequential Programming
  10. Chapter 3: Basic Concepts
    1. Starting and Stopping the Erlang Shell
    2. Simple Integer Arithmetic
    3. Variables
    4. Floating-Point Numbers
    5. Atoms
    6. Tuples
    7. Lists
    8. Strings
    9. Pattern Matching Again
  11. Chapter 4: Modules and Functions
    1. Modules Are Where We Store Code
    2. Back to Shopping
    3. Funs: The Basic Unit of Abstraction
    4. Simple List Processing
    5. List Comprehensions
    6. BIFs
    7. Guards
    8. case and if Expressions
    9. Building Lists in Natural Order
    10. Accumulators
  12. Chapter 5: Records and Maps
    1. When to Use Maps or Records
    2. Naming Tuple Items with Records
    3. Maps: Associative Key-Value Stores
  13. Chapter 6: Error Handling in Sequential Programs
    1. Handling Errors in Sequential Code
    2. Trapping an Exception with try…catch
    3. Trapping an Exception with catch
    4. Programming Style with Exceptions
    5. Stack Traces
    6. Fail Fast and Noisily, Fail Politely
  14. Chapter 7: Binaries and the Bit Syntax
    1. Binaries
    2. The Bit Syntax
    3. Bitstrings: Processing Bit-Level Data
  15. Chapter 8: The Rest of Sequential Erlang
    1. apply
    2. Arithmetic Expressions
    3. Arity
    4. Attributes
    5. Block Expressions
    6. Booleans
    7. Boolean Expressions
    8. Character Set
    10. Dynamic Code Loading
    11. Erlang Preprocessor
    12. Escape Sequences
    13. Expressions and Expression Sequences
    14. Function References
    15. Include Files
    16. List Operations ++ and - -
    17. Macros
    18. Match Operator in Patterns
    19. Numbers
    20. Operator Precedence
    21. The Process Dictionary
    22. References
    23. Short-Circuit Boolean Expressions
    24. Term Comparisons
    25. Tuple Modules
    26. Underscore Variables
  16. Chapter 9: Types
    1. Specifying Data and Function Types
    2. Erlang Type Notation
    3. A Session with the Dialyzer
    4. Type Inference and Success Typing
    5. Limitations of the Type System
  17. Chapter 10: Compiling and Running Your Program
    1. Modifying the Development Environment
    2. Different Ways to Run Your Program
    3. Automating Compilation with Makefiles
    4. When Things Go Wrong
    5. Getting Help
    6. Tweaking the Environment
  18. Part 3: Concurrent and Distributed Programs
  19. Chapter 11: Real-World Concurrency
  20. Chapter 12: Concurrent Programming
    1. The Concurrency Primitives
    2. Introducing Client-Server
    3. Processes Are Cheap
    4. Receive with a Timeout
    5. Selective Receive
    6. Registered Processes
    7. A Word About Tail Recursion
    8. Spawning with MFAs or Funs
  21. Chapter 13: Errors in Concurrent Programs
    1. Error Handling Philosophy
    2. Error Handling Semantics
    3. Creating Links
    4. Groups of Processes That All Die Together
    5. Setting Up a Firewall
    6. Monitors
    7. Error Handling Primitives
    8. Programming for Fault Tolerance
  22. Chapter 14: Distributed Programming
    1. Two Models for Distribution
    2. Writing a Distributed Program
    3. Building the Name Server
    4. Libraries and BIFS for Distributed Programming
    5. The Cookie Protection System
    6. Socket-Based Distribution
  23. Part 4: Programming Libraries and Frameworks
  24. Chapter 15: Interfacing Techniques
    1. How Erlang Communicates with External Programs
    2. Interfacing an External C Program with a Port
    3. Calling a Shell Script from Erlang
    4. Advanced Interfacing Techniques
  25. Chapter 16: Programming with Files
    1. Modules for Manipulating Files
    2. Ways to Read a File
    3. Ways to Write a File
    4. Directory and File Operations
    5. Bits and Pieces
    6. A Find Utility
  26. Chapter 17: Programming with Sockets
    1. Using TCP
    2. Active and Passive Sockets
    3. Error Handling with Sockets
    4. UDP
    5. Broadcasting to Multiple Machines
    6. A SHOUTcast Server
  27. Chapter 18: Browsing with Websockets and Erlang
    1. Creating a Digital Clock
    2. Basic Interaction
    3. An Erlang Shell in the Browser
    4. Creating a Chat Widget
    5. IRC Lite
    6. Graphics in the Browser
    7. The Browser Server Protocol
  28. Chapter 19: Storing Data with ETS and DETS
    1. Types of Table
    2. ETS Table Efficiency Considerations
    3. Creating an ETS Table
    4. Example Programs with ETS
    5. Storing Tuples on Disk
    6. What Haven’t We Talked About?
  29. Chapter 20: Mnesia: The Erlang Database
    1. Creating the Initial Database
    2. Database Queries
    3. Adding and Removing Data in the Database
    4. Mnesia Transactions
    5. Storing Complex Data in Tables
    6. Table Types and Location
    7. The Table Viewer
    8. Digging Deeper
  30. Chapter 21: Profiling, Debugging, and Tracing
    1. Tools for Profiling Erlang Code
    2. Testing Code Coverage
    3. Generating Cross-References
    4. Compiler Diagnostics
    5. Runtime Diagnostics
    6. Debugging Techniques
    7. The Erlang Debugger
    8. Tracing Messages and Process Execution
    9. Frameworks for Testing Erlang Code
  31. Chapter 22: Introducing OTP
    1. The Road to the Generic Server
    2. Getting Started with gen_server
    3. The gen_server Callback Structure
    4. Filling in the gen_server Template
    5. Digging Deeper
  32. Chapter 23: Making a System with OTP
    1. Generic Event Handling
    2. The Error Logger
    3. Alarm Management
    4. The Application Servers
    5. The Supervision Tree
    6. Starting the System
    7. The Application
    8. File System Organization
    9. The Application Monitor
    10. How Did We Make That Prime?
    11. Digging Deeper
  33. Part 5: Building Applications
  34. Chapter 24: Programming Idioms
    1. Maintaining the Erlang View of the World
    2. A Multipurpose Server
    3. Stateful Modules
    4. Adapter Patterns
    5. Intentional Programming
  35. Chapter 25: Third-Party Programs
    1. Making a Shareable Archive and Managing Your Code with Rebar
    2. Integrating External Programs with Our Code
    3. Making a Local Copy of the Dependencies
    4. Building Embedded Web Servers with Cowboy
  36. Chapter 26: Programming Multicore CPUs
    1. Good News for Erlang Programmers
    2. How to Make Programs Run Efficiently on a Multicore CPU
    3. Parallelizing Sequential Code
    4. Small Messages, Big Computations
    5. Parallelizing Computations with mapreduce
  37. Chapter 27: Sherlock’s Last Case
    1. Finding Similarities in Data
    2. A Session with Sherlock
    3. The Importance of Partitioning the Data
    4. Adding Keywords to the Postings
    5. Overview of the Implementation
    6. Exercises
    7. Wrapping Up
  38. Appendix 1: OTP Templates
    1. The Generic Server Template
    2. The Supervisor Template
    3. The Application Template
  39. Appendix 2: A Socket Application
    1. An Example
    2. How lib_chan Works
    3. The lib_chan Code
  40. Appendix 3: A Simple Execution Environment
    1. How Erlang Starts
    2. Running Some Test Programs in SEE
    3. The SEE API
    4. SEE Implementation Details
    5. How Code Gets Loaded in Erlang
    6. You May Be Interested In…