Functional Programming in JavaScript
Luis Atencio
Copyright
For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact
Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email:
orders@manning.com2016 by Manning Publications Co. All rights reserved.
No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps.
Recognizing the importance of preserving what has been written, it is Mannings policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine.
| Manning Publications Co.20 Baldwin RoadPO Box 761Shelter Island, NY 11964 |
Development editor: Marina MichaelsTechnical development editor: Dean IversonReview editor: Aleksandar DragosavljevicProject editor: Tiffany TaylorCopy editor: Tiffany TaylorProofreader: Katie TennantTechnical proofreader: Daniel LambTypesetter: Dennis DalinnikCover designer: Leslie Haimes
ISBN: 9781617292828
Printed in the United States of America
1 2 3 4 5 6 7 8 9 10 EBM 21 20 19 18 17 16
To my wonderful wife, Ana. Thank you for your unconditional support and for being the source of passion and inspiration in my life.
Brief Table of Contents
Table of Contents
Preface
When I was in college and graduate school, my class schedule was focused on object-oriented design as the sole methodology for planning and architecting software systems. And, like many developers, I began my career writing object-oriented code and building entire systems based on this paradigm.
Throughout my development career, Ive learned and followed programming languages closely, not only because I want to learn something cool, but also because Im intrigued by the design decisions and philosophy that each language fosters. Just as a new language provides a different perspective on how to approach software problems, a new paradigm can achieve the same effect. Although the object-oriented approach continues to be the modus operandi of software design, learning about functional programming will open your eyes to new techniques that you can use on their own or in parallel with any other design paradigm that fits your application.
Functional programming has been around for years, but to me it was only a minor distraction. I had heard and read about the benefits of Haskell, Lisp, Scheme, and, more recently, Scala, Clojure, and F# in terms of expressiveness and being highly productive platforms; even Java, which has traditionally been known as a verbose language, has functional artifacts that make code more succinct. Eventually, the minor distraction became impossible to avoid. And guess what? JavaScript, that object-oriented language everyone uses, can be turned around 180 degrees and used functionally. It turns out that this is the most powerful and effective way to use JavaScript. It took me a long time to discover this, and in this book I want to make you aware of it so you dont have go on wondering why your JavaScript code is becoming so complex.
Throughout my journey as a developer, Ive learned how to use functional programming principles to create code that is modular, expressive, robust, easy to reason about, and simple to test. Without a doubt, this has changed me as a software engineer, so I wanted to capture and jot down my experiences somehowperhaps in a book. Naturally, I approached Manning, with the idea of writing a functional programming book using the Dart programming language. I was playing around with Dart at the time and thought that combining it with my functional background would be a fun, unexplored, uncharted territory. I wrote a proposal, and a week later I had an interview. During the interview, I learned that Manning was seeking a person to write a book about functional programming in JavaScript. Because JavaScript is a language Im very much obsessed with, to say the least, I was thrilled to jump into this opportunity. By writing this book, I hope to help you develop the same skills and take your development in a new direction.
Acknowledgments
Writing a book is not a trivial undertaking, and the tireless collaboration of many people with a variety of talents brought to life the manuscript youre holding (or reading onscreen).
The staff at Manning were incredible and instrumental in obtaining the level of quality we all hoped for, and I thank all of them from the bottom of my heart. Without them, this book would not have been possible. Special thanks to Marjan Bace and Mike Stephens for believing in the idea of this book and in me as an author; to Marina Michaels, for giving me a map and a flashlight to navigate this maze of book-writing challenges; to Susan Conant, for bringing me up to speed and teaching me my first lessons about what it means to write a technical book; to Bert Bates, for giving me my initial sparks of creativity and for his amazing insights on how to teach programming; and to everyone on the editorial and production teams, including Mary Piergies, Janet Vail, Kevin Sullivan, Tiffany Taylor, Katie Tennant, Dennis Dalinnik, and many others who worked behind the scenes.
I cant thank enough the amazing group of technical peer reviewers led by Aleksandar DragosavljevicAmy Teng, Andrew Meredith, Becky Huett, Daniel Lamb, David Barkol, Ed Griebel, Efran Cobisi, Ezra Simeloff, John Shea, Ken Fukuyama, Peter Edwards, Subhasis Ghosh, Tanner Slayton, Thorsten Szutzkus, Wilfredo Manrique, William E. Wheeler, and Yiling Luand the talented forum contributors. Their contributions included catching technical mistakes, errors in terminology, and typos, and making topic suggestions. Each pass through the review process and each piece of feedback implemented through the forum topics shaped and molded the manuscript.
On the technical side, special thanks to Dean Iverson, who served as the books technical editor; Daniel Lamb, who served as the books technical proofreader; and Brian Hanafee, for his thorough and in-depth evaluation of the entire book. They are the best technical editors I could have hoped for.
Last but not least, I thank my wife for always supporting me, and my family for pushing me to become better every day and not asking why I didnt call as often to check in while I was writing this book. Also, thanks go to my colleagues at work for purchasing early releases of the chapters. I am grateful to have the pleasure of working alongside such wonderful people.
About this Book
Complexity is a huge beast to tame, and well never get rid of it entirely; it will always be an aspect of software development. Ive spent countless hours and immeasurable brainpower trying to understand what a particular piece of code does. The secret is to control the complexity so it doesnt grow in proportion to the size of your code baseand functional programming can help. Were writing more JavaScript than ever before. Weve gone from building small client-side event-handling routines, to heavy client-side architectures, to complete isomorphic (server + client) JavaScript applications. Functional programming isnt a toolits a way of thinking that can apply equally to any of these environments.