-
Notifications
You must be signed in to change notification settings - Fork 7
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
lib: Extract keys and values from map #46
Conversation
"good_instance": [ | ||
[ | ||
"aa", | ||
"ab" | ||
], | ||
[ | ||
2, | ||
3 | ||
], | ||
{ | ||
"cc": 4, | ||
"cd": 5 | ||
} | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CI is failing because map doesn't maintain order when iterating using range
. Any workarounds to make the tests pass?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a nice property to have would be to make zip(map_keys(m), map_values(m))
return the original map (well, a copy of it). This currently does not happen, but if it did, this test would pass. It adds an allocation cost to the map_values
call and a sort CPU cost to both, but I think it is worth it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added new test into zip to test zip(map_keys(m), map_values(m))
lib/collections.go
Outdated
type kv struct { | ||
Key ref.Val | ||
Value ref.Val | ||
} | ||
if mapK.Size() != types.IntZero { | ||
canSort := true | ||
it := mapK.Iterator() | ||
ss := make([]kv, 0, n) | ||
for it.HasNext() == types.True { | ||
k := it.Next() | ||
v := mapK.Get(k) | ||
ss = append(ss, kv{k, v}) | ||
_, ok := k.(traits.Comparer) | ||
if !ok { | ||
canSort = false | ||
} | ||
} | ||
if canSort { | ||
sort.Slice(ss, func(i, j int) bool { | ||
return ss[i].Key.(traits.Comparer).Compare(ss[j].Key) == types.Int(-1) | ||
}) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type kv struct { | |
Key ref.Val | |
Value ref.Val | |
} | |
if mapK.Size() != types.IntZero { | |
canSort := true | |
it := mapK.Iterator() | |
ss := make([]kv, 0, n) | |
for it.HasNext() == types.True { | |
k := it.Next() | |
v := mapK.Get(k) | |
ss = append(ss, kv{k, v}) | |
_, ok := k.(traits.Comparer) | |
if !ok { | |
canSort = false | |
} | |
} | |
if canSort { | |
sort.Slice(ss, func(i, j int) bool { | |
return ss[i].Key.(traits.Comparer).Compare(ss[j].Key) == types.Int(-1) | |
}) | |
} | |
type valComparer interface { | |
ref.Val | |
traits.Comparer | |
} | |
type kv struct { | |
Key valComparer | |
Value ref.Val | |
} | |
if mapK.Size() != types.IntZero { | |
canSort := true | |
it := mapK.Iterator() | |
ss := make([]kv, 0, n) | |
for it.HasNext() == types.True { | |
k := it.Next() | |
v := mapK.Get(k) | |
ck, ok := k.(valComparer) | |
if !ok { | |
canSort = false | |
} | |
ss = append(ss, kv{ck, v}) | |
} | |
if canSort { | |
sort.Slice(ss, func(i, j int) bool { | |
return ss[i].Key.Compare(ss[j].Key) == types.Int(-1) | |
}) | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks for the recommendation here. Updated
This adds a 2 functions to collections, namely
keys
andvalues
that can be applied to amap
and extract keys and values respectively into alist
.