Learn You Some Erlang for Great Good! Erlang for You Some Great Good ! ang for You Some Learn A Beginner’s Guide at Good ! Erlang for ginner’s Great Guide Good! A Beginner’s Guide Fred Hébert Foreword by Joe Armstrong Fred Hébert word by Joe Armstrong San Francisco Fred Hébert Foreword by Joe Armstrong Learn You Some Erlang for Great Good! Copyright © 2013 by Fred Hébert All rights reserved No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher Printed in USA First printing 17 16 15 14 13 ISBN-10: 1-59327-435-1 ISBN-13: 978-1-59327-435-1 Publisher: William Pollock Production Editor: Alison Law Cover Design: Sonia Brown Developmental Editor: Keith Fancher Technical Reviewer: Geoff Cant Copyeditor: Marilyn Smith Compositor: Susan Glinert Stevens Proofreader: Greg Teague For information on book distributors or translations, please contact No Starch Press, Inc directly: No Starch Press, Inc 38 Ringold Street, San Francisco, CA 94103 phone: 415.863.9900; fax: 415.863.9950; info@nostarch.com; www.nostarch.com Library of Congress Cataloging-in-Publication Data A catalog record of this book is available from the Library of Congress No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc Other product and company names mentioned herein may be the trademarks of their respective owners Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark The information in this book is distributed on an “As Is” basis, without warranty While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it Brief Contents About the Author xvii Foreword by Joe Armstrong xix Preface xxi Acknowledgments xxiii Introduction Chapter 1: Starting Out Chapter 2: Modules 31 Chapter 3: Syntax in Functions 43 Chapter 4: Types (or Lack Thereof) 55 Chapter 5: Hello Recursion! 61 Chapter 6: Higher-Order Functions 77 Chapter 7: Errors and Exceptions 87 Chapter 8: Functionally Solving Problems 105 Chapter 9: A Short Visit to Common Data Structures 121 Chapter 10: The Hitchhiker’s Guide to Concurrency 135 Chapter 11: More on Multiprocessing 149 Chapter 12: Errors and Processes 161 Chapter 13: Designing a Concurrent Application 175 Chapter 14: An Introduction to OTP 199 Chapter 15: Rage Against the Finite-State Machines 219 Chapter 16: Event Handlers 247 Chapter 17: Who Supervises the Supervisors? 263 Chapter 18: Building an Application 281 Chapter 19: Building Applications the OTP Way 303 Chapter 20: The Count of Applications 315 Chapter 21: Release Is the Word 335 Chapter 22: Leveling Up in the Process Quest 353 Chapter 23: Buckets of Sockets 375 Chapter 24: EUnited Nations Council 397 Chapter 25: Bears, ETS, Beets: In-Memory NoSQL for Free! 419 Chapter 26: Distribunomicon 441 Chapter 27: Distributed OTP Applications 473 Chapter 28: Common Test for Uncommon Tests 485 Chapter 29: Mnesia and the Art of Remembering 511 Chapter 30: Type Specifications and Dialyzer 543 Afterword 573 Appendix: On Erlang’s Syntax 577 Index 581 vi Brief Contents Conte nt s in De ta il About the Author xvii Foreword by Joe Armstrong xix Preface xxi To the Foreigner xxi To the Erlang Regular xxii To the Person Who Has Read This Online xxii Acknowledgments xxiii Introduction 1 So What’s Erlang? Don’t Drink Too Much Kool-Aid What You Need to Dive In Where to Get Help Starting Out Using the Erlang Shell Entering Shell Commands Exiting the Shell Some Erlang Basics Numbers Invariable Variables Atoms Boolean Algebra and Comparison Operators Tuples Lists List Comprehensions Working with Binary Data Bit Syntax Bitwise Binary Operations Binary Strings Binary Comprehensions 10 10 11 12 14 16 18 21 23 23 26 27 28 Modules 31 What Are Modules? 31 Creating Modules 32 Compiling Code Compiler Options Defining Macros More About Modules Metadata Circular Dependencies Syntax in Functions Pattern Matching Fancier Patterns Variables in a Bind Guards, Guards! What the If ?! In case of Which Should We Use? 43 Types (or Lack Thereof) Dynamite-Strong Typing Type Conversions To Guard a Data Type For Type Junkies How Recursion Works Length of a List Length of a Tail Recursion More Recursive Functions A Duplicate Function A Reverse Function A Sublist Function A Zip Function Quick, Sort! More Than Lists Thinking Recursively Hello Recursion! 55 57 58 60 61 Higher-Order Functions Let’s Get Functional Anonymous Functions More Anonymous Function Power Function Scope and Closures Maps, Filters, Folds, and More Filters Fold Everything More Abstractions Contents in Detail 43 45 46 48 49 52 54 55 viii 36 37 38 40 40 41 62 63 64 66 66 66 68 69 70 72 75 77 77 79 80 81 83 83 84 86 image, of a function, 562 imported functions, 35 improper lists, 21 included applications, 333 inet module active once, 384–386 vs inets application, 384 socket options, 385 inet:setopts function, 385 installing Erlang, 5–6 integers See numbers IO lists, 375–377 is_alive() function, 470 is_process_alive(Pid) function, 411 kernel (OTP application configuration), 482 key/value storage, 127–130, 306, 419 killing a process exit/2 function, 163 kill signal, 167 to establish dependencies, 179 unlinking, 162, 164 Lipovacˇa, Miran, 1, 105 list comprehensions for database queries, 539–540 filters, 23 generator expressions, 22 set theory origins, 21 syntax, 22–23 list generator (