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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from

Conversation

bramvandewalle
Copy link

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
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
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 Outdated 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
Author

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

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