I like functional programming languages. One of my favorite features is native immutability. Some functional languages, by default, don't let you change values. What's amazing about this capability is that you can't accidentally break your application by mutating data at the wrong time. If you try, the code won't even compile.
I also dislike certain aspects of functional programming languages. On one hand, it's great that immutability is enforced the way it is. On the other hand, there are times when I just need to push aside my functional programming principles and get the job done.
The JavaScript language has many useful functional programming features out of the box. You don't need a library to compose higher-order functions, for example. Something that's difficult to get right with JavaScript is immutability. This is where Immutable.js shines.
When I first heard about Immutable.js, I pictured a library that enforced immutability on JavaScript structures and nothing more. It turns out there's much more. When you're working with immutable structures, you're constantly creating them. The Immutable.js collection API helps you create the structures you need, when you need them.
Since this is a JavaScript library, there's no immutability by default. This allows me to use Immutable.js where immutability matters, and to mutate things when I need to. This is what makes this such an essential library for applications in the real world; pragmatic functional programming that gets the job done.
Chapter 1, Why Immutable.js?, explains why you want to use this library.
Chapter 2, Creating Immutable Data, helps you get started by creating immutable collections.
Chapter 3, Persistent Changes, discusses how to create new data by changing existing data.
Chapter 4, Filtering Collections and Finding Items, talks about locating the data that you need.
Chapter 5, Sequences and Side-Effects, explains how to iterate over collections in order to use collection data.
Chapter 6, Sorting Collections, helps you in get the order right.
Chapter 7, Mapping and Reducing, introduces the fundamentals of transforming collection data.
Chapter 8, Zipping and Flattening, discusses simplifying iterations and collection structures.
Chapter 9, Persistent Change Detection, enables you to determine whether a persistent change method actually changed data.
Chapter 10, Working with Sets, explains how to produce unique collection values.
Chapter 11, Comparing Collections, talks about figuring out how one collection relates to another.
Chapter 12, Combining Collections, helps you build new collections by adding collections together.
Chapter 13, Declarative Decision Making, uses immutable collections to help write declarative code.
Chapter 14, Side-Effects in User Interfaces, explains rendering UI components with immutable collections.
Chapter 15, Side-Effects in Node.js, discusses reading and writing collection data using IO streams.
Chapter 16, Immutable Architecture, puts it all together, in the form of a reusable pattern.
You need a computer, Node.js, and a code editor. Instructions for setting up tools to run the code examples can be found at https://github.com/PacktPublishing/Mastering-Immutablejs.
This book is for anyone who wants to improve the reliability and performance of their JavaScript code by leveraging the ideas of immutability. Basic JavaScript knowledge is assumed. You should be able to create arrays and objects, manipulate arrays and objects, and create and call functions.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: Code words in text are shown as follows: "We can include other contexts through the use of the import
statement."
A block of code is set as follows:
#import packages into the project from bs4 import BeautifulSoup from urllib.request import urlopen import pandas as pd
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
import { List } from 'immutable';
const myList = List();
console.log('List', myList instanceof List);
// -> List true
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "In order to download new modules, we will go to Files
| Settings
| Project Name
| Project Interpreter
."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply email [email protected]
, and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for this book 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 emailed directly to you. You can download the code files by following these steps:
- Log in or register to our website using your email address and password.
- Hover the mouse pointer on the
SUPPORT
tab at the top. - Click on
Code Downloads & Errata
. - Enter the name of the book in the
Search
box. - Select the book for which you're looking to download the code files.
- Choose from the drop-down menu where you purchased this book from.
- Click on
Code Download
.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
- WinRAR / 7-Zip for Windows
- Zipeg / iZip / UnRarX for Mac
- 7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/MasteringImmutablejs. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/MasteringImmutablejs.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form
link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title. To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata
section.
Piracy of copyrighted material on the internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the internet, please provide us with the location address or website name immediately so that we can pursue a remedy. Please contact us at [email protected]
with a link to the suspected pirated material. We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at [email protected]
, and we will do our best to address the problem.