O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Functional Programming in JavaScript

Book Description

Unlock the powers of functional programming hidden within JavaScript to build smarter, cleaner, and more reliable web apps

In Detail

This is a fast-paced guide that will help you to write real-world applications by utilizing a wide range of functional techniques and styles.

The book first explores the core concepts of functional programming common to all functional languages, with examples of their use in JavaScript. It's followed by a comprehensive roundup of functional programming libraries for JavaScript that minimizes the burden of digging deep into JavaScript to expose a set of tools that makes functional programming not just possible but highly convenient. The book then rounds off with an overview of methods to effectively use and mix functional programming with object-oriented programming.

What You Will Learn

  • Separate core logic from the program state to write more maintainable code
  • Replace ugly for loops with pure functions and recursion
  • Simplify code with method chains of pure, higher-order functions
  • Create more reliable code with closures and immutable data
  • Explore lazy evaluation strategies and what they can accomplish
  • Develop more powerful applications with currying and function composition
  • Use promises, functors, monads, and function factories

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Functional Programming in JavaScript
    1. Table of Contents
    2. Functional Programming in JavaScript
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers and more
        1. Why Subscribe?
        2. Free Access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. The Powers of JavaScript's Functional Side – a Demonstration
      1. Introduction
      2. The demonstration
      3. The application – an e-commerce website
        1. Imperative methods
      4. Functional programming
      5. Summary
    9. 2. Fundamentals of Functional Programming
      1. Functional programming languages
        1. What makes a language functional?
        2. Advantages
          1. Cleaner code
          2. Modularity
          3. Reusability
          4. Reduced coupling
          5. Mathematically correct
        3. Functional programming in a nonfunctional world
          1. Is JavaScript a functional programming language?
      2. Working with functions
        1. Self-invoking functions and closures
        2. Higher-order functions
        3. Pure functions
        4. Anonymous functions
        5. Method chains
        6. Recursion
          1. Divide and conquer
        7. Lazy evaluation
      3. The functional programmer's toolkit
        1. Callbacks
        2. Array.prototype.map()
        3. Array.prototype.filter()
        4. Array.prototype.reduce()
        5. Honorable mentions
          1. Array.prototype.forEach
          2. Array.prototype.concat
          3. Array.prototype.reverse
          4. Array.prototype.sort
          5. Array.prototype.every and Array.prototype.some
      4. Summary
    10. 3. Setting Up the Functional Programming Environment
      1. Introduction
      2. Functional libraries for JavaScript
        1. Underscore.js
        2. Fantasy Land
        3. Bilby.js
        4. Lazy.js
        5. Bacon.js
        6. Honorable mentions
      3. Development and production environments
        1. Browsers
        2. Server-side JavaScript
          1. A functional use case in the server-side environment
        3. CLI
        4. Using functional libraries with other JavaScript modules
        5. Functional languages that compile into JavaScript
      4. Summary
    11. 4. Implementing Functional Programming Techniques in JavaScript
      1. Partial function application and currying
        1. Function manipulation
          1. Apply, call, and the this keyword
          2. Binding arguments
          3. Function factories
        2. Partial application
          1. Partial application from the left
          2. Partial application from the right
        3. Currying
      2. Function composition
        1. Compose
          1. Sequence – compose in reverse
        2. Compositions versus chains
        3. Programming with compose
      3. Mostly functional programming
        1. Handling events
      4. Functional reactive programming
        1. Reactivity
        2. Putting it all together
      5. Summary
    12. 5. Category Theory
      1. Category theory
        1. Category theory in a nutshell
        2. Type safety
          1. Object identities
      2. Functors
        1. Creating functors
        2. Arrays and functors
        3. Function compositions, revisited
      3. Monads
        1. Maybes
        2. Promises
        3. Lenses
        4. jQuery is a monad
      4. Implementing categories
      5. Summary
    13. 6. Advanced Topics and Pitfalls in JavaScript
      1. Recursion
        1. Tail recursion
          1. The Tail-call elimination
        2. Trampolining
        3. The Y-combinator
          1. Memoization
      2. Variable scope
        1. Scope resolutions
          1. Global scope
          2. Local scope
          3. Object properties
        2. Closures
        3. Gotchas
      3. Function declarations versus function expressions versus the function constructor
        1. Function declarations
        2. Function expressions
        3. The function constructor
        4. Unpredictable behavior
      4. Summary
    14. 7. Functional and Object-oriented Programming in JavaScript
      1. JavaScript – the multi-paradigm language
      2. JavaScript's object-oriented implementation – using prototypes
        1. Inheritance
        2. JavaScript's prototype chain
        3. Inheritance in JavaScript and the Object.create() method
      3. Mixing functional and object-oriented programming in JavaScript
        1. Functional inheritance
          1. Strategy Pattern
        2. Mixins
          1. Classical mixins
          2. Functional mixins
      4. Summary
    15. A. Common Functions for Functional Programming in JavaScript
    16. B. Glossary of Terms
    17. Index