Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For Each HashMap Item #64

Closed
RauppRafael opened this issue Dec 3, 2017 · 6 comments
Closed

For Each HashMap Item #64

RauppRafael opened this issue Dec 3, 2017 · 6 comments

Comments

@RauppRafael
Copy link

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

How can I iterate through it?

Can't find any docs or intructions.

@axefrog
Copy link
Member

axefrog commented Dec 3, 2017

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
Copy link
Member

axefrog commented Dec 3, 2017

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

@RauppRafael
Copy link
Author

RauppRafael commented Dec 3, 2017

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
Copy link
Member

axefrog commented Dec 3, 2017

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
Copy link
Member

axefrog commented Dec 3, 2017

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 axefrog added the wontfix label Mar 7, 2018
@axefrog
Copy link
Member

axefrog commented Mar 7, 2018

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

@axefrog axefrog closed this as completed Mar 7, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants