a type cannot be both Map and List #133

Closed
opened 2019-12-27 06:53:30 +00:00 by kahing · 1 comment
kahing commented 2019-12-27 06:53:30 +00:00 (Migrated from github.com)

because both Map and List define a Get with different parameters. Ideally I want something like:

m := TypeThatIsBothMapAndList.New()
m.Put("foo", 4)
m.Add(Pair.New("foo", 4))
pair := m.At(0)
fmt.Println(pair.First, pair.Second)

Use case is I want to have a different LinkedHashMap that's not only enumerable, but also allow random access to the index. It makes sense for this hypothetical map to also implement List, but right now that's not possible.

because both `Map` and `List` define a `Get` with different parameters. Ideally I want something like: ``` m := TypeThatIsBothMapAndList.New() m.Put("foo", 4) m.Add(Pair.New("foo", 4)) pair := m.At(0) fmt.Println(pair.First, pair.Second) ``` Use case is I want to have a different `LinkedHashMap` that's not only `enumerable`, but also allow random access to the index. It makes sense for this hypothetical map to also implement `List`, but right now that's not possible.
emirpasic commented 2020-02-07 13:09:13 +00:00 (Migrated from github.com)

@kahing not sure if I understood, but map and list are different in many aspects. If you would like to treat the keys as a list, i.e. ordered collection (list) by insertion order, you can use the LinkedHashMap. So that would be insertion-ordering, otherwise, I am not sure what the first or n-th element is in a map? If you look at Golang's native map, the keys are shuffled around when you iterate on the keys, i.e. seemingly random ordering... and they have done that for a good reason.

For the specific use case you mentioned, perhaps you can achieve that simply by using two data structures. One is a map and the other one is a list, at least that is exactly how such data structure would be implemented. Then you either use the map or list depending on the method of retrieval (get-by-key or get-by-index).

It would help if you could point me out to a data structure (can be in another language) that has this functionality that you are asking for, so I can get ideas from there... if at least, how to name it :D

@kahing not sure if I understood, but map and list are different in many aspects. If you would like to treat the keys as a list, i.e. ordered collection (list) by insertion order, you can use the LinkedHashMap. So that would be insertion-ordering, otherwise, I am not sure what the first or n-th element is in a map? If you look at Golang's native map, the keys are shuffled around when you iterate on the keys, i.e. seemingly random ordering... and they have done that for a good reason. For the specific use case you mentioned, perhaps you can achieve that simply by using two data structures. One is a map and the other one is a list, at least that is exactly how such data structure would be implemented. Then you either use the map or list depending on the method of retrieval (get-by-key or get-by-index). It would help if you could point me out to a data structure (can be in another language) that has this functionality that you are asking for, so I can get ideas from there... if at least, how to name it :D
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
golibs/gms-gods#133
No description provided.