Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Mastering Immutable.js

You're reading from   Mastering Immutable.js Better JavaScript development using immutable data

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher Packt
ISBN-13 9781788395113
Length 216 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Adam Boduch Adam Boduch
Author Profile Icon Adam Boduch
Adam Boduch
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Why Immutable.js? FREE CHAPTER 2. Creating Immutable Data 3. Persistent Changes 4. Filtering Collections and Finding Items 5. Sequences and Side-Effects 6. Sorting Collections 7. Mapping and Reducing 8. Zipping and Flattening 9. Persistent Change Detection 10. Working with Sets 11. Comparing Collections 12. Combining Collections 13. Declarative Decision Making 14. Side-Effects in User Interfaces 15. Side-Effects in Node.js 16. Immutable Architecture

Lazy filtering


We've seen multiple ways to build sequences so far—now let's introduce a real need for them. When you filter sequences, each value from the collection is passed through the predicate function and into the side-effect, one at a time.

Basic lazy filtering

To help illustrate the concept of lazy filtering, let's implement a basic predicate function with some extra logging:

const predicate = (item) => {
  console.log('filtering', item);
  return item % 2;
};

This logging will make it easy for us to see when the actual filtering of a value happens relative to when the value is used in the side-effect. Let's try this on an indexed sequence:

const myList = List.of(1, 2, 3, 4, 5, 6);
myList
  .toSeq()
  .filter(predicate)
  .forEach(item => console.log('myList', item));
  // -> filtering 1
  // -> myList 1
  // -> filtering 2
  // -> filtering 3
  // -> myList 3
  // -> filtering 4
  // -> filtering 5
  // -> myList 5
  // -> filtering 6

If you pay close...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image