samber / lo

💥 A Lodash-style Go library based on Go 1.18+ Generics (map, filter, contains, find...)
https://pkg.go.dev/github.com/samber/lo
MIT License
18.02k stars 830 forks source link

feat: add In function #547

Closed thedevsaddam closed 3 weeks ago

thedevsaddam commented 4 weeks ago

Introduce In function for element existence check in a collection

Checks if an element exists in the collection. It returns true if the element is found; otherwise, false.

result := lo.In([]int{1, 2, 3, 4, 5}, 4)
// true

result = lo.In([]string{"a", "b"}, "c")
// false

Note: This can be done using existing lo.Count([]int{1, 5, 1}, 1)>0 or using lo.CountBy

The In function offers several advantages:

SnowOnion commented 4 weeks ago

lo contains lo.Contains already. ^_^ lo.Contains :: func[T comparable](collection []T, element T) bool

Since go1.21, stdlib slices is added and it contains Contains, too. slices.Contains :: func[S ~[]E, E comparable](s S, v E) bool

[2024-11-01 09 21 36](https://godoogle.sonion.xyz/search?q=%5BT+comparable%5Dfunc%28collection+%5B%5DT%2C+needle+T%29+bool)

https://godoogle.sonion.xyz/search?q=%5BT+comparable%5Dfunc%28collection+%5B%5DT%2C+needle+T%29+bool

thedevsaddam commented 3 weeks ago

Thank you for the feedback! Given that similar functionality already exists, I'll go ahead and close this PR. I appreciate the insights!