Mastering JavaScript Functional Programming - Second Edition

Book description

Explore the functional programming paradigm and the different techniques for developing better algorithms, writing more concise code, and performing seamless testing

Key Features

  • Explore this second edition updated to cover features like async functions and transducers, as well as functional reactive programming
  • Enhance your functional programming (FP) skills to build web and server apps using JavaScript
  • Use FP to enhance the modularity, reusability, and performance of apps

Book Description

Functional programming is a paradigm for developing software with better performance. It helps you write concise and testable code. To help you take your programming skills to the next level, this comprehensive book will assist you in harnessing the capabilities of functional programming with JavaScript and writing highly maintainable and testable web and server apps using functional JavaScript.

This second edition is updated and improved to cover features such as transducers, lenses, prisms and various other concepts to help you write efficient programs. By focusing on functional programming, you’ll not only start to write but also to test pure functions, and reduce side effects. The book also specifically allows you to discover techniques for simplifying code and applying recursion for loopless coding. Gradually, you’ll understand how to achieve immutability, implement design patterns, and work with data types for your application, before going on to learn functional reactive programming to handle complex events in your app. Finally, the book will take you through the design patterns that are relevant to functional programming.

By the end of this book, you’ll have developed your JavaScript skills and have gained knowledge of the essential functional programming techniques to program effectively.

What you will learn

  • Simplify JavaScript coding using function composition, pipelining, chaining, and transducing
  • Use declarative coding as opposed to imperative coding to write clean JavaScript code
  • Create more reliable code with closures and immutable data
  • Apply practical solutions to complex programming problems using recursion
  • Improve your functional code using data types, type checking, and immutability
  • Understand advanced functional programming concepts such as lenses and prisms for data access

Who this book is for

This book is for JavaScript developers who want to enhance their programming skills and build efficient web applications. Frontend and backend developers who use various JavaScript frameworks and libraries like React, Angular, or Node.js will also find the book helpful. Working knowledge of ES2019 is required to grasp the concepts covered in the book easily.

