Anto Aravinth

Beginning Functional JavaScript

Functional Programming with JavaScript Using EcmaScript 6

Anto Aravinth

Chennai, Tamil Nadu, India

Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the book’s product page, located at www.apress.com/978-1-4842-2655-1 . For more detailed information, please visit http://www.apress.com/source-code .

ISBN 978-1-4842-2655-1

e-ISBN 978-1-4842-2656-8

DOI 10.1007/978-1-4842-2656-8

Library of Congress Control Number: 2017934504

© Anto Aravinth 2017

This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.

Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.

While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.

Printed on acid-free paper

Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

Dedicated to God, Belgin Rayen (late), Susila, Kishore, Ramya and my beloved ones.

Acknowledgments

Writing a book is not easy as I would have thought. It’s almost very like making a movie. You need to go to each publication unit with your table of contents. The table of contents are like your movie script. It needs to have a strong beginning, keep the audience engaged, and then have a good ending. The screenplay should be very good which is achieved via your text. The process of a book starts when an editorial team accepts your table of contents. I want to thank Pramila for that; she was helpful in the initial stages of the book. Of course, writing a technical book needs to be technically correct as well. And that’s where I need to give a special thanks to our technical editorial team! They were very good at catching any technical issues in my writing. I want to give special thanks to Anila for working through the chapters and finding any grammatical errors – and making sure the contents are good enough for the readers to be engaged. All these phases are managed by our manager named Prachi. Thanks, Prachi, for making it happen!

I want to dedicate this book to my Lord Jesus, late father Belgin Rayen, and my beloved mother named Susila. I also want to give thanks to Kishore, my brother-in-law, for supporting me throughout my life and career. I never told my sister Ramya (one and only sibling) that I’m authoring a book. I just couldn’t predict how she would react to this event. Special thanks to her as well.

Special thanks to all my friends & colleagues who have been very supportive in my career: Deepak, Vishal, Shiva, Mustafa, Anand, Ram (Juspay), Vimal (Juspay), Lalitha, Swetha, Vishwapriya. Final thanks to my close cousins: Bianca, Jennifer, Amara, Arun, Clinton, Shiny, Sanju.

There can be improvements made in my style of writing, content, authoring, etc. If you want to share your thoughts, please contact me at antoaravinthrayen@gmail.com . I’m also available on twitter @antoaravinth.

Thanks for purchasing this book! I hope you will enjoy it. Good luck!

Anto Aravinth, India

Contents

  1. Chapter 1:​ Functional Programming in Simple Terms
    1. What Is Functional Programming?​ Why It Matters?​
    2. Referential Transparency
    3. Imperative, Declarative, Abstraction
    4. Functional Programming Benefits
    5. Pure Functions
      1. Pure Functions Lead to Testable Code
      2. Reasonable Code
    6. Parallel Code
    7. Cachable
    8. Pipelines and Composable
    9. Pure Function Is a Mathematical Function
    10. What We Are Going to Build
    11. Is JavaScript a Functional Programming Language?​
    12. Summary
  2. Chapter 2:​ Fundamentals of JavaScript Functions
    1. ECMAScript A Bit of History
    2. Creating and Executing Functions
      1. First Function
      2. Strict Mode
      3. Return Statement Is Optional
      4. Multiple Statement Functions
      5. Function Arguments
      6. ES5 Functions Are Valid in ES6
    3. Setting Up Our Project
      1. Initial Setup
      2. Our First Functional Approach to the Loop Problem
      3. Gist on Exports
      4. Gist on Imports
      5. Running the Code Using Babel-Node
      6. Creating Script in Npm
      7. Running the Source Code from Git
    4. Summary
  3. Chapter 3:​ Higher-Order Functions
    1. Understanding Data
      1. Understanding JavaScript Data Types
      2. Storing a Function
      3. Passing a Function
      4. Returning a Function
    2. Abstraction and Higher-Order Functions
      1. Abstraction Definitions
      2. Abstraction via Higher-Order Functions
    3. Higher-Order Functions in the Real World
      1. every Function
      2. some Function
      3. sort Function
    4. Summary
  4. Chapter 4:​ Closures and Higher-Order Functions
    1. Understanding Closures
      1. What Are Closures?​
      2. Remembering Where It Is Born
      3. Revisiting sortBy Function
    2. Higher-Order Functions in the Real World (Continued)
      1. tap Function
      2. unary Function
      3. once Function
      4. Memoize Function
    3. Summary
  5. Chapter 5:​ Being Functional on Arrays
    1. Working Functionally on Arrays
      1. map
      2. filter
    2. Chaining Operations
      1. concatAll
    3. Reducing Function
      1. reduce Function
    4. Zipping Arrays
      1. zip Function
    5. Summary
  6. Chapter 6:​ Currying and Partial Application
    1. A Few Terminologies
      1. unary Function
      2. Binary Function
      3. variadic Functions
    2. Currying
      1. Currying Use Cases
      2. A logger Function - Using Currying
      3. Revisit Curry
      4. Back to logger Function
    3. Currying in Action
      1. Finding number in Array Contents
      2. squaring an Array
    4. Data Flow
      1. Partial Application
      2. Implementing partial Function
      3. Currying vs.​ Partial Application
    5. Summary
  7. Chapter 7:​ Composition and Pipelines
    1. Composition in General Terms
      1. Unix Philosophy
    2. Functional Composition
      1. Revisiting map,filter
      2. compose Function
    3. Playing with compose function
      1. curry and partial to the Rescue
      2. compose many function
    4. Pipelines /​ Sequence
      1. Implementing pipe
      2. Odds on Composition
      3. Debugging Using tap Function
    5. Summary
  8. Chapter 8:​ Fun with Functors
    1. What Is a Functor?​
      1. Functor Is a Container
      2. Functor Implements Method Called map
    2. MayBe
      1. Implementing MayBe
      2. Simple Use Cases
      3. Real-World Use Cases
    3. Either Functor
      1. Implementing Either
      2. Reddit Example Either Version
    4. Word of Caution - Pointed Functor
    5. Summary
  9. Chapter 9:​ Monads in Depth
    1. Getting Reddit Comments for Our Search Query
    2. The Problem
      1. Implementation of the First Step
      2. Problem of So Many maps
    3. Solving the Problem via join
      1. join Implementation
      2. chain Implementation
    4. Summary
  10. Chapter 10:​ Pause, Resume with Generators
    1. Async Code and Its Problem
      1. Callback Hell
    2. Generators 101
      1. Creating Generators
      2. Caveats of Generators
      3. yield New Keyword
      4. done Property of Generator
      5. Passing Data to Generators
    3. Using Generators to Handle Async Calls
      1. Generators for Async - A Simple Case
      2. Generators for Async - A Real-World Case
    4. Summary
  11. Appendix A
  12. How to Install Node In Your System
  13. Installing Depedencies
  14. Index

About the Author

Anto Aravinth is a Senior Business Intelligence Developer at VisualBI, Chennai. He has been busy developing web applications using Java; JavaScript; and frameworks like ReactJs, Angular, etc., for the last five years. He has a solid understanding of the Web and its standards. Anto is also an open source contributor to popular frameworks such as ReactJs, Selenium, and Groovy.

Anto loves playing table tennis in his free time. He has a great sense of humor, too! Anto is also a Technical Development Editor for React Quickly , a book that will be published by Manning in 2017.