Learn You Some Erlang for Great Good!

Book description

Erlang is the language of choice for programmers who want to write robust, concurrent applications, but its strange syntax and functional design can intimidate the uninitiated. Luckily, there's a new weapon in the battle against Erlang-phobia: Learn You Some Erlang for Great Good!

Erlang maestro Fred Hebert starts slow and eases you into the basics: You'll learn about Erlang's unorthodox syntax, its data structures, its type system (or lack thereof!), and basic functional programming techniques. Once you've wrapped your head around the simple stuff, you'll tackle the real meat-and-potatoes of the language: concurrency, distributed computing, hot code loading, and all the other dark magic that makes Erlang such a hot topic among today's savvy developers.

As you dive into Erlang's functional fantasy world, you'll learn about:

  • Testing your applications with EUnit and Common Test
  • Building and releasing your applications with the OTP framework
  • Passing messages, raising errors, and starting/stopping processes over many nodes
  • Storing and retrieving data using Mnesia and ETS
  • Network programming with TCP, UDP, and the inet module
  • The simple joys and potential pitfalls of writing distributed, concurrent applications

Packed with lighthearted illustrations and just the right mix of offbeat and practical example programs, Learn You Some Erlang for Great Good! is the perfect entry point into the sometimes-crazy, always-thrilling world of Erlang.

Publisher resources

View/Submit Errata

