Apply functional programming techniques to write testable terser code for frontend and backend development with improved clarity, concurrency, and performance
Key Features
• Apply functional programming concepts and techniques to everyday JavaScript or TypeScript programming
• Master functional programming in JavaScript and TypeScript to solve real-world development problems
• Apply functional programming to get better testable programs with higher modularity and reusability
Book Description
Functional programming is a programming paradigm that uses functions for developing software. This book is filled with examples that enable you to leverage the latest JavaScript and TypeScript versions to produce modern and clean code, as well as teach you to how apply functional programming techniques to develop more efficient algorithms, write more concise code, and simplify unit testing.
This book provides comprehensive coverage of the major topics in functional programming to produce shorter, clearer, and testable programs. You'll begin by getting to grips with writing and testing pure functions, reducing side effects, as well as other key features to make your applications functional in nature. The book specifically explores techniques to simplify coding, apply recursion, perform high-level coding, learn ways to achieve immutability, implement design patterns, and work with data types.
By the end of this book, you'll have developed the practical programming skills needed to confidently enhance your applications by adding functional programming to wherever it's most suitable.
What you will learn
• Understand when to use functional programming versus classic object-oriented programming
• Use declarative coding instead of imperative coding for clearer, more understandable code
• Know how to avoid side effects and create more reliable code with closures and immutable data
• Use recursion to help design and implement more understandable solutions to complex problems
• Define functional programing data types with or without TypeScript, add type checking, and implement immutability
• Apply advanced containers to get better structures to tackle errors and implement async programming
Who this book is for
If you are a JavaScript or TypeScript developer looking to enhance your programming skills, then this book is for you. The book applies to both frontend developers working with frameworks such as React, Vue, or Angular as well as backend developers using Node.js or Deno.
Author(s): Federico Kereki
Edition: 3
Publisher: Packt Publishing
Year: 2023
Language: English
Commentary: Publisher's PDF
Pages: 614
City: Birmingham, UK
Tags: JavaScript; Functional Programming; Asynchronous Programming; Pipelines; Design Patterns; Lambda Functions; Recursion; Monads; TypeScript; Currying; Testing; Higher-Order Functions; Pure Functions; Function Composition; Declarative Programming; Function Partial Application
Cover
Title Page
Copyright and Credits
Contributors
About the reviewers
Table of Contents
Preface
Chapter 1: Becoming Functional – Several Questions
What is functional programming?
Theory versus practice
A different way of thinking
FP and other programming paradigms
What FP is not
Why use FP?
What we need
What we get
Not all is gold
Is JavaScript functional?
JavaScript as a tool
Going functional with JavaScript
Key features of JavaScript
How do we work with JavaScript?
Using transpilers
Working online
A step further – TypeScript
Testing
Summary
Questions
Chapter 2: Thinking Functionally – A First Example
Our problem – doing something only once
Solution 1 – hoping for the best!
Solution 2 – using a global flag
Solution 3 – removing the handler
Solution 4 – changing the handler
Solution 5 – disabling the button
Solution 6 – redefining the handler
Solution 7 – using a local flag
A functional solution to our problem
A higher-order solution
Testing the solution manually
Testing the solution automatically
Producing an even better solution
Summary
Questions
Chapter 3: Starting Out with Functions – A Core Concept
All about functions
Of lambdas and functions
Arrow functions – the modern way
Functions as objects
Using functions in FP ways
Injection – sorting it out
Callbacks and promises
Continuation-passing style
Polyfills
Stubbing
Immediate invocation (IIFE)
Summary
Questions
Chapter 4: Behaving Properly – Pure Functions
Pure functions
Referential transparency
Side effects
Advantages of pure functions
Impure functions
Avoiding impure functions
Is your function pure?
Testing – pure versus impure
Testing pure functions
Testing purified functions
Testing impure functions
Summary
Questions
Chapter 5: Programming Declaratively – A Better Style
Transformations
Reducing an array to a value
Applying an operation – map()
Dealing with arrays of arrays
More general looping
Logical HOFs
Filtering an array
Searching an array
Higher-level predicates – every() and some()
Checking negatives – none()
Working with async functions
Some strange behaviors
Async-ready looping
Working with parallel functions
Unresponsive pages
A frontend worker
A backend worker
Workers, FP style
Long-living pooled workers
Summary
Questions
Chapter 6: Producing Functions – Higher-Order Functions
Wrapping functions – keeping behavior
Logging
Timing functions
Memoizing functions
Altering a function’s behavior
Doing things once, revisited
Logically negating a function
Inverting the results
Arity changing
Throttling and debouncing
Changing functions in other ways
Turning operations into functions
Turning functions into promises
Getting a property from an object
Demethodizing – turning methods into functions
Methodizing – turning functions into methods
Finding the optimum
Summary
Questions
Chapter 7: Transforming Functions – Currying and Partial Application
A bit of theory
Currying
Dealing with many parameters
Currying by hand
Currying with bind()
Partial application
Partial application with arrow functions
Partial application with closures
Partial currying
Partial currying with bind()
Partial currying with closures
Final thoughts
Variable number of parameters
Parameter order
Being functional
Summary
Questions
Chapter 8: Connecting Functions – Pipelining, Composition, and More
Pipelining
Piping in Unix/Linux
Revisiting an example
Creating pipelines
Debugging pipelines
Pointfree style
Chaining and fluent interfaces
An example of fluent APIs
Chaining method calls
Composing
Some examples of composition
Composing with higher-order functions
Transducing
Composing reducers
Generalizing for all reducers
Testing connected functions
Testing pipelined functions
Testing composed functions
Testing chained functions
Testing transduced functions
Summary
Questions
Chapter 9: Designing Functions – Recursion
Using recursion
Thinking recursively
Higher-order functions revisited
Searching and backtracking
Mutual recursion
Odds and evens
Doing arithmetic
Recursion techniques
Tail call optimization
Continuation-passing style
Trampolines and thunks
Recursion elimination
Summary
Questions
Chapter 10: Ensuring Purity – Immutability
Going the straightforward JavaScript way
Mutator functions
Constants
Freezing
Cloning and mutating
Getters and setters
Lenses
Prisms
Creating persistent data structures
Working with lists
Updating objects
A final caveat
Summary
Questions
Chapter 11: Implementing Design Patterns – The Functional Way
Understanding design patterns
Design pattern categories
Do we need design patterns?
Object-oriented design patterns
Facade and Adapter
Decorator or Wrapper
Strategy, Template, and Command
Dependency Injection
Observers and reactive programming
Other patterns
Functional design patterns
Summary
Questions
Chapter 12: Building Better Containers – Functional Data Types
Specifying data types
Signatures for functions
Other data type options
Building containers
Extending current data types
Containers and functors
Monads
Functions as data structures
Binary trees in Haskell
Functions as binary trees
Summary
Questions
Answers to Questions
Chapter 1, Becoming Functional – Several Questions
Chapter 2, Thinking Functionally – A First Example
Chapter 3, Starting Out with Functions – A Core Concept
Chapter 4, Behaving Properly – Pure Functions
Chapter 5, Programming Declaratively – A Better Style
Chapter 6, Producing Functions – Higher-Order Functions
Chapter 7, Transforming Functions – Currying and Partial Application
Chapter 8, Connecting Functions – Pipelining, Composition, and More
Chapter 9, Designing Functions – Recursion
Chapter 10, Ensuring Purity – Immutability
Chapter 11, Implementing Design Patterns – The Functional Way
Chapter 12, Building Better Containers – Functional Data Types
Bibliography
Index
Other Books You May Enjoy