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

Keys+Values: accept multiple maps (vaargs) - Adding UniqKeys+UniqValues #503

Merged
merged 24 commits into from
Jul 29, 2024

Conversation

shivamrazorpay
Copy link
Contributor

@shivamrazorpay shivamrazorpay commented Jul 19, 2024

Resolved Issue : #452

Added :

  • Accept Multiple maps as vaargs in Keys
  • Accept Multiple maps as vaargs in Values
  • Added UniqKeys which creates an array of unique map keys.
  • Added UniqValues which creates an array of unique map values.
  • Added Test for the same

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding is not only about thinking how to code the things you need to add for this feature, but to think about all the use cases your additional features could lead to.

map.go Show resolved Hide resolved
map.go Show resolved Hide resolved
map_test.go Show resolved Hide resolved
Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍 thanks

Let's wait for @samber feedback now

@samber
Copy link
Owner

samber commented Jul 21, 2024

According to your changes, you're doing 2 operations: list keys and dedup.

Can you argue on this behavior?

IMO, some developers would only need to list keys, eg: lo.CountValues(lo.Keys(a, b))

Others might want to list keys with deduplication, but it could be done with 2 methods:

  • lo.Uniq(lo.Keys(a, b))
  • or a faster implem: lo.UniqKeys(a, b)

Also, I'm thinking about a very similar helper: lo.Values. Would you dedup values? I don't think so.

So to push this work forward, I would suggest the following:

  • vaarg on lo.Keys without dedup
  • a new helper lo.UniqKeys
  • vaarg on lo.Values without dedup
  • a new helper lo.UniqValues

Any ideas on the matter?

@shivamrazorpay
Copy link
Contributor Author

@samber seems like a fair callout (I was also thinking along the same lines) and also a good suggestion. I will make the 4 changes you have asked and will ask you to review them. Thank you for your suggestion.

@samber
Copy link
Owner

samber commented Jul 21, 2024

Sure. I'm also curious about other developer's opinions on this.

@ccoVeille
Copy link
Contributor

I'm fine with the changes @samber suggest

I just want to make sure the code will be benchmarked against slices.Compact when using in-house dedup once you will get the keys or values

@shivamrazorpay
Copy link
Contributor Author

@samber @ccoVeille added the required changes.
can you please review it? 🙏

README.md Outdated
@@ -1039,14 +1041,39 @@ result = lo.Splice([]string{"a", "b"}, 42, "1", "2")
### Keys

