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.
Author(s): Fred Hebert
Publisher: No Starch Press
Year: 2013
Language: English
Pages: 628
About the Author......Page 19
Foreword......Page 21
To the Foreigner......Page 23
To the Person Who Has Read This Online......Page 24
Acknowledgments......Page 25
Introduction......Page 27
So What’s Erlang?......Page 28
Don’t Drink Too Much Kool-Aid......Page 30
What You Need to Dive In......Page 31
Where to Get Help......Page 32
Using the Erlang Shell......Page 33
Exiting the Shell......Page 34
Numbers......Page 36
Invariable Variables......Page 37
Atoms......Page 38
Boolean Algebra and Comparison Operators......Page 40
Tuples......Page 42
Lists......Page 44
List Comprehensions......Page 47
Bit Syntax......Page 49
Bitwise Binary Operations......Page 52
Binary Strings......Page 53
Binary Comprehensions......Page 54
What Are Modules?......Page 57
Creating Modules......Page 58
Compiling Code......Page 62
Compiler Options......Page 63
Defining Macros......Page 64
Metadata......Page 66
Circular Dependencies......Page 67
Pattern Matching......Page 69
Fancier Patterns......Page 71
Variables in a Bind......Page 72
Guards, Guards!......Page 74
What the If ?!......Page 75
In case ... of......Page 78
Which Should We Use?......Page 80
Dynamite-Strong Typing......Page 81
Type Conversions......Page 83
To Guard a Data Type......Page 84
For Type Junkies......Page 86
5: Hello Recursion!......Page 87
How Recursion Works......Page 88
Length of a List......Page 89
Length of a Tail Recursion......Page 90
A Reverse Function......Page 92
A Sublist Function......Page 94
A Zip Function......Page 95
Quick, Sort!......Page 96
More Than Lists......Page 98
Thinking Recursively......Page 101
Let’s Get Functional......Page 103
Anonymous Functions......Page 105
More Anonymous Function Power......Page 106
Function Scope and Closures......Page 107
Filters......Page 109
Fold Everything......Page 110
More Abstractions......Page 112
7: Errors and Exceptions......Page 113
Compile-Time Errors......Page 114
No, YOUR Logic Is Wrong!......Page 115
Runtime Errors......Page 116
Error Exceptions......Page 119
Exit Exceptions......Page 120
Throw Exceptions......Page 121
Handling Different Types of Exceptions......Page 122
Trying Multiple Expressions......Page 125
Wait, There’s More!......Page 126
Try a try in a Tree......Page 129
8: Functionally Solving Problems......Page 131
How RPN Calculators Work......Page 132
Creating an RPN Calculator......Page 133
Testing the Code......Page 136
Heathrow to London......Page 137
Solving the Problem Recursively......Page 138
Writing the Code......Page 140
Running the Program Without the Erlang Shell......Page 144
9: A Short Visit to Common
Data Structures......Page 147
Defining Records......Page 148
Reading Values from Records......Page 149
Updating Records......Page 151
Sharing Records......Page 152
Stores for Small Amounts of Data......Page 153
Larger Dictionaries: Dicts and GB Trees......Page 154
A Set of Sets......Page 156
Directed Graphs......Page 157
Queues......Page 158
End of the Short Visit......Page 159
10: The Hitchhiker’s Guide to Concurrency......Page 161
Don’t Panic......Page 162
Scalability......Page 163
Fault Tolerance......Page 164
Not Entirely Unlike Linear Scaling......Page 166
Spawning Processes......Page 168
Sending Messages......Page 170
Receiving Messages......Page 171
11: More on Multiprocessing......Page 175
State Your State......Page 176
We Love Messages, But We Keep Them Secret......Page 178
Time Out......Page 179
The Pitfalls of Selective Receives......Page 182
More Mailbox Pitfalls......Page 185
12: Errors and Processes......Page 187
Links......Page 188
It’s a Trap!......Page 190
Old Exceptions, New Concepts......Page 191
Monitors......Page 194
Naming Processes......Page 196
13: Designing a Concurrent Application......Page 201
Understanding the Problem......Page 202
Defining the Protocol......Page 204
Lay Them Foundations......Page 206
Events and Loops......Page 207
Adding An Interface......Page 210
The Event Server......Page 212
Handling Messages......Page 214
Hot Code Loving......Page 217
I Said, Hide Your Messages......Page 218
A Test Drive......Page 220
Adding Supervision......Page 221
Namespaces (or Lack Thereof)......Page 222
14: An Introduction to OTP......Page 225
The Common Process, Abstracted......Page 226
Introducing the Kitty Server......Page 227
Generalizing Calls......Page 229
Generalizing the Server Loop......Page 230
Starter Functions......Page 232
Generalizing Kitty Server......Page 233
Specific vs. Generic......Page 235
The init Function......Page 236
The handle_call Function......Page 237
The terminate Function......Page 238
.BEAM Me Up, Scotty!......Page 239
15: Rage Against the
Finite-State Machines......Page 245
What Is a Finite-State Machine?......Page 246
The init Function......Page 249
The StateName Function......Page 250
A Trading System Specification......Page 251
Show Me Your Moves......Page 252
Defining the State Diagrams and Transitions......Page 253
The Public Interface......Page 259
FSM-to-FSM Functions......Page 261
The gen_fsm Callbacks......Page 262
That Was Really Something......Page 271
Fit for the Real World?......Page 272
16: Event Handlers......Page 273
Handle This! *pumps shotgun*......Page 274
Generic Event Handlers......Page 275
The handle_event Function......Page 276
The code_change Function......Page 277
The Scoreboard......Page 278
Game Events......Page 279
Alert the Press!......Page 283
17: Who Supervises the Supervisors?......Page 289
Supervisor Concepts......Page 290
Restart Strategies......Page 292
Child Specifications......Page 294
Musicians......Page 297
Band Supervisor......Page 300
Using Standard Supervisors Dynamically......Page 303
Using a simple_one_for_one Supervisor......Page 305
18: Building an Application......Page 307
A Pool of Processes......Page 308
The Onion Layer Theory......Page 309
A Pool’s Tree......Page 310
Implementing the Supervisors......Page 312
Working on the Workers......Page 316
Writing a Worker......Page 322
Run Pool Run......Page 324
Cleaning the Pool......Page 327
19:
Building Applications
the OTP Way......Page 329
My Other Car Is a Pool......Page 330
The Application Resource File......Page 331
Converting the Pool......Page 333
The Application Behavior......Page 335
From Chaos to Application......Page 336
Library Applications......Page 340
20: The Count of Applications......Page 341
From OTP Application to Real Application......Page 342
The Application File......Page 343
The Application Callback Module and Supervisor......Page 344
The Dispatcher......Page 345
The Counter......Page 355
Run App Run......Page 357
Complex Terminations......Page 359
21: Release Is the Word......Page 361
Updating the Application Files......Page 362
Compiling the Applications......Page 363
Releases with systools......Page 364
Creating a Boot File......Page 365
Packaging the Release......Page 366
Releases with Reltool......Page 367
Reltool Options......Page 371
Reltool Recipes......Page 374
Released from Releases......Page 378
22: Leveling Up in the Process Quest......Page 379
The Hiccups of Appups and Relups......Page 380
The Ninth Circle of Erl......Page 382
Process Quest......Page 383
The regis-1.0.0 Application......Page 384
The processquest-1.0.0 Application......Page 385
The Release......Page 386
Updating code_change Functions......Page 389
Adding Appup Files......Page 391
Upgrading the Release......Page 395
Relup Review......Page 398
IO Lists......Page 401
UDP and TCP: Bro-tocols......Page 403
UDP Sockets......Page 405
TCP Sockets......Page 407
More Control with Inet......Page 410
Sockserv, Revisited......Page 413
Where to Go from Here?......Page 422
24: EUnited Nations Council......Page 423
EUnit—What’s an EUnit?......Page 424
Test Generators......Page 428
Fixtures......Page 430
More Test Control......Page 432
Test Documentation......Page 433
Testing Regis......Page 434
He Who Knits EUnits......Page 443
25: Bears, ETS, Beets:
In-Memory NoSQL for Free!......Page 445
Why ETS......Page 446
The Concepts of ETS......Page 447
Creating and Deleting Tables......Page 449
Inserting and Looking Up Data......Page 451
Meeting Your Match......Page 453
You Have Been Selected......Page 454
DETS......Page 459
The Interface......Page 460
Implementation Details......Page 461
26: Distribunomicon......Page 467
This Is My Boomstick......Page 468
The Network Is Reliable......Page 471
Bandwidth Is Infinite......Page 472
The Network Is Secure......Page 473
Topology Doesn’t Change......Page 474
Transport Cost Is Zero......Page 475
The Network Is Homogeneous......Page 476
Dead or Dead-Alive......Page 477
Availability......Page 479
Zombie Survivors and CAP......Page 480
Through the Desert on a Node with No Name......Page 484
Connecting Nodes......Page 485
More Tools......Page 486
Cookies......Page 488
Remote Shells......Page 490
Hidden Nodes......Page 491
The Walls Are Made of Fire, and the Goggles Do Nothing......Page 492
The global Module......Page 493
The rpc Module......Page 495
Burying the Distribunomicon......Page 497
27: Distributed OTP Applications......Page 499
Adding More to OTP......Page 500
Taking and Failing Over......Page 501
The Magic 8 Ball......Page 502
Building the Application......Page 503
Making the Application Distributed......Page 506
What Is Common Test?......Page 511
Common Test Structure......Page 512
Creating a Simple Test Suite......Page 514
Running the Tests......Page 515
Testing with State......Page 517
Test Groups......Page 519
Defining Test Groups......Page 520
Test Group Properties......Page 521
The Meeting Room......Page 522
Test Suites Redux......Page 526
Specification File Contents......Page 527
Running Tests with a Spec File......Page 529
Large-Scale Testing......Page 530
Creating a Distributed Spec File......Page 532
Running Distributed Tests......Page 533
Integrating EUnit Within Common Test......Page 534
Is There More?......Page 535
29: Mnesia and the Art of Remembering......Page 537
What’s Mnesia?......Page 538
The Data to Store......Page 539
Table Structure......Page 540
From Record to Table......Page 541
Of Mnesia Schemas and Tables......Page 542
Installing the Database......Page 545
Starting the Application......Page 548
Access and Context......Page 549
Reads, Writes, and More......Page 550
A Test for Adding Services......Page 552
Tests for Lookups......Page 555
Accounts and New Needs......Page 558
Meet the Boss......Page 560
Deleting Stuff, Demonstrated......Page 562
Query List Comprehensions......Page 565
Remember Mnesia......Page 567
PLTs Are the Best Sandwiches......Page 569
Success Typing......Page 571
Type Inference and Discrepancies......Page 573
Singleton Types......Page 576
Union and Built-in Types......Page 577
Defining Types......Page 580
Types for Records......Page 581
Typing Functions......Page 582
Typing Practice......Page 586
Exporting Types......Page 590
Typed Behaviors......Page 592
Polymorphic Types......Page 593
We Bought a Zoo......Page 594
Some Cautions......Page 596
That’s All, Folks......Page 598
Afterword......Page 599
Other Erlang Applications......Page 600
Community Libraries......Page 601
Is That It?......Page 602
The Template......Page 603
The English Sentence......Page 605
In Conclusion......Page 606
Index......Page 607