Table of contents

  1. Title Page
  2. Copyright and Credits
    1. Mastering JavaScript Functional Programming Second Edition
  3. Dedication
  4. About Packt
    1. Why subscribe?
  5. Contributors
    1. About the author
    2. About the reviewers
    3. Packt is searching for authors like you
  6. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Download the example code files
      2. Conventions used
    4. Get in touch
      1. Reviews
  7. Technical Requirements
  8. Becoming Functional - Several Questions
    1. What is functional programming?
      1. Theory versus practice
      2. A different way of thinking
      3. What FP is not
    2. Why use FP?
      1. What we need
      2. What we get
      3. Not all is gold
    3. Is JavaScript functional?
      1. JavaScript as a tool
      2. Going functional with JavaScript
      3. Key features of JavaScript
        1. Functions as first-class objects
        2. Recursion
        3. Closures
        4. Arrow functions
        5. Spread
    4. How do we work with JavaScript?
      1. Using transpilers
      2. Working online
      3. Testing
    5. Summary
    6. Questions
  9. Thinking Functionally - A First Example
    1. Our problem – doing something only once
      1. Solution 1 – hoping for the best!
      2. Solution 2 – using a global flag
      3. Solution 3 – removing the handler
      4. Solution 4 – changing the handler
      5. Solution 5 – disabling the button
      6. Solution 6 – redefining the handler
      7. Solution 7 – using a local flag
    2. A functional solution to our problem
      1. A higher-order solution
      2. Testing the solution manually
      3. Testing the solution automatically
      4. Producing an even better solution
    3. Summary
    4. Questions
  10. Starting Out with Functions - A Core Concept
    1. All about functions
      1. Of lambdas and functions
      2. Arrow functions – the modern way
        1. Returning values
        2. Handling the this value
        3. Working with arguments
        4. One argument or many?
      3. Functions as objects
        1. A React-Redux reducer
        2. An unnecessary mistake
        3. Working with methods
    2. Using functions in FP ways
      1. Injection – sorting it out
      2. Callbacks, promises, and continuations
      3. Continuation passing style
      4. Polyfills
        1. Detecting Ajax
        2. Adding missing functions
      5. Stubbing
      6. Immediate invocation
    3. Summary
    4. Questions 
  11. Behaving Properly - Pure Functions
    1. Pure functions
      1. Referential transparency
      2. Side effects
        1. Usual side effects
        2. Global state
        3. Inner state
        4. Argument mutation
        5. Troublesome functions
      3. Advantages of pure functions
        1. Order of execution
        2. Memoization
        3. Self-documentation
        4. Testing
    2. Impure functions
      1. Avoiding impure functions
        1. Avoiding the usage of state
        2. Injecting impure functions
      2. Is your function pure?
    3. Testing – pure versus impure
      1. Testing pure functions
      2. Testing purified functions
      3. Testing impure functions
    4. Summary
    5. Questions
  12. Programming Declaratively - A Better Style
    1. Transformations
      1. Reducing an array to a value
        1. Summing an array
        2. Calculating an average
        3. Calculating several values at once
        4. Folding left and right
      2. Applying an operation – map
        1. Extracting data from objects
        2. Parsing numbers tacitly
        3. Working with ranges
        4. Emulating map() with reduce()
      3. Dealing with arrays of arrays
        1. Flattening an array
        2. Mapping and flattening – flatMap()
        3. Emulating flat() and flatMap()
      4. More general looping
    2. Logical higher-order functions
      1. Filtering an array
        1. A reduce() example
        2. Emulating filter() with reduce()
      2. Searching an array
        1. A special search case
        2. Emulating find() and findIndex() with reduce()
      3. Higher-level predicates – some, every
      4. Checking negatives – none
    3. Working with async functions
      1. Some strange behaviors
      2. Async-ready looping
        1. Looping over async calls
        2. Mapping async calls
        3. Filtering with async calls
        4. Reducing async calls
    4. Summary
    5. Questions
  13. Producing Functions - Higher-Order Functions
    1. Wrapping functions – keeping behavior
      1. Logging
        1. Logging in a functional way
        2. Taking exceptions into account
        3. Working in a purer way
      2. Timing functions
      3. Memoizing functions
        1. Simple memoization
        2. More complex memoization
        3. Memoization testing
    2. Altering a function's behavior
      1. Doing things once, revisited
      2. Logically negating a function
      3. Inverting the results
      4. Arity changing
    3. Changing functions in other ways
      1. Turning operations into functions
        1. Implementing operations
        2. A handier implementation
      2. Turning functions into promises
      3. Getting a property from an object
      4. Demethodizing – turning methods into functions
      5. Finding the optimum
    4. Summary
    5. Questions
  14. Transforming Functions - Currying and Partial Application
    1. A bit of theory
    2. Currying
      1. Dealing with many parameters
      2. Currying by hand
      3. Currying with bind()
      4. Currying with eval()
    3. Partial application
      1. Partial application with arrow functions
      2. Partial application with eval()
      3. Partial application with closures
    4. Partial currying
      1. Partial currying with bind()
      2. Partial currying with closures
    5. Final thoughts
      1. Parameter order
      2. Being functional
    6. Summary
    7. Questions
  15. Connecting Functions - Pipelining and Composition
    1. Pipelining
      1. Piping in Unix/Linux
      2. Revisiting an example
      3. Creating pipelines
        1. Building pipelines by hand
        2. Using other constructs
      4. Debugging pipelines
        1. Using tee
        2. Tapping into a flow
        3. Using a logging wrapper
      5. Pointfree style
        1. Defining pointfree functions
        2. Converting to pointfree style
    2. Chaining and fluent interfaces
      1. An example of fluent APIs
      2. Chaining method calls
    3. Composing
      1. Some examples of composition
        1. Unary operators
        2. Counting files
        3. Finding unique words
      2. Composing with higher-order functions
      3. Testing composed functions
    4. Transducing
      1. Composing reducers
      2. Generalizing for all reducers
    5. Summary
    6. Questions
  16. Designing Functions - Recursion
    1. Using recursion
      1. Thinking recursively
        1. Decrease and conquer – searching
        2. Decrease and conquer – doing powers
        3. Divide and conquer – the Towers of Hanoi
        4. Divide and conquer – sorting
        5. Dynamic programming – making change
      2. Higher-order functions revisited
        1. Mapping and filtering
        2. Other higher-order functions
      3. Searching and backtracking
        1. The eight queens puzzle
        2. Traversing a tree structure
    2. Recursion techniques
      1. Tail call optimization
      2. Continuation passing style
      3. Trampolines and thunks
      4. Recursion elimination
    3. Summary
    4. Questions
  17. Ensuring Purity - Immutability
    1. Going the straightforward JavaScript way
      1. Mutator functions
      2. Constants
      3. Freezing
      4. Cloning and mutating
      5. Getters and setters
        1. Getting a property
        2. Setting a property by path
      6. Lenses
        1. Working with lenses
        2. Implementing lenses with objects
        3. Implementing lenses with functions
      7. Prisms
        1. Working with prisms
        2. Implementing prisms
    2. Creating persistent data structures
      1. Working with lists
      2. Updating objects
      3. A final caveat
    3. Summary
    4. Questions
  18. Implementing Design Patterns - The Functional Way
    1. Understanding design patterns
      1. Design pattern categories
      2. Do we need design patterns?
    2. Object-oriented design patterns
      1. Facade and adapter
      2. Decorator or wrapper
      3. Strategy, Template, and Command
      4. Observer and reactive programming
        1. Basic concepts and terms
        2. Operators for observables
        3. Detecting multi-clicks
        4. Providing typeahead searches
      5. Other patterns
    3. Functional design patterns
    4. Summary
    5. Questions
  19. Building Better Containers - Functional Data Types
    1. Specifying data types
      1. Signatures for functions 
      2. Other data type options
    2. Building containers 
      1. Extending current data types
      2. Containers and functors
        1. Wrapping a value – a basic container
        2. Enhancing our container – functors
        3. Dealing with missing values with Maybe
          1. Dealing with varying API results
          2. Implementing Prisms
      3. Monads
        1. Adding operations
        2. Handling alternatives – the Either monad
        3. Calling a function – the Try monad
        4. Unexpected monads – promises
    3. Functions as data structures
      1. Binary trees in Haskell
      2. Functions as binary trees
    4. Summary
    5. Questions
  20. Bibliography
  21. Answers to Questions
    1. Chapter 1, Becoming Functional – Several Questions
    2. Chapter 2, Thinking Functionally – a First Example
    3. Chapter 3, Starting Out with Functions – a Core Concept
    4. Chapter 4, Behaving Properly – Pure Functions
    5. Chapter 5, Programming Declaratively – a Better Style
    6. Chapter 6, Producing Functions – Higher-Order Functions
    7. Chapter 7, Transforming Functions – Currying and Partial Application
    8. Chapter 8, Connecting Functions – Pipelining and Composition
    9. Chapter 9, Designing Functions – Recursion
    10. Chapter 10, Ensuring Purity – Immutability
    11. Chapter 11, Implementing Design Patterns – the Functional Way
    12. Chapter 12, Building Better Containers – Functional Data Types
  22. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think

Product information

  • Title: Mastering JavaScript Functional Programming - Second Edition
  • Author(s): Federico Kereki
  • Release date: January 2020
  • Publisher(s): Packt Publishing
  • ISBN: 9781839213069