frptools / collectable

High-performance immutable data structures for modern JavaScript and TypeScript applications. Functional interfaces, deep/composite operations API, mixed mutability API, TypeScript definitions, ES2015 module exports.
MIT License
273 stars 14 forks source link

For Each HashMap Item #64

Closed RauppRafael closed 6 years ago

RauppRafael commented 6 years ago

I have a map of type: HashMapStructure<string, number>

How can I iterate through it?

Can't find any docs or intructions.

axefrog commented 6 years ago

Hey there, yeah the docs are lacking at this point. There are a few ways you can iterate through entries:

import * as HashMap from '@collectable/map';

const map = getMyHashMapSomehow();

// The HashMap structure actually implements [Symbol.iterator]:

for (let [key, value] of map) {
  // ...
}

// The above internally just makes the following call:

const iterator = HashMap.entries(map);
const first = iterator.next();
const [key, value] = first.value;

// There are also methods for keys and values:

const keysIterator = HashMap.keys(map);
const valuesIterator = HashMap.values(map);

// And of course you can always make an array out of any of these:

const keys = Array.from(HashMap.keys(map));
const values = Array.from(HashMap.values(map));
const entries = Array.from(map);
axefrog commented 6 years ago

For a list of available functions and their signatures, check out: https://github.com/frptools/collectable/tree/master/packages/map/src/functions

RauppRafael commented 6 years ago

If I try to do:

for (let [key, value] of map) {
      console.log(key);
}

I get this error:

error TS2495: Type 'HashMapStructure<string, number>' is not an array type or a string type.

Therefore the iteration won't work.

axefrog commented 6 years ago

Oh, that's TypeScript being lame. It is actually valid, but TypeScript is failing to recognise the [Symbol.iterator] property. I'm not sure what your compiler settings are, but maybe this will help. Alternatively, try casting it to any and then to Array<[K, V]> where K and V are your key and value types.

axefrog commented 6 years ago

I just ran the following quick tests in JavaScript and had no issues:

// Test when constructing from an array

const map1 = HashMap.fromArray([
  ['foo', 10],
  ['bar', 20],
  ['baz', 30],
]);

for (let [key, value] of map1) {
  console.log(key, value);
}

// Test when constructing from an object

const map2 = HashMap.fromObject({
  foo: 10,
  bar: 20,
  baz: 30,
});

for (let [key, value] of map2) {
  console.log(key, value);
}

// Test when adding items manually

let map3 = HashMap.empty();
map3 = HashMap.set('foo', 10, map3);
map3 = HashMap.set('bar', 20, map3);
map3 = HashMap.set('baz', 30, map3);

for (let [key, value] of map3) {
  console.log(key, value);
}

// Test adding a lot of items

const map4 = HashMap.empty(true);
for (let i = 0; i < 1000; i++) {
  HashMap.set(i, `#${i}`, map4);
}

for (let [key, value] of map4) {
  console.log(key, value);
}
axefrog commented 6 years ago

Closing this as it appears to be a non-issue.