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

feat: add SampleBy and SamplesBy #516

Merged
merged 21 commits into from
Jan 26, 2025
Merged

Conversation

bramvandewalle
Copy link
Contributor

Why?

See #512

What?

Added a SampleBy and SamplesBy that accepts an intFunc that is the generator of the indexes. This way, a client of the lo library would be able to provide their own seeded random generator.

Example?

package main

import (
	"fmt"
	"math/rand"

	"github.com/samber/lo"
)

func Example() {
	r := rand.New(rand.NewSource(42))
	collection := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	result := lo.SampleBy(collection, r.Intn)
	results := lo.SamplesBy(collection, len(collection), r.Intn)
	fmt.Printf("Sample: %v\n", result)
	fmt.Printf("Samples: %v\n", results)
}

func main() {
	Example()
}

@bramvandewalle bramvandewalle changed the title Add SampleBy and SamplesBy feat: Add SampleBy and SamplesBy Aug 15, 2024
@bramvandewalle bramvandewalle changed the title feat: Add SampleBy and SamplesBy feat: add SampleBy and SamplesBy Aug 15, 2024
find_test.go Outdated Show resolved Hide resolved
find.go Outdated Show resolved Hide resolved
find.go Outdated Show resolved Hide resolved
find.go Outdated
}

// SampleBy returns a random item from collection, using intFunc as the random index generator.
func SampleBy[T any](collection []T, intFunc func(int) int) T {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why this method doesn't call SamplesBy with count=1 and return the first entry?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also the original implementation of the Sample doesn't use Samples. I didn't want to change any implementation of existing functions. Also... not sure if there's that much of a benefit in doing so?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But if you want me to update it... by all means and I change it!

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's wait for owner's feedback

cc @samber

find.go Outdated Show resolved Hide resolved
find.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.

I posted this, but feel free to merge.

@bramvandewalle bramvandewalle requested a review from samber August 23, 2024 09:13
@bramvandewalle
Copy link
Contributor Author

@ccoVeille I don't have the rights to merge this. Not sure if @samber first needs to approve it?

find.go Outdated Show resolved Hide resolved
find.go Outdated Show resolved Hide resolved
@samber samber merged commit 86ce870 into samber:master Jan 26, 2025
7 checks passed
Copy link

codecov bot commented Jan 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.15%. Comparing base (dc8a175) to head (4e7903d).
Report is 46 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #516      +/-   ##
==========================================
- Coverage   94.33%   94.15%   -0.18%     
==========================================
  Files          17       18       +1     
  Lines        2790     3065     +275     
==========================================
+ Hits         2632     2886     +254     
- Misses        154      168      +14     
- Partials        4       11       +7     
Flag Coverage Δ
unittests 94.15% <100.00%> (-0.18%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

3 participants