Log In
Or create an account ->
Imperial Library
Home
About
News
Upload
Forum
Help
Login/SignUp
Index
Erlang Programming
SPECIAL OFFER: Upgrade this ebook with O’Reilly
Foreword
Preface
Francesco: Why Erlang?
Simon: Why Erlang?
Who Should Read This Book?
How to Read This Book
Conventions Used in This Book
Using Code Examples
Safari® Books Online
How to Contact Us
Acknowledgments
1. Introduction
Why Should I Use Erlang?
The History of Erlang
Erlang’s Characteristics
High-Level Constructs
Concurrent Processes and Message Passing
Scalable, Safe, and Efficient Concurrency
Soft Real-Time Properties
Robustness
Distributed Computation
Integration and Openness
Erlang and Multicore
Case Studies
The AXD301 ATM Switch
CouchDB
Comparing Erlang to C++
How Should I Use Erlang?
2. Basic Erlang
Integers
The Erlang Shell
Floats
Mathematical Operators
Atoms
Booleans
Tuples
Lists
Characters and Strings
Atoms and Strings
Building and Processing Lists
List Functions and Operations
Term Comparison
Variables
Complex Data Structures
Pattern Matching
Functions
Modules
Compilation and the Erlang Virtual Machine
Module Directives
Exercises
Exercise 2-1: The Shell
A. Erlang expressions
B. Assigning through pattern matching
C. Recursive list definitions
D. Flow of execution through pattern matching
E. Extracting values in composite data types through pattern matching
Exercise 2-2: Modules and Functions
Exercise 2-3: Simple Pattern Matching
3. Sequential Erlang
Conditional Evaluations
The case Construct
Variable Scope
The if Construct
Guards
Built-in Functions
Object Access and Examination
Type Conversion
Process Dictionary
Meta Programming
Process, Port, Distribution, and System Information
Input and Output
Recursion
Tail-Recursive Functions
Tail-Call Recursion Optimization
Two accumulators example
Iterations Versus Recursive Functions
Runtime Errors
Handling Errors
Using try ... catch
Using catch
Library Modules
Documentation
Useful Modules
The Debugger
Exercises
Exercise 3-1: Evaluating Expressions
Exercise 3-2: Creating Lists
Exercise 3-3: Side Effects
Exercise 3-4: Database Handling Using Lists
Exercise 3-5: Manipulating Lists
Exercise 3-6: Sorting Lists
Exercise 3-7: Using Library Modules
Exercise 3-8: Evaluating and Compiling Expressions
Exercise 3-9: Indexing
Exercise 3-10: Text Processing
4. Concurrent Programming
Creating Processes
Message Passing
Receiving Messages
Selective and Nonselective Receives
An Echo Example
Registered Processes
Timeouts
Benchmarking
Process Skeletons
Tail Recursion and Memory Leaks
A Case Study on Concurrency-Oriented Programming
Race Conditions, Deadlocks, and Process Starvation
The Process Manager
Exercises
Exercise 4-1: An Echo Server
Exercise 4-2: The Process Ring
5. Process Design Patterns
Client/Server Models
A Client/Server Example
A Process Pattern Example
Finite State Machines
An FSM Example
A Mutex Semaphore
Event Managers and Handlers
A Generic Event Manager Example
Event Handlers
Exercises
Exercise 5-1: A Database Server
Exercise 5-2: Changing the Frequency Server
Exercise 5-3: Swapping Handlers
Exercise 5-4: Event Statistics
Exercise 5-5: Phone FSM
6. Process Error Handling
Process Links and Exit Signals
Trapping Exits
The monitor BIFs
The exit BIFs
BIFs and Terminology
Propagation Semantics
Robust Systems
Monitoring Clients
A Supervisor Example
Exercises
Exercise 6-1: The Linked Ping Pong Server
Exercise 6-2: A Reliable Mutex Semaphore
Exercise 6-3: A Supervisor Process
7. Records and Macros
Records
Introducing Records
Working with Records
Functions and Pattern Matching over Records
Records in the Shell
Record Implementation
Record BIFs
Macros
Simple Macros
Parameterized Macros
Debugging and Macros
Include Files
Exercises
Exercise 7-1: Extending Records
Exercise 7-2: Record Guards
Exercise 7-3: The db.erl Exercise Revisited
Exercise 7-4: Records and Shapes
Exercise 7-5: Binary Tree Records
Exercise 7-6: Parameterized Macros
Exercise 7-7: Counting Calls
Exercise 7-8: Enumerated Types
Exercise 7-9: Debugging the db.erl Exercise
8. Software Upgrade
Upgrading Modules
Behind the Scenes
Loading Code
The Code Server
Loading modules
Manipulating the code search path
Purging Modules
Upgrading Processes
The .erlang File
Exercise
Exercise 8-1: Software Upgrade During Runtime
9. More Data Types and High-Level Constructs
Functional Programming for Real
Funs and Higher-Order Functions
Functions As Arguments
Writing Down Functions: fun Expressions
Functions As Results
Using Already Defined Functions
Functions and Variables
Predefined, Higher-Order Functions
Lazy Evaluation and Lists
List Comprehensions
A First Example
General List Comprehensions
Multiple Generators
Standard Functions
Binaries and Serialization
Binaries
The Bit Syntax
Sizes
Types
Pattern-Matching Bits
Bitstring Comprehensions
Bit Syntax Example: Decoding TCP Segments
Bitwise Operators
Serialization
References
Exercises
Exercise 9-1: Higher-Order Functions
Exercise 9-2: List Comprehensions
Exercise 9-3: Zip Functions
Exercise 9-4: Existing Higher-Order Functions
Exercise 9-5: Length Specifications in List Comprehensions
Exercise 9-6: Bitstrings
10. ETS and Dets Tables
ETS Tables
Implementations and Trade-offs
Creating Tables
Handling Table Elements
Example: Building an Index, Act I
Traversing Tables
Example: Building an Index, Act II
Extracting Table Information: match
Extracting Table Information: select
Other Operations on Tables
Records and ETS Tables
Visualizing Tables
Dets Tables
A Mobile Subscriber Database Example
The Database Backend Operations
The Database Server
Exercises
Exercise 10-1: Pretty-Printing
Exercise 10-2: Indexing Revisited
Exercise 10-3: ETS Tables for System Logging
11. Distributed Programming in Erlang
Distributed Systems in Erlang
Distributed Computing in Erlang: The Basics
Node Names and Visibility
Communication and Security
Distributing the Erlang code: A warning
Communication and Messages
Node Connections
Hidden nodes
Remote Procedure Calls
The rpc Module
Essential Distributed Programming Modules
The epmd Process
Distributed Erlang Behind Firewalls
Exercises
Exercise 11-1: Distributed Associative Store
Exercise 11-2: System Monitoring
12. OTP Behaviors
Introduction to OTP Behaviors
Generic Servers
Starting Your Server
Passing Messages
Stopping the Server
The Example in Full
Running gen_server
Supervisors
Supervisor Specifications
Child Specifications
Supervisor Example
Dynamic Children
Applications
Directory Structure
The Application Resource File
Starting and Stopping Applications
The Application Monitor
Release Handling
Other Behaviors and Further Reading
Exercises
Exercise 12-1: Database Server Revisited
Exercise 12-2: Supervising the Database Server
Exercise 12-3: The Database Server As an Application
13. Introducing Mnesia
When to Use Mnesia
Configuring Mnesia
Setting Up the Schema
Starting Mnesia
Mnesia Tables
Transactions
Writing
Reading and Deleting
Indexing
Dirty Operations
Partitioned Networks
Further Reading
Exercises
Exercise 13-1: Setting Up Mnesia
Exercise 13-2: Transactions
Exercise 13-3: Dirty Mnesia Operations
14. GUI Programming with wxErlang
wxWidgets
wxErlang: An Erlang Binding for wxWidgets
Objects and Types
Event Handling, Object Identifiers, and Event Types
Putting It All Together
A First Example: MicroBlog
The MiniBlog Example
Obtaining and Running wxErlang
Exercises
Exercise 14-1: Selecting the Blog File
Exercise 14-2: Saving Blog Items Separately
Exercise 14-3: Multiple Blogs in Separate Tabs
Exercise 14-4: Extending the Entries—Rich Text
Exercise 14-5: Tagging Entries
Exercise 14-6: Multiple Users and Comments
Exercise 14-7: Layout and wxErlang Sizers
15. Socket Programming
User Datagram Protocol
Transmission Control Protocol
A TCP Example
The inet Module
Further Reading
Exercises
Exercise 15-1: Snooping an HTTP Request
Exercise 15-2: A Simple HTTP Proxy
Exercise 15-3: Peer to Peer
16. Interfacing Erlang with Other Programming Languages
An Overview of Interworking
Interworking with Java
Nodes and Mailboxes
Representing Erlang Types
Communication
Putting It Together: RPC Revisited
Interaction
The Small Print
Taking It Further
C Nodes
Going Further
Erlang from the Unix Shell: erl_call
Port Programs
Erlang Port Commands
Communicating Data to and from a Port
Library Support for Communication
Working in Ruby: erlectricity
An example using erlectricity
Linked-in Drivers and the FFI
Exercises
Exercise 16-1: C Factorial via a Port
Exercise 16-2: Factorial Server in Another Language
17. Trace BIFs, the dbg Tracer, and Match Specifications
Introduction
The Trace BIFs
Process Trace Flags
Inheritance Flags
Garbage Collection and Timestamps
Tracing Calls with the trace_pattern BIF
The dbg Tracer
Getting Started with dbg
Tracing and Profiling Functions
Tracing Local and Global Function Calls
Distributed Environments
Redirecting the Output
Redirecting to sockets and binary files
Match Specifications: The fun Syntax
Generating Specifications Using fun2ms
Odds and ends with fun2ms
Difference Between ets and dbg Match Specifications
Match Specifications: The Nuts and Bolts
The Head
Conditions
The Specification Body
Saving Match Specifications
Further Reading
Exercises
Exercise 17-1: Measuring Garbage Collection Times
Exercise 17-2: Garbage Collection Using dbg
Exercise 17-3: Tracing ETS Table Entries
Exercise 17-4: Who Is the Culprit?
18. Types and Documentation
Types in Erlang
An Example: Records with Typed Fields
Erlang Type Notation
TypEr: Success Types and Type Inference
Dialyzer: A DIscrepancy AnaLYZer for ERlang Programs
Documentation with EDoc
Documenting usr_db.erl
Module tags
Function tags
Generic tags
Running EDoc
Module pages
Overview page
Types in EDoc
Going Further with EDoc
Exercises
19. EUnit and Test-Driven Development
Test-Driven Development
EUnit
How to Use EUnit
Functional Testing, an Example: Tree Serialization
The EUnit Infrastructure
Assert Macros
Test-Generating Functions
EUnit Test Representation
Testing State-Based Systems
Fixtures: Setup and Cleanup
Testing Concurrent Programs in Erlang
Exercises
Exercise 19-1: Testing Sequential Functions
Exercise 19-2: Testing Concurrent Systems
Exercise 19-3: Software Upgrade
Exercise 19-4: Testing OTP Behaviors
Exercise 19-5: Devising Tests for OTP Behaviors
20. Style and Efficiency
Applications and Modules
Libraries
Dirty Code
Interfaces
Return Values
Internal Data Structures
Processes and Concurrency
Stylistic Conventions
Coding Strategies
Efficiency
Sequential Programming
Lists
Tail Recursion and Non-tail Recursion
Concurrency
And Finally...
A. Using Erlang
Getting Started with Erlang
Installing the System
Running the Erlang Shell
Tools for Erlang
Editors
Other Tools
Where to Learn More
Index
About the Authors
Colophon
SPECIAL OFFER: Upgrade this ebook with O’Reilly
← Prev
Back
Next →
← Prev
Back
Next →