mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-10 22:31:32 +02:00
fix: add a uniq method to arrays (#88)
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
32
array/generic/uniq.go
Normal file
32
array/generic/uniq.go
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package generic
|
||||||
|
|
||||||
|
import F "github.com/IBM/fp-go/function"
|
||||||
|
|
||||||
|
// StrictUniq converts an array of arbitrary items into an array or unique items
|
||||||
|
// where uniqueness is determined by the built-in uniqueness constraint
|
||||||
|
func StrictUniq[AS ~[]A, A comparable](as AS) AS {
|
||||||
|
return Uniq[AS](F.Identity[A])(as)
|
||||||
|
}
|
||||||
|
|
||||||
|
// uniquePredUnsafe returns a predicate on a map for uniqueness
|
||||||
|
func uniquePredUnsafe[PRED ~func(A) K, A any, K comparable](f PRED) func(int, A) bool {
|
||||||
|
lookup := make(map[K]bool)
|
||||||
|
return func(_ int, a A) bool {
|
||||||
|
k := f(a)
|
||||||
|
_, has := lookup[k]
|
||||||
|
if has {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
lookup[k] = true
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uniq converts an array of arbitrary items into an array or unique items
|
||||||
|
// where uniqueness is determined based on a key extractor function
|
||||||
|
func Uniq[AS ~[]A, PRED ~func(A) K, A any, K comparable](f PRED) func(as AS) AS {
|
||||||
|
return func(as AS) AS {
|
||||||
|
// we need to create a new predicate for each iteration
|
||||||
|
return filterWithIndex(as, uniquePredUnsafe(f))
|
||||||
|
}
|
||||||
|
}
|
17
array/uniq.go
Normal file
17
array/uniq.go
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package array
|
||||||
|
|
||||||
|
import (
|
||||||
|
G "github.com/IBM/fp-go/array/generic"
|
||||||
|
)
|
||||||
|
|
||||||
|
// StrictUniq converts an array of arbitrary items into an array or unique items
|
||||||
|
// where uniqueness is determined by the built-in uniqueness constraint
|
||||||
|
func StrictUniq[A comparable](as []A) []A {
|
||||||
|
return G.StrictUniq[[]A](as)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Uniq converts an array of arbitrary items into an array or unique items
|
||||||
|
// where uniqueness is determined based on a key extractor function
|
||||||
|
func Uniq[A any, K comparable](f func(A) K) func(as []A) []A {
|
||||||
|
return G.Uniq[[]A](f)
|
||||||
|
}
|
14
array/uniq_test.go
Normal file
14
array/uniq_test.go
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package array
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUniq(t *testing.T) {
|
||||||
|
data := From(1, 2, 3, 2, 4, 1)
|
||||||
|
|
||||||
|
uniq := StrictUniq(data)
|
||||||
|
assert.Equal(t, From(1, 2, 3, 4), uniq)
|
||||||
|
}
|
@@ -19,13 +19,13 @@ import (
|
|||||||
G "github.com/IBM/fp-go/iterator/stateless/generic"
|
G "github.com/IBM/fp-go/iterator/stateless/generic"
|
||||||
)
|
)
|
||||||
|
|
||||||
// StrictUniq converts an [Iterator] or arbitrary items into an [Iterator] or unique items
|
// StrictUniq converts an [Iterator] of arbitrary items into an [Iterator] or unique items
|
||||||
// where uniqueness is determined by the built-in uniqueness constraint
|
// where uniqueness is determined by the built-in uniqueness constraint
|
||||||
func StrictUniq[A comparable](as Iterator[A]) Iterator[A] {
|
func StrictUniq[A comparable](as Iterator[A]) Iterator[A] {
|
||||||
return G.StrictUniq[Iterator[A]](as)
|
return G.StrictUniq[Iterator[A]](as)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uniq converts an [Iterator] or arbitrary items into an [Iterator] or unique items
|
// Uniq converts an [Iterator] of arbitrary items into an [Iterator] or unique items
|
||||||
// where uniqueness is determined based on a key extractor function
|
// where uniqueness is determined based on a key extractor function
|
||||||
func Uniq[A any, K comparable](f func(A) K) func(as Iterator[A]) Iterator[A] {
|
func Uniq[A any, K comparable](f func(A) K) func(as Iterator[A]) Iterator[A] {
|
||||||
return G.Uniq[Iterator[A], K](f)
|
return G.Uniq[Iterator[A], K](f)
|
||||||
|
Reference in New Issue
Block a user