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

MultiMap.from broken #174

Open
andreasdamm opened this issue Jan 25, 2022 · 4 comments
Open

MultiMap.from broken #174

andreasdamm opened this issue Jan 25, 2022 · 4 comments

Comments

@andreasdamm
Copy link

When using MultiMap.from to create a MultiMap from an iterable of tuples or one-attribute-objects, the index of the item from the iterable is used as the key into the map instead of the first member of the tuple, or the attribute name of the one-attribute-object.

MultiMap.from([['a', 'aa']], Set) currently results in Map(1) { 0 => Set(1) { [ 'a', 'aa' ] } }
MultiMap.from([{a: 'aa'}], Set) currently results in Map(1) { 0 => Set(1) { { a: 'aa' } } }

The desired output should be Map(1) { 'a' => Set(1) { 'aa' } }

@Yomguithereal
Copy link
Owner

Hello @andreasdamm,

MultiMap.from actually works as intended because it follows the standard used throughout the library regarding mixed iteration over potentially key-valued containers. But it does not follow JS Map constructor's rationale indeed because it does not make sense in a broader perspective. This said I can add another static method called #.fromEntries tailored to handle this specific use case as it makes sense in relation with JS Map if you want. It would handle 2-tuples as you wish it to. But it would not handle single attribute object as it does not make sense and because a JS Map does not understand those either.

@andreasdamm
Copy link
Author

Would the declaration file for MultiMap () multi-map.d.ts) have to be changed to reflect the intended behavior? Right now it is

interface MultiMapConstructor {
  new <K, V>(container: SetConstructor): MultiMap<K, V, Set<V>>;
  new <K, V>(container?: ArrayConstructor): MultiMap<K, V, V[]>;

  from<K, V>(
    iterable: Iterable<[K, V]> | {[key: string]: V},
    Container: SetConstructor
  ): MultiMap<K, V, Set<V>>;
  from<K, V>(
    iterable: Iterable<[K, V]> | {[key: string]: V},
    Container?: ArrayConstructor
  ): MultiMap<K, V, V[]>;
}

which indicates that the first member of the tuple becomes the key, and the second one the value.

@Yomguithereal
Copy link
Owner

The type declaration is probably wrong indeed. I have better types suited to this kind of thing in obliterator. I will fix that and add #.fromEntries in the meantime.

@andreasdamm
Copy link
Author

There still doesn't appear to be a good way to create a MultiMap from a list of entry tuples, other then creating an empty map and then iterating through the tuples in a loop calling MultiMap.set. How would oblitarator help?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants