How to use the Collection function from immutable

Find comprehensive JavaScript immutable.Collection code examples handpicked from public code repositorys.

immutable.Collection is a class in the Immutable.js library that provides a high-performance, persistent data structure for managing collections of values in JavaScript.

4
5
6
7
8
9
10
11
12
13
14
15
const throwError = msg => {
  throw new Error(msg)
}


const toJS = x =>
  x instanceof im.Collection
    ? x.toJS()
    : x


// Language primitives
fork icon0
star icon3
watch icon1

How does immutable.Collection work?

In Immutable.js, immutable.Collection is a class that provides a high-performance, persistent data structure for managing collections of values in JavaScript. A persistent data structure is one that preserves the previous version of the data when it is modified, instead of modifying the original data in place. When you create a new immutable.Collection object, you can add, remove, and update values in the collection using a variety of methods, such as add, remove, merge, and set. Each of these methods returns a new immutable.Collection object that represents the modified version of the collection, while leaving the original collection unchanged. Under the hood, immutable.Collection uses a technique called structural sharing to ensure that only the modified parts of the collection are copied to the new version, while the rest of the data is shared between the old and new versions. This makes it possible to efficiently manage large collections of data without incurring the performance penalty of copying the entire collection every time it is modified. Overall, immutable.Collection provides a powerful and efficient way to manage collections of values in JavaScript, with the added benefit of immutability, making it easier to reason about your code and avoid bugs related to unexpected changes in your data.

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
const { Collection } = require("immutable");

// Create a new collection
const collection = Collection([1, 2, 3, 4, 5]);

// Add a new value to the collection
const newCollection = collection.add(6);

// Remove a value from the collection
const newerCollection = newCollection.remove(2);

// Update a value in the collection
const newestCollection = newerCollection.set(3, 7);

// Log the original and modified collections to the console
console.log(`Original collection: ${collection.toArray()}`);
console.log(`New collection: ${newestCollection.toArray()}`);

In this example, we're using immutable.Collection to manage a collection of values. We first create a new collection containing the numbers 1 through 5. We then use the add method to add the number 6 to the collection, the remove method to remove the number 2 from the collection, and the set method to update the value at index 3 to 7. Each time we modify the collection, we create a new immutable.Collection object that represents the modified version of the collection. We then log the original and modified collections to the console using the toArray method, which converts the immutable.Collection object to a regular JavaScript array. When you run this code, you'll see that the original collection and the modified collection are both printed to the console. Note that the modified collection contains the numbers 1, 3, 4, 5, 6, and 7, with the number 2 removed and the number 7 added in its place.