I'm working on some NodeJS code that uses many different "lists" of items. The underlying implementation uses including Immutable.List, Immutable.Set, Javascript Array and Javascript Set.
For reasons I won't go into, I would like to be able to loop over these items without regard to the underlying collection.
Some background might be useful. For Array, I can use the following to loop over a Javascript Array.
let anArray = [ new SomeObj(), new SomeObj(), ... ]
for(let idx=0; idx < anArray.length; idx++){
let someObj = anArray[idx];
// ... etc
if (someCondition) break;
}
For the Immutable.List, I need to use the Immutable.List.size property (instead of the length property) and use "Immutable.List.get" function as follows:
const immutable = require('immutable');
let aList = immutable.List([ new SomeObj(), new SomeObj(), ... ]);
for(idx=0; idx < aList.size; idx++){
var item = aList.get(idx);
// ... etc
if (someCondition) break;
}
The looping logic is VERY close except for some small differences.
For those who would suggest the natural answer of using the forEach() method that is available on each of these objects. The normal Javascript Array.forEach() method doesn't handle breaking out of the loop (the Array.some() method is needed for that situation). The Immutable.List is based on a Immutable.Collection.forEach that does support "breaking" out of the loop so this might work but would require wrapping the Javascript objects into a Immutable sequence/collection. I'm new to using the Immutable library so there might be another approach that is well known to those familiar with Immutable.
The approach I'm considering is described next. I'm thinking of a utility function that provides the solution as follows:
for(let idx=0, looper=new Looper(aListOrSet); idx < looper.length(); idx++){
var item = looper.get(idx);
// ... etc
if (someCondition) break;
}
Some things I've looked at include:
- How to loop through Immutable List like forEach
- Immutable.Collection.forEach
- Immutable Sequences - This might be the trick. I would have to convert the Javascript objects to an immutable sequence, but this might work.
Has anyone else tried to solve this, and if so how did they solve this problem?