How to use the List function from immutable

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

immutable.List is a data structure in the Immutable.js library that represents an ordered list of values.

109
110
111
112
113
114
115
116
117
  setDeep: (obj, key1, key2, val) => obj.setIn([key1, key2], val),
  getIn: (obj, path) => obj.getIn(path),
  setIn: (obj, path, val) => obj.setIn(path, val),
  merge: (obj1, obj2) => obj1.merge(obj2),
  mergeDeep: (obj1, obj2) => obj1.mergeDeep(obj2),
  initArr: () => Immutable.List(INITIAL_ARRAY), // shallow
  getAt: (arr, idx) => arr.get(idx),
  setAt: (arr, idx, val) => arr.set(idx, val),
};
fork icon23
star icon265
watch icon8

+ 13 other calls in file

26
27
28
29
30
31
32
33
34
35
例

```
const { List, Set } = require('immutable')

const emptyList = List()
// List []

const plainArray = [ 1, 2, 3, 4 ]
const listFromPlainArray = List(plainArray)
fork icon8
star icon31
watch icon1

+ 29 other calls in file

How does immutable.List work?

immutable.List is a collection of ordered values that can be accessed by index, where each List instance represents a single immutable list of values that can be manipulated and updated without affecting the original list. It is implemented using a specialized data structure called a persistent vector, which allows for efficient, memory-efficient updates and operations on large lists. Each update to a List instance returns a new instance, allowing for safe and easy parallelism and avoiding unexpected changes to data.

169
170
171
172
173
174
175
176
177
178
* const plainSet = Set([ 1, 2, 3, 4 ])
* const listFromPlainSet = List(plainSet)
* // List [ 1, 2, 3, 4 ]
*
* const arrayIterator = plainArray[Symbol.iterator]()
* const listFromCollectionArray = List(arrayIterator)
* // List [ 1, 2, 3, 4 ]
*
* listFromPlainArray.equals(listFromCollectionArray) // true
* listFromPlainSet.equals(listFromCollectionArray) // true
fork icon5
star icon30
watch icon2

+ 129 other calls in file

5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
    return state.set("diceCount", state.diceCount - 1).update("roll", function (r) {
      return r.pop();
    });
  },
  updateRoll: function updateRoll(state, roll) {
    return state.set("roll", new _immutable.List(roll));
  }
};


// Helper function to perform a roll. The random generation must be done
fork icon2
star icon17
watch icon0

+ 13 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const { List } = require("immutable");

// Creating a new List
const myList = List([1, 2, 3, 4]);

// Adding a new element to the List
const newList = myList.push(5);

// Removing an element from the List
const finalList = newList.delete(0);

console.log(finalList.toArray()); // Output: [2, 3, 4, 5]

In this example, we create a new List containing four numbers, then add a new number to the end using the push method. Finally, we remove the first number from the list using the delete method, and output the resulting list as an array using the toArray method.

15
16
17
18
19
20
21
22
23
24

    default: ''
  });
}

var emptyList = Immutable.List();

function Collection(child, validator) {
  if (validator === void 0) {
    validator = alwaysValid;
fork icon0
star icon8
watch icon3

6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
},{"./mutations":7,"./state":9,"immutable":4}],9:[function(require,module,exports){
var derivable_1 = require('derivable');
var immutable_1 = require('immutable');
exports.$AppState = derivable_1.atom({
    world: null,
    sithIDs: immutable_1.List([
        null,
        null,
        3616,
        null,
fork icon223
star icon0
watch icon0

121
122
123
124
125
126
127
128
129
130
if (!isImmutableStructure(this.current) || !this.current) {
  this.current = Immutable.fromJS(this.current || {});
}

if (!!options.history) {
  this.history = Immutable.List.of(this.current);
  this._currentRevision = 0;
  this._historyLimit = (typeof options.historyLimit === 'number') ?
    options.historyLimit :
    Infinity;
fork icon21
star icon374
watch icon0

21
22
23
24
25
26
27
28
29
30
  );
}
if ('data' in payload) {
  nextState = nextState.setIn(
    ['collections', targetName, 'data'],
    List(data)
  );
}
if ('info' in payload) {
  nextState = nextState.setIn(['collections', targetName, 'info'], info);
fork icon8
star icon0
watch icon2

177
178
179
180
181
182
183
184
185
186
 * listFromPlainArray.equals(listFromCollectionArray) // true
 * listFromPlainSet.equals(listFromCollectionArray) // true
 * listFromPlainSet.equals(listFromPlainArray) // true
 * ```
 */
export function List(): List<unknown>;
export function List<T>(): List<T>;
export function List<T>(collection: Iterable<T>): List<T>;