Table of contents

  1. Learn You Some Erlang For Great Good!
  2. Foreword
  3. Preface
    1. To the Foreigner
    2. To the Erlang Regular
    3. To the Person Who Has Read This Online
  4. Acknowledgments
  5. Introduction
    1. So What’s Erlang?
    2. Don’t Drink Too Much Kool-Aid
    3. What You Need to Dive In
    4. Where to Get Help
  6. 1. Starting Out
    1. Using the Erlang Shell
      1. Entering Shell Commands
      2. Exiting the Shell
    2. Some Erlang Basics
      1. Numbers
      2. Invariable Variables
      3. Atoms
      4. Boolean Algebra and Comparison Operators
      5. Tuples
      6. Lists
      7. List Comprehensions
    3. Working with Binary Data
      1. Bit Syntax
      2. Bitwise Binary Operations
      3. Binary Strings
      4. Binary Comprehensions
  7. 2. Modules
    1. What Are Modules?
    2. Creating Modules
    3. Compiling Code
      1. Compiler Options
    4. Defining Macros
    5. More About Modules
      1. Metadata
      2. Circular Dependencies
  8. 3. Syntax in Functions
    1. Pattern Matching
      1. Fancier Patterns
      2. Variables in a Bind
    2. Guards, Guards!
    3. What the If ?!
    4. In case ... of
    5. Which Should We Use?
  9. 4. Types (or Lack Thereof)
    1. Dynamite-Strong Typing
    2. Type Conversions
    3. To Guard a Data Type
    4. For Type Junkies
  10. 5. Hello Recursion!
    1. How Recursion Works
      1. Length of a List
      2. Length of a Tail Recursion
    2. More Recursive Functions
      1. A Duplicate Function
      2. A Reverse Function
      3. A Sublist Function
      4. A Zip Function
      5. Quick, Sort!
    3. More Than Lists
    4. Thinking Recursively
  11. 6. Higher-Order Functions
    1. Let’s Get Functional
    2. Anonymous Functions
      1. More Anonymous Function Power
      2. Function Scope and Closures
    3. Maps, Filters, Folds, and More
      1. Filters
      2. Fold Everything
      3. More Abstractions
  12. 7. Errors and Exceptions
    1. A Compilation of Errors
      1. Compile-Time Errors
      2. No, YOUR Logic Is Wrong!
      3. Runtime Errors
        1. Function Clause Errors
        2. Case Clause Errors
        3. If Clause Errors
        4. Bad Match Errors
        5. Bad Argument Errors
        6. Undefined Function Errors
        7. Bad Arithmetic Errors
        8. Bad Function Errors
        9. Bad Arity Errors
        10. System Limit Errors
    2. Raising Exceptions
      1. Error Exceptions
        1. When Not to Use Errors
        2. Custom Errors
      2. Exit Exceptions
      3. Throw Exceptions
    3. Dealing with Exceptions
      1. Handling Different Types of Exceptions
      2. After the Catch
      3. Trying Multiple Expressions
      4. Wait, There’s More!
    4. Try a try in a Tree
  13. 8. Functionally Solving Problems
    1. Reverse Polish Notation Calculator
      1. How RPN Calculators Work
      2. Creating an RPN Calculator
      3. Testing the Code
    2. Heathrow to London
      1. Solving the Problem Recursively
      2. Writing the Code
      3. Running the Program Without the Erlang Shell
  14. 9. A Short Visit to Common Data Structures
    1. Records
      1. Defining Records
      2. Reading Values from Records
      3. Updating Records
      4. Sharing Records
    2. Key/Value Stores
      1. Stores for Small Amounts of Data
        1. Proplists
        2. Orddicts
      2. Larger Dictionaries: Dicts and GB Trees
    3. A Set of Sets
    4. Directed Graphs
    5. Queues
    6. End of the Short Visit
  15. 10. The Hitchhiker’s Guide to Concurrency
    1. Don’t Panic
    2. Concurrency Concepts
      1. Scalability
      2. Fault Tolerance
      3. Concurrency Implementation
    3. Not Entirely Unlike Linear Scaling
    4. So Long and Thanks for All the Fish!
      1. Spawning Processes
      2. Sending Messages
      3. Receiving Messages
  16. 11. More on Multiprocessing
    1. State Your State
    2. We Love Messages, But We Keep Them Secret
    3. Time Out
    4. Selective Receives
      1. The Pitfalls of Selective Receives
      2. More Mailbox Pitfalls
  17. 12. Errors and Processes
    1. Links
      1. It’s a Trap!
      2. Old Exceptions, New Concepts
        1. Exceptions and Traps
        2. exit/2 Changes Everything
        3. Killing Me (Not So) Softly
    2. Monitors
    3. Naming Processes
  18. 13. Designing a Concurrent Application
    1. Understanding the Problem
    2. Defining the Protocol
    3. Lay Them Foundations
    4. An Event Module
      1. Events and Loops
      2. Adding An Interface
    5. The Event Server
      1. Handling Messages
      2. Hot Code Loving
      3. I Said, Hide Your Messages
    6. A Test Drive
    7. Adding Supervision
    8. Namespaces (or Lack Thereof)
  19. 14. An Introduction to OTP
    1. The Common Process, Abstracted
    2. The Basic Server
      1. Introducing the Kitty Server
      2. Generalizing Calls
      3. Generalizing the Server Loop
      4. Starter Functions
      5. Generalizing Kitty Server
    3. Specific vs. Generic
    4. Callback to the Future
      1. The init Function
      2. The handle_call Function
      3. The handle_cast Function
      4. The handle_info Function
      5. The terminate Function
      6. The code_change Function
    5. .BEAM Me Up, Scotty!
  20. 15. Rage Against the Finite-State Machines
    1. What Is a Finite-State Machine?
    2. Generic Finite-State Machines
      1. The init Function
      2. The StateName Function
      3. The handle_event Function
      4. The handle_sync_event Function
      5. The code_change and terminate Functions
    3. A Trading System Specification
      1. Show Me Your Moves
      2. Defining the State Diagrams and Transitions
    4. Game Trading Between Two Players
      1. The Public Interface
      2. FSM-to-FSM Functions
      3. The gen_fsm Callbacks
    5. That Was Really Something
    6. Fit for the Real World?
  21. 16. Event Handlers
    1. Handle This! *pumps shotgun*
    2. Generic Event Handlers
      1. The init and terminate Functions
      2. The handle_event Function
      3. The handle_call Function
      4. The handle_info Function
      5. The code_change Function
    3. It’s Curling Time!
      1. The Scoreboard
      2. Game Events
      3. Alert the Press!
  22. 17. Who Supervises the Supervisors?
    1. Supervisor Concepts
    2. Using Supervisors
      1. Restart Strategies
        1. one_for_one
        2. one_for_all
        3. rest_for_one
        4. simple_one_for_one
      2. Restart Limits
      3. Child Specifications
        1. ChildId
        2. StartFunc
        3. Restart
        4. Shutdown
        5. Type
        6. Modules
    3. Band Practice
      1. Musicians
      2. Band Supervisor
    4. Dynamic Supervision
      1. Using Standard Supervisors Dynamically
      2. Using a simple_one_for_one Supervisor
  23. 18. Building an Application
    1. A Pool of Processes
      1. The Onion Layer Theory
      2. A Pool’s Tree
    2. Implementing the Supervisors
    3. Working on the Workers
    4. Writing a Worker
    5. Run Pool Run
    6. Cleaning the Pool
  24. 19. Building Applications the OTP Way
    1. My Other Car Is a Pool
    2. The Application Resource File
    3. Converting the Pool
    4. The Application Behavior
    5. From Chaos to Application
    6. Library Applications
  25. 20. The Count of Applications
    1. From OTP Application to Real Application
      1. The Application File
      2. The Application Callback Module and Supervisor
      3. The Dispatcher
        1. Returning Results through CPS
        2. Dispatching and Receiving
      4. The Counter
    2. Run App Run
    3. Included Applications
    4. Complex Terminations
  26. 21. Release Is the Word
    1. Fixing the Leaky Pipes
      1. Terminating the VM
      2. Updating the Application Files
      3. Compiling the Applications
    2. Releases with systools
      1. Creating a Boot File
      2. Packaging the Release
    3. Releases with Reltool
      1. Reltool Options
        1. Release-Only Options
        2. Release-wide and Application-wide Options
        3. Module-Specific Options
        4. All-levels Options
        5. That’s Dense
    4. Reltool Recipes
        1. Development Versions
        2. Importing or Exporting Only Part of a Library
        3. Smaller Apps for Programmers with Big Hearts
    5. Released from Releases
  27. 22. Leveling Up in the Process Quest
    1. The Hiccups of Appups and Relups
    2. The Ninth Circle of Erl
    3. Process Quest
      1. The regis-1.0.0 Application
      2. The processquest-1.0.0 Application
      3. The sockserv-1.0.0 Application
      4. The Release
    4. Making Process Quest Better
      1. Updating code_change Functions
      2. Adding Appup Files
      3. Upgrading the Release
    5. Relup Review
  28. 23. Buckets of Sockets
    1. IO Lists
    2. UDP and TCP: Bro-tocols
      1. UDP Sockets
      2. TCP Sockets
    3. More Control with Inet
    4. Sockserv, Revisited
    5. Where to Go from Here?
  29. 24. EUnited Nations Council
    1. EUnit—What’s an EUnit?
    2. Test Generators
    3. Fixtures
      1. More Test Control
      2. Test Documentation
    4. Testing Regis
    5. He Who Knits EUnits
  30. 25. Bears, ETS, Beets: In-Memory NoSQL for Free!
    1. Why ETS
    2. The Concepts of ETS
    3. ETS Phone Home
      1. Creating and Deleting Tables
      2. Inserting and Looking Up Data
    4. Meeting Your Match
    5. You Have Been Selected
    6. DETS
    7. A Little Less Conversation, a Little More Action, Please
      1. The Interface
      2. Implementation Details
  31. 26. Distribunomicon
    1. This Is My Boomstick
    2. Fallacies of Distributed Computing
      1. The Network Is Reliable
      2. There Is No Latency
      3. Bandwidth Is Infinite
      4. The Network Is Secure
      5. Topology Doesn’t Change
      6. There Is Only One Administrator
      7. Transport Cost Is Zero
      8. The Network Is Homogeneous
      9. Fallacies in a Nutshell
    3. Dead or Dead-Alive
    4. My Other Cap Is a Theorem
      1. Consistency
      2. Availability
      3. Partition Tolerance
      4. Zombie Survivors and CAP
    5. Setting Up an Erlang Cluster
      1. Through the Desert on a Node with No Name
      2. Connecting Nodes
      3. More Tools
    6. Cookies
    7. Remote Shells
    8. Hidden Nodes
    9. The Walls Are Made of Fire, and the Goggles Do Nothing
    10. The Calls from Beyond
      1. The net_kernel Module
      2. The global Module
      3. The rpc Module
    11. Burying the Distribunomicon
  32. 27. Distributed OTP Applications
    1. Adding More to OTP
    2. Taking and Failing Over
    3. The Magic 8 Ball
      1. Building the Application
        1. The Supervisor Module
        2. The Server Module
      2. Making the Application Distributed
  33. 28. Common Test for Uncommon Tests
    1. What Is Common Test?
    2. Common Test Structure
    3. Creating a Simple Test Suite
      1. Running the Tests
    4. Testing with State
    5. Test Groups
      1. Defining Test Groups
      2. Test Group Properties
      3. The Meeting Room
    6. Test Suites Redux
    7. Test Specifications
      1. Specification File Contents
      2. Creating a Spec File
      3. Running Tests with a Spec File
    8. Large-Scale Testing
      1. Creating a Distributed Spec File
      2. Running Distributed Tests
    9. Integrating EUnit Within Common Test
    10. Is There More?
  34. 29. Mnesia and the Art of Remembering
    1. What’s Mnesia?
    2. What Should the Store Store?
      1. The Data to Store
      2. Table Structure
    3. From Record to Table
    4. Of Mnesia Schemas and Tables
    5. Creating Tables
      1. Installing the Database
      2. Starting the Application
    6. Access and Context
    7. Reads, Writes, and More
    8. Implementing the First Requests
      1. A Test for Adding Services
      2. Tests for Lookups
      3. Accounts and New Needs
    9. Meet the Boss
    10. Deleting Stuff, Demonstrated
    11. Query List Comprehensions
    12. Remember Mnesia
  35. 30. Type Specifications and Dialyzer
    1. PLTs Are the Best Sandwiches
    2. Success Typing
    3. Type Inference and Discrepancies
    4. Typing About Types of Types
      1. Singleton Types
      2. Union and Built-in Types
      3. Defining Types
      4. Types for Records
    5. Typing Functions
    6. Typing Practice
    7. Exporting Types
    8. Typed Behaviors
    9. Polymorphic Types
      1. We Bought a Zoo
      2. Some Cautions
    10. You’re My Type
    11. That’s All, Folks
  36. A. Afterword
    1. Other Erlang Applications
    2. Community Libraries
    3. Your Ideas Are Intriguing to Me and I Wish to Subscribe to Your Newsletter
    4. Is That It?
  37. B. On Erlang’s Syntax
    1. The Template
    2. The English Sentence
    3. And, Or, Done.
    4. In Conclusion
  38. Index
  39. About the Author
  40. C. Updates
  41. Copyright

Product information

  • Title: Learn You Some Erlang for Great Good!
  • Author(s): Fred Hebert
  • Release date: January 2013
  • Publisher(s): No Starch Press
  • ISBN: 9781593274351