Functional JavaScript

Book description

How can you overcome JavaScript language oddities and unsafe features? With this book, you’ll learn how to create code that’s beautiful, safe, and simple to understand and test by using JavaScript’s functional programming support. Author Michael Fogus shows you how to apply functional-style concepts with Underscore.js, a JavaScript library that facilitates functional programming techniques. Sample code is available on GitHub at https://github.com/funjs/book-source.

Fogus helps you think in a functional way to help you minimize complexity in the programs you build. If you’re a JavaScript programmer hoping to learn functional programming techniques, or a functional programmer looking to learn JavaScript, this book is the ideal introduction.

  • Use applicative programming techniques with first-class functions
  • Understand how and why you might leverage variable scoping and closures
  • Delve into higher-order functions—and learn how they take other functions as arguments for maximum advantage
  • Explore ways to compose new functions from existing functions
  • Get around JavaScript’s limitations for using recursive functions
  • Reduce, hide, or eliminate the footprint of state change in your programs
  • Practice flow-based programming with chains and functional pipelines
  • Discover how to code without using classes

Publisher resources

View/Submit Errata

Table of contents

  1. Functional JavaScript
  2. Dedication
  3. Foreword by Jeremy Ashkenas
  4. Foreword by Steve Vinoski
  5. Preface
    1. What Is Underscore?
      1. Getting Underscore
      2. Using Underscore
      3. The Source Code for Functional JavaScript
      4. Running the Code in This Book
      5. Notational Conventions
    2. Whom Functional JavaScript Is Written For
    3. A Roadmap for Functional JavaScript
    4. Conventions Used in This Book
    5. Using Code Examples
    6. Safari® Books Online
    7. How to Contact Us
    8. Acknowledgments
  6. 1. Introducing Functional JavaScript
    1. The Case for JavaScript
      1. Some Limitations of JavaScript
    2. Getting Started with Functional Programming
      1. Why Functional Programming Matters
      2. Functions as Units of Abstraction
      3. Encapsulation and Hiding
      4. Functions as Units of Behavior
      5. Data as Abstraction
      6. A Taste of Functional JavaScript
      7. On Speed
    3. The Case for Underscore
    4. Summary
  7. 2. First-Class Functions and Applicative Programming
    1. Functions as First-Class Things
      1. JavaScript’s Multiple Paradigms
        1. Imperative programming
        2. Prototype-based object-oriented programming
        3. Metaprogramming
    2. Applicative Programming
      1. Collection-Centric Programming
      2. Other Examples of Applicative Programming
        1. reduceRight
        2. find
        3. reject
        4. all
        5. any
        6. sortBy, groupBy, and countBy
      3. Defining a Few Applicative Functions
    3. Data Thinking
      1. “Table-Like” Data
    4. Summary
  8. 3. Variable Scope and Closures
    1. Global Scope
    2. Lexical Scope
    3. Dynamic Scope
      1. JavaScript’s Dynamic Scope
    4. Function Scope
    5. Closures
      1. Simulating Closures
        1. Free variables
        2. Shadowing
      2. Using Closures
      3. Closures as an Abstraction
    6. Summary
  9. 4. Higher-Order Functions
    1. Functions That Take Other Functions
      1. Thinking About Passing Functions: max, finder, and best
        1. Tightening it up a bit
      2. More Thinking About Passing Functions: repeat, repeatedly, and iterateUntil
        1. Use functions, not values
        2. I said, “Use functions, not values”
    2. Functions That Return Other Functions
      1. Capturing Arguments to Higher-Order Functions
      2. Capturing Variables for Great Good
        1. Take care when mutating values
      3. A Function to Guard Against Nonexistence: fnull
    3. Putting It All Together: Object Validators
    4. Summary
  10. 5. Function-Building Functions
    1. The Essence of Functional Composition
      1. Mutation Is a Low-Level Operation
    2. Currying
      1. To Curry Right, or To Curry Left
      2. Automatically Currying Parameters
        1. Building new functions using currying
        2. Currying three parameters to implement HTML hex color builders
      3. Currying for Fluent APIs
      4. The Disadvantages of Currying in JavaScript
    3. Partial Application
      1. Partially Applying One and Two Known Arguments
      2. Partially Applying an Arbitrary Number of Arguments
      3. Partial Application in Action: Preconditions
    4. Stitching Functions End-to-End with Compose
      1. Pre- and Postconditions Using Composition
    5. Summary
  11. 6. Recursion
    1. Self-Absorbed Functions (Functions That Call Themselves)
      1. Graph Walking with Recursion
      2. Depth-First Self-Recursive Search with Memory
        1. Tail (self-)recursion
      3. Recursion and Composing Functions: Conjoin and Disjoin
    2. Codependent Functions (Functions Calling Other Functions That Call Back)
      1. Deep Cloning with Recursion
      2. Walking Nested Arrays
    3. Too Much Recursion!
      1. Generators
      2. The Trampoline Principle and Callbacks
    4. Recursion Is a Low-Level Operation
    5. Summary
  12. 7. Purity, Immutability, and Policies for Change
    1. Purity
      1. The Relationship Between Purity and Testing
      2. Separating the Pure from the Impure
      3. Property-Testing Impure Functions
      4. Purity and the Relationship to Referential Transparency
      5. Purity and the Relationship to Idempotence
    2. Immutability
      1. If a Tree Falls in the Woods, Does It Make a Sound?
      2. Immutability and the Relationship to Recursion
      3. Defensive Freezing and Cloning
      4. Observing Immutability at the Function Level
      5. Observing Immutability in Objects
      6. Objects Are Often a Low-Level Operation
    3. Policies for Controlling Change
    4. Summary
  13. 8. Flow-Based Programming
    1. Chaining
      1. A Lazy Chain
      2. Promises
    2. Pipelining
    3. Data Flow versus Control Flow
      1. Finding a Common Shape
      2. A Function to Simplify Action Creation
    4. Summary
  14. 9. Programming Without Class
    1. Data Orientation
      1. Building Toward Functions
    2. Mixins
      1. Core Prototype Munging
      2. Class Hierarchies
      3. Changing Hierarchies
      4. Flattening the Hierarchy with Mixins
      5. New Semantics via Mixin Extension
      6. New Types via Mixin Mixing
      7. Methods Are Low-Level Operations
    3. }).call(“Finis”);
  15. A. Functional JavaScript in the Wild
    1. Functional Libraries for JavaScript
      1. Functional JavaScript
      2. Underscore-contrib
      3. RxJS
      4. Bilby
      5. allong.es
      6. Other Functional Libraries
    2. Functional Programming Languages Targeting JavaScript
      1. ClojureScript
      2. CoffeeScript
      3. Roy
      4. Elm
  16. B. Annotated Bibliography
    1. Papers/Books/Blog Posts/Talks
    2. Presentations
    3. Blog Posts
    4. Journal Articles
  17. Index
  18. About the Author
  19. Colophon
  20. Copyright

Product information

  • Title: Functional JavaScript
  • Author(s): Michael Fogus
  • Release date: June 2013
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449360726