Lazily interleaving values

Interleaving sequence values is useful when you're looking for a value in more than one sequence. This makes it easy to look in every collection without having to search one collection entirely before moving on to the next one:

const myList1 = List.of(1, 2, 3);const myList2 = List.of(4, 5, 6);const myList3 = List.of(7, 8, 9);console.log('found', myList1  .toSeq()  .interleave(myList2, myList3)  .find((v) => {    console.log('checking', v);    return v === 7;  }));  // -> checking 1  // -> checking 4  // -> checking 7  // -> found 7

As the logging shows, you only have to check the values 1 and 4 before finding the value you're looking for. Because you have interleaved the lists using interleave(), you avoid having to check 2

Get Mastering Immutable.js now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.