Creates an array of the map keys.
<br />
(Note: The order of the keys is not guaranteed to be the same as the order returned by the map,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
map.go Outdated Show resolved Hide resolved
map.go Outdated Show resolved Hide resolved
map.go Outdated Show resolved Hide resolved
map_example_test.go Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
map.go Outdated
Comment on lines 25 to 27
if _, exists := seen[k]; !exists {
seen[k] = struct{}{}
result = append(result, k)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if _, exists := seen[k]; !exists {
seen[k] = struct{}{}
result = append(result, k)
_, exists := seen[k]
if exists {
continue
}
seen[k] = struct{}{}
result = append(result, k)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ccoVeille why is this change much better ?? Not able to understand. Is using continue a better approach ??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the logic of "leave early", it's for readability and maintainability.

First, here, it's simpler to read that when existing we skip, than reading than when no existing we have to do something.

The leave early concept is there to avoid big if branch.

Also, imagine you have an new condition in this loop

With your current code, it would lead to put an if in the if

So the code would be something like this

for {
    if condition1 {
         something1
         if condition2 {
               something2 
         }
    }
}

Here is the same code when applying leave early

for {
    if !condition1 {
         continue
    }
    something1
    if !condition2 {
          continue
    }
    something2 
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waaaooo this seems a very good explanation. Thank you making it soo clear

@shivamrazorpay
Copy link
Contributor Author

@samber can you review the PR ?? all the review comments are resolved.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@samber
Copy link
Owner

samber commented Jul 26, 2024

Please check the tests.

@shivamrazorpay
Copy link
Contributor Author

@samber Sorry my bad. There I was asserting the 3rd test and sorted the 1st test slice.
Fixed it. I checked all the tests are now working.

@ccoVeille
Copy link
Contributor

That's why I'm using a pattern I barely seen here.

func TestWhatever(t *testing.T) {


    t.Run("one test", func(t *testing.T) {
          // …
    })

    t.Run("another test", func(t *testing.T) {
          // …
    })

   t.Run("and so on", func(t *testing.T) {
          // …
    })
}

@samber samber changed the title Added Keys: accept multiple maps (vaargs) - [Issue #452] Keys+Values: accept multiple maps (vaargs) - Adding UniqKeys+UniqValues Jul 29, 2024
@samber samber merged commit d8e5707 into samber:master Jul 29, 2024
7 checks passed
map.go Show resolved Hide resolved

// UniqKeys creates an array of unique keys in the map.
// Play: https://go.dev/play/p/TPKAb6ILdHk
func UniqKeys[K comparable, V any](in ...map[K]V) []K {
Copy link
Contributor

@d-enk d-enk Aug 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be more effective

seen := make(map[K]struct{})
	
for i := range in {
  for k := range in[i] {
	seen[k] = struct{}{}
  }
}

result := make([]K, 0, len(seen))

for k := range seen {
	result = append(result, k)
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly UniqValues

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with this version, the order is not guarantee

IMO, we would need a new helper (UniqKeysUnordered) ?

mx-psi referenced this pull request in open-telemetry/opentelemetry-collector-contrib Aug 13, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/samber/lo](https://togithub.com/samber/lo) | `v1.46.0` ->
`v1.47.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fsamber%2flo/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fsamber%2flo/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fsamber%2flo/v1.46.0/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fsamber%2flo/v1.46.0/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>samber/lo (github.com/samber/lo)</summary>

### [`v1.47.0`](https://togithub.com/samber/lo/releases/tag/v1.47.0)

[Compare
Source](https://togithub.com/samber/lo/compare/v1.46.0...v1.47.0)

#### What's Changed

- feat: Improve Substring by
[@&#8203;liujundezhanghao](https://togithub.com/liujundezhanghao) in
[https://github.com/samber/lo/pull/496](https://togithub.com/samber/lo/pull/496)
- doc: Wrong method reference and output in readme by
[@&#8203;ColeZia](https://togithub.com/ColeZia) in
[https://github.com/samber/lo/pull/497](https://togithub.com/samber/lo/pull/497)
- doc: Fix documentation for Duration3 by
[@&#8203;gecko655](https://togithub.com/gecko655) in
[https://github.com/samber/lo/pull/502](https://togithub.com/samber/lo/pull/502)
- feat: add FromSlicePtr by [@&#8203;mash](https://togithub.com/mash) in
[https://github.com/samber/lo/pull/217](https://togithub.com/samber/lo/pull/217)
- feat: adding FromSlicePtrOr by
[@&#8203;samber](https://togithub.com/samber) in
[https://github.com/samber/lo/pull/506](https://togithub.com/samber/lo/pull/506)
- feat: Keys+Values: accept multiple maps (vaargs) - Adding
UniqKeys+UniqValues by
[@&#8203;shivamrazorpay](https://togithub.com/shivamrazorpay) in
[https://github.com/samber/lo/pull/503](https://togithub.com/samber/lo/pull/503)
- doc: Update foreachwhile readme.md by
[@&#8203;Sianao](https://togithub.com/Sianao) in
[https://github.com/samber/lo/pull/508](https://togithub.com/samber/lo/pull/508)

#### New Contributors

- [@&#8203;liujundezhanghao](https://togithub.com/liujundezhanghao) made
their first contribution in
[https://github.com/samber/lo/pull/496](https://togithub.com/samber/lo/pull/496)
- [@&#8203;ColeZia](https://togithub.com/ColeZia) made their first
contribution in
[https://github.com/samber/lo/pull/497](https://togithub.com/samber/lo/pull/497)
- [@&#8203;gecko655](https://togithub.com/gecko655) made their first
contribution in
[https://github.com/samber/lo/pull/502](https://togithub.com/samber/lo/pull/502)
- [@&#8203;mash](https://togithub.com/mash) made their first
contribution in
[https://github.com/samber/lo/pull/217](https://togithub.com/samber/lo/pull/217)
- [@&#8203;shivamrazorpay](https://togithub.com/shivamrazorpay) made
their first contribution in
[https://github.com/samber/lo/pull/503](https://togithub.com/samber/lo/pull/503)

**Full Changelog**:
samber/lo@v1.46.0...v1.47.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on tuesday" (UTC), Automerge - At any
time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/open-telemetry/opentelemetry-collector-contrib).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yNi4xIiwidXBkYXRlZEluVmVyIjoiMzguMjYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIiwicmVub3ZhdGVib3QiXX0=-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
github-actions bot referenced this pull request in kairos-io/provider-kairos Aug 13, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/samber/lo](https://togithub.com/samber/lo) | `v1.46.0` ->
`v1.47.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fsamber%2flo/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fsamber%2flo/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fsamber%2flo/v1.46.0/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fsamber%2flo/v1.46.0/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

### Release Notes

<details>
<summary>samber/lo (github.com/samber/lo)</summary>

### [`v1.47.0`](https://togithub.com/samber/lo/releases/tag/v1.47.0)

[Compare
Source](https://togithub.com/samber/lo/compare/v1.46.0...v1.47.0)

#### What's Changed

- feat: Improve Substring by
[@&#8203;liujundezhanghao](https://togithub.com/liujundezhanghao) in
[https://github.com/samber/lo/pull/496](https://togithub.com/samber/lo/pull/496)
- doc: Wrong method reference and output in readme by
[@&#8203;ColeZia](https://togithub.com/ColeZia) in
[https://github.com/samber/lo/pull/497](https://togithub.com/samber/lo/pull/497)
- doc: Fix documentation for Duration3 by
[@&#8203;gecko655](https://togithub.com/gecko655) in
[https://github.com/samber/lo/pull/502](https://togithub.com/samber/lo/pull/502)
- feat: add FromSlicePtr by [@&#8203;mash](https://togithub.com/mash) in
[https://github.com/samber/lo/pull/217](https://togithub.com/samber/lo/pull/217)
- feat: adding FromSlicePtrOr by
[@&#8203;samber](https://togithub.com/samber) in
[https://github.com/samber/lo/pull/506](https://togithub.com/samber/lo/pull/506)
- feat: Keys+Values: accept multiple maps (vaargs) - Adding
UniqKeys+UniqValues by
[@&#8203;shivamrazorpay](https://togithub.com/shivamrazorpay) in
[https://github.com/samber/lo/pull/503](https://togithub.com/samber/lo/pull/503)
- doc: Update foreachwhile readme.md by
[@&#8203;Sianao](https://togithub.com/Sianao) in
[https://github.com/samber/lo/pull/508](https://togithub.com/samber/lo/pull/508)

#### New Contributors

- [@&#8203;liujundezhanghao](https://togithub.com/liujundezhanghao) made
their first contribution in
[https://github.com/samber/lo/pull/496](https://togithub.com/samber/lo/pull/496)
- [@&#8203;ColeZia](https://togithub.com/ColeZia) made their first
contribution in
[https://github.com/samber/lo/pull/497](https://togithub.com/samber/lo/pull/497)
- [@&#8203;gecko655](https://togithub.com/gecko655) made their first
contribution in
[https://github.com/samber/lo/pull/502](https://togithub.com/samber/lo/pull/502)
- [@&#8203;mash](https://togithub.com/mash) made their first
contribution in
[https://github.com/samber/lo/pull/217](https://togithub.com/samber/lo/pull/217)
- [@&#8203;shivamrazorpay](https://togithub.com/shivamrazorpay) made
their first contribution in
[https://github.com/samber/lo/pull/503](https://togithub.com/samber/lo/pull/503)

**Full Changelog**:
samber/lo@v1.46.0...v1.47.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "after 11pm every weekday,before 7am
every weekday,every weekend" in timezone Europe/Brussels, Automerge - At
any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/kairos-io/provider-kairos).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yNi4xIiwidXBkYXRlZEluVmVyIjoiMzguMjYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
f7o referenced this pull request in f7o/opentelemetry-collector-contrib Sep 12, 2024
[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [github.com/samber/lo](https://togithub.com/samber/lo) | `v1.46.0` ->
`v1.47.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fsamber%2flo/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fsamber%2flo/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fsamber%2flo/v1.46.0/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fsamber%2flo/v1.46.0/v1.47.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### Release Notes

<details>
<summary>samber/lo (github.com/samber/lo)</summary>

### [`v1.47.0`](https://togithub.com/samber/lo/releases/tag/v1.47.0)

[Compare
Source](https://togithub.com/samber/lo/compare/v1.46.0...v1.47.0)

#### What's Changed

- feat: Improve Substring by
[@&open-telemetry#8203;liujundezhanghao](https://togithub.com/liujundezhanghao) in
[https://github.com/samber/lo/pull/496](https://togithub.com/samber/lo/pull/496)
- doc: Wrong method reference and output in readme by
[@&open-telemetry#8203;ColeZia](https://togithub.com/ColeZia) in
[https://github.com/samber/lo/pull/497](https://togithub.com/samber/lo/pull/497)
- doc: Fix documentation for Duration3 by
[@&open-telemetry#8203;gecko655](https://togithub.com/gecko655) in
[https://github.com/samber/lo/pull/502](https://togithub.com/samber/lo/pull/502)
- feat: add FromSlicePtr by [@&open-telemetry#8203;mash](https://togithub.com/mash) in
[https://github.com/samber/lo/pull/217](https://togithub.com/samber/lo/pull/217)
- feat: adding FromSlicePtrOr by
[@&open-telemetry#8203;samber](https://togithub.com/samber) in
[https://github.com/samber/lo/pull/506](https://togithub.com/samber/lo/pull/506)
- feat: Keys+Values: accept multiple maps (vaargs) - Adding
UniqKeys+UniqValues by
[@&open-telemetry#8203;shivamrazorpay](https://togithub.com/shivamrazorpay) in
[https://github.com/samber/lo/pull/503](https://togithub.com/samber/lo/pull/503)
- doc: Update foreachwhile readme.md by
[@&open-telemetry#8203;Sianao](https://togithub.com/Sianao) in
[https://github.com/samber/lo/pull/508](https://togithub.com/samber/lo/pull/508)

#### New Contributors

- [@&open-telemetry#8203;liujundezhanghao](https://togithub.com/liujundezhanghao) made
their first contribution in
[https://github.com/samber/lo/pull/496](https://togithub.com/samber/lo/pull/496)
- [@&open-telemetry#8203;ColeZia](https://togithub.com/ColeZia) made their first
contribution in
[https://github.com/samber/lo/pull/497](https://togithub.com/samber/lo/pull/497)
- [@&open-telemetry#8203;gecko655](https://togithub.com/gecko655) made their first
contribution in
[https://github.com/samber/lo/pull/502](https://togithub.com/samber/lo/pull/502)
- [@&open-telemetry#8203;mash](https://togithub.com/mash) made their first
contribution in
[https://github.com/samber/lo/pull/217](https://togithub.com/samber/lo/pull/217)
- [@&open-telemetry#8203;shivamrazorpay](https://togithub.com/shivamrazorpay) made
their first contribution in
[https://github.com/samber/lo/pull/503](https://togithub.com/samber/lo/pull/503)

**Full Changelog**:
samber/lo@v1.46.0...v1.47.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "on tuesday" (UTC), Automerge - At any
time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR was generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View the
[repository job
log](https://developer.mend.io/github/open-telemetry/opentelemetry-collector-contrib).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOC4yNi4xIiwidXBkYXRlZEluVmVyIjoiMzguMjYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiZGVwZW5kZW5jaWVzIiwicmVub3ZhdGVib3QiXX0=-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: opentelemetrybot <107717825+opentelemetrybot@users.noreply.github.com>
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

Successfully merging this pull request may close these issues.

4 participants