How to use the Set function from immutable

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

immutable.Set is a JavaScript data structure that represents a set of unique values, which is immutable and cannot be modified once created.

262
263
264
265
266
267
268
269
270
271

path = valToKeyPath(path) || [];

var self = this,
    cursor = this.cursor(path),
    unobservers = Immutable.Set();

function cursorRefresher() { cursor = self.cursor(path); }
function _subscribe (path, fn) {
  self._referencelisteners = subscribe(self._referencelisteners, path, fn);
fork icon21
star icon374
watch icon0

33
34
35
36
37
38
39
40
41
42

const plainArray = [ 1, 2, 3, 4 ]
const listFromPlainArray = List(plainArray)
// List [ 1, 2, 3, 4 ]

const plainSet = Set([ 1, 2, 3, 4 ])
const listFromPlainSet = List(plainSet)
// List [ 1, 2, 3, 4 ]

const arrayIterator = plainArray[Symbol.iterator]()
fork icon8
star icon31
watch icon1

+ 5 other calls in file

How does immutable.Set work?

immutable.Set works by creating a new set object that contains a collection of unique values.

The set object is immutable, meaning that it cannot be modified once created.

However, new sets can be created from existing sets using set operations such as union, intersection, and difference.

Sets can be compared for equality, and new sets can be created by adding or removing values from existing sets using add and delete methods.

Immutable sets can be useful in situations where you need to keep track of a collection of unique values that cannot be modified, such as in a functional programming paradigm.

By providing a set of reliable and efficient operations for working with sets of data, immutable.Set can simplify many programming tasks that involve sets of data.

7
8
9
10
11
12
13
14
15
16
function callListener(listener) {
  listener();
}

function FormContext(val, schema) {
  var listeners = Immutable.Set();

  return freeze({
    getIn: function(keyPath) {
      return val.getIn(keyPath || []);
fork icon0
star icon8
watch icon3

+ 13 other calls in file

6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
    var cache = exports.$sithCache.get();
    return ids.map(function (id) { return cache.get(id); });
});
exports.$remoteSiths = exports.$sithIDs.derive(function (ids) {
    var cache = exports.$sithCache.get();
    return immutable_1.Set(ids.filter(function (id) { return id !== null && cache.get(id) == null; }));
});
exports.$redAlert = exports.$localSiths.derive(function (siths) {
    var worldId = exports.$worldId.get();
    return siths.filter(function (sith) {
fork icon223
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const { Set } = require("immutable");

// Create a new immutable set
const set1 = Set(["a", "b", "c"]);

// Create a new set by adding a value to the original set
const set2 = set1.add("d");

console.log(set1.toJS()); // ['a', 'b', 'c']
console.log(set2.toJS()); // ['a', 'b', 'c', 'd']

// Create a new set by removing a value from the original set
const set3 = set2.delete("b");

console.log(set2.toJS()); // ['a', 'b', 'c', 'd']
console.log(set3.toJS()); // ['a', 'c', 'd']

// Create a new set by merging two sets together
const set4 = Set(["c", "d", "e"]);
const set5 = set3.union(set4);

console.log(set3.toJS()); // ['a', 'c', 'd']
console.log(set4.toJS()); // ['c', 'd', 'e']
console.log(set5.toJS()); // ['a', 'c', 'd', 'e']

In this example, we use the immutable module to create and manipulate an immutable.Set. We first create a new Set containing the values 'a', 'b', and 'c'. We then create a new set set2 by adding the value 'd' to the original set set1. We also create a new set set3 by removing the value 'b' from set2 using the delete method. Finally, we create a new set set5 by merging set3 and a new set set4 containing the values 'c', 'd', and 'e' using the union method. The resulting sets demonstrate how immutable.Set can be used to perform set operations and create new sets without modifying the original sets.

115
116
117
118
119
120
121
122
123
124
output.inline = true;
const prefixOutput = this.prefix(output.clone(), actual);
const suffixOutput = this.suffix(output.clone(), actual);
const actualKeys = this.getKeys(actual);
const expectedKeys = this.getKeys(expected);
const keys = new Set([...actualKeys, ...expectedKeys]).toArray();

output.append(prefixOutput).nl(prefixOutput.isEmpty() ? 0 : 1);

if (this.indent) {
fork icon1
star icon2
watch icon0

350
351
352
353
354
355
356
357
358
359
[
  new Set([new Set([1]), new Set([2])]),
  new Set([new Set([1]), new Set([3])]),
],
[Immutable.Set([1, 2]), Immutable.Set()],
[Immutable.Set([1, 2]), Immutable.Set([1, 2, 3])],
[Immutable.OrderedSet([1, 2]), Immutable.OrderedSet([2, 1])],
[new Map([[1, 'one'], [2, 'two']]), new Map([[1, 'one']])],
[new Map([['a', 0]]), new Map([['b', 0]])],
[new Map([['v', 1]]), new Map([['v', 2]])],
fork icon1
star icon0
watch icon0

+ 4 other calls in file

81
82
83
84
85
86
87
88
89
90
91
92
};
exports.QUERY_PARAMS = QUERY_PARAMS;


var getShortQueryParams = function getShortQueryParams() {
  var isParam = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  var uniqKeys = new _immutable.Set();
  var result = {};


  for (var _i = 0, _Object$entries = Object.entries(QUERY_PARAMS); _i < _Object$entries.length; _i++) {
    var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
fork icon0
star icon3
watch icon4

+ 5 other calls in file

214
215
216
217
218
219
220
221
222
223
// interpret anything that renders to a number (like Date, time-sugar) as a timestamp
const touch = typeof options.touch !== 'undefined' ? options.touch.valueOf() : options.touch;
options.touch = typeof touch === 'number' ? new Date(touch) : 
  touch === false ? touch : new Date();

const savedModels = options._savedModels || Immutable.Set()
options._savedModels = savedModels.add(Immutable.Map({ tableName: this.tableName, id: properties.id }))

// Convert everything to something we can put into the database
properties = this._prepareFields(properties);
fork icon0
star icon0
watch icon2

+ 8 other calls in file

386
387
388
389
390
391
392
393
394
395
    return get(s, k)
  }],
  [symbol('conj'), evalConj],
  [symbol('type'), (exp, env) => expType(evalRest(exp, env).first())],
  [sym.do, evalDo],
  ['unquotedForms', im.Set([sym.bind, sym.list, sym.set])],
  ['expTotal', 0],
  [sym.emptyList, emptyList],
  [sym.emptySet, emptySet]
]),
fork icon0
star icon0
watch icon0

686
687
688
689
690
691
692
693
694
  if (data.children && data.children.forEach) {
    data.children.forEach(cid => {
      this._parents = this._parents.set(cid, data.id);
    });
  }
  var curNodes = this._nodesByName.get(data.name) || new Set();
  this._nodesByName = this._nodesByName.set(data.name, curNodes.add(data.id));
  this.emit(data.id);
}
fork icon0
star icon0
watch icon0