export interface List<T> extends Collection.Indexed<T> {
fork icon5
star icon23
watch icon2

+ 25 other calls in file

44
45
46
47
48
49
50
51
52
53
        });
}

function collection(uri) {
    const route = myro({ [uri]: 'uri' });
    let data = new Immutable.List();
    let req = null;
    return {
        load() {
            if (req) cancelRequest(req);
fork icon4
star icon4
watch icon10

7
8
9
10
11
12
13
14
15
16
17
18


var immutable = require('immutable');
var fromJS = immutable.fromJS;
var Map = immutable.Map;
var Record = immutable.Record;
var List = immutable.List;


var normalizr = require('normalizr');
var normalize = normalizr.normalize;
var Schema = normalizr.Schema;
fork icon3
star icon9
watch icon2

+ 8 other calls in file

117
118
119
120
121
122
123
124
125
126
  ReactDOM.render(<TodoApp state={state} />, mountNode);
}

// Default initial structure as immutable data.
var structure = immutable.Map({
  items: immutable.List(),
  text: ''
});
// Render out initial application
render(structure);
fork icon55
star icon0
watch icon36

+ 5 other calls in file

24
25
26
27
28
29
30
31
32
33
34
35
  }
  return syms[name]
}


const makeSet = (...xs) => im.Map(xs)
const makeList = (...xs) => im.List(xs)
const makeForm = (name, ...args) => makeList(sym(name), ...args)


const is = im.is
const isSym = x => x instanceof Sym
fork icon0
star icon3
watch icon1

18
19
20
21
22
23
24
25
26
27
```

## Lists

```js
var list = Immutable.List.of(1, 2)

list
  .push(3, 4, 5)
  .unshift(0)
fork icon0
star icon1
watch icon2

+ 3 other calls in file

9
10
11
12
13
14
15
16
17
18
const user_id = req.session.i7e.get('user_id');
const summary = watersheds.summary(req.app.get('db'), watershed_id);
let reports_by_level = Map();
Range(0, summary.get('progression_level') + 1).reverse().forEach(
  function (level) {
    reports_by_level = reports_by_level.set(level, List());
  });
summary.get('reports').forEach(function (report) {
  reports_by_level = reports_by_level.update(report.get('progression'),
    function (level_list) {
fork icon1
star icon1
watch icon2

568
569
570
571
572
573
574
575
576
577
[
  [1, 2],
  [2, 1],
],
[Immutable.List([1]), Immutable.List([2])],
[Immutable.List([1, 2]), Immutable.List([2, 1])],
[new Map(), new Set()],
[new Set([1, 2]), new Set()],
[new Set([1, 2]), new Set([1, 2, 3])],
[new Set([[1], [2]]), new Set([[1], [2], [3]])],
fork icon0
star icon1
watch icon4

+ 74 other calls in file

388
389
390
391
392
393
394
395
396
397
	var res = '', num = false, srt = {
			true   (a, b) { return by(a) - by(b); },
			false  (a, b) { return by(b) - by(a); },
		}[!!dsc];
	by  = by || function (v) { return (num ? v : v.toString().len); };
	arr = Imm.List(arr).toArray();
	try { res = (arr.length > 1 ? arr.sort(srt) : arr)[0].toString(); } catch (e) { }
	try { num = arr.filter(v=>IaN(v)).length==arr.length; } catch (e) { }
	try { return by(res); } catch (e) { res.length; }
}
fork icon0
star icon1
watch icon2

+ 21 other calls in file

7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611


},{}],6:[function(require,module,exports){
///<reference path="./references.d.ts" />
"use strict";
var immutable_1 = require("immutable");
exports.List = immutable_1.List;
exports.is = immutable_1.is;
var Repliq_1 = require("./Repliq");
exports.Repliq = Repliq_1.Repliq;
var Time_1 = require("./protocols/Time");
fork icon0
star icon1
watch icon0

+ 12 other calls in file

59
60
61
62
63
64
65
66
67
68
69
70
71

*/


describe('scrubbable', () => {


	function reflector(state = Immutable.List(), action){
		return state;
	}


	const simpleReflector = scrubbable(reflector);
fork icon0
star icon1
watch icon0

1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
 * @param {Immutable} entities
 * @param {Immutable} payload
 * @return {Map}
 */
function entitiesMerger(A, B) {
  if (Immutable.List.isList(A) && Immutable.List.isList(B)) {
    return B; // Replace the nested list
  }
  if (A && A.mergeWith) {
    return A.mergeWith(entitiesMerger, B);
fork icon1
star icon0
watch icon2