From f3579fc142a6c3a827db5c2d8648a0d7bfb47928 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 11 Nov 2024 10:52:29 +0800 Subject: [PATCH] feat: add IndexOf and LastIndexOf for stream --- cryptor/rsa_private_example.pem | 98 ++++++++++++++++----------------- cryptor/rsa_public_example.pem | 24 ++++---- docs/api/packages/stream.md | 70 ++++++++++++++++++++++- docs/en/api/packages/stream.md | 66 ++++++++++++++++++++++ stream/stream.go | 20 +++++++ stream/stream_example_test.go | 28 ++++++++++ stream/stream_test.go | 19 +++++++ 7 files changed, 263 insertions(+), 62 deletions(-) diff --git a/cryptor/rsa_private_example.pem b/cryptor/rsa_private_example.pem index 88d252d1..91b1a19a 100644 --- a/cryptor/rsa_private_example.pem +++ b/cryptor/rsa_private_example.pem @@ -1,51 +1,51 @@ -----BEGIN rsa private key----- -MIIJKAIBAAKCAgEAm1rBCE+U+7M46S21q0yoygTXHlhnZZ6IfQwRCK24EFQ6yBxt -emHKjNuHEIQ/DlSh+B5DRX/bCHQ/m5c0yrNPiMKv9N3K4e8Wv/pBVqAsSmF2nSjx -pXSraIACqrNSAig8MzUJ8pZq5d/9Le1AXNRa0aqHbw9YoLQJKACrdY4YbDi1NXU2 -ADImpnaSON0VJt5sdqA8OrEVOhm02DA4KEshJXAp2pR4h3KT82Fuj0dMKB1iKAIn -Xo8OeYwKgcjBxNZK4mPo6JT0ejYnFwhgtPaLlUm0BpXAqI2eclvhn+LmM9Q/S1mv -DCeLdNgQ8rh1+qPBXkCesskyu2hyuY3/H+WjQ4PHjF++lCnuRv/GJYIgbUnLxKP4 -cxsB+jj7SU+ltgADZoLyNeUpQ9QxY8FNbKDxJKnA7khtmvHjE4PKdy2e4M4mVcMh -eOtJdjvxhA2SEh9xnzP5G2JSHzE3877evInpQCJb+bfEG533B5Yrgm85C+sPcxrK -684+Dmn+Y3wdQQNJgYgQGZDGDIRY8stWpRX9G2dnWQfVtZLmSM5WwVPeI4wa6dxi -zk/ukylsgk9E5zJj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Qibh9cnBMghY3kqCCHviohbZlAoIBAQCg+hW7o6kwyFa7Ahioaq05jZkc +FA/2reCv2jgU16JkyLcnSQffpLlkWxDOL+ae40YVJDldvzTBZBsDxfqaZzZL2ddQ +HuT3H28z5byMq72FTOxQo6E0H6K5RW1edAN8IW9u5weAFAka3Qsvvy23P5ZajiRR +hTRrIFR5+Nv888MuZ9UDfKdQIlahMvqUqE+//tqXSzL18aFB1ufYh6A8bvtB+Yrm +5GzCMXhD87QoeV4+tRVUmYrBF9uIusiBMoM6tpY6/abQMyYSZh+7cYNJDETKmZP7 +HFzdr7uQkoog+LH4C2ZlIrBA7Hv7uJVrkY5BKg825Bg41UUjZrD2knsK3yyZ -----END rsa private key----- diff --git a/cryptor/rsa_public_example.pem b/cryptor/rsa_public_example.pem index 724d49d4..1c524d3d 100644 --- a/cryptor/rsa_public_example.pem +++ b/cryptor/rsa_public_example.pem @@ -1,14 +1,14 @@ -----BEGIN rsa public key----- -MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAm1rBCE+U+7M46S21q0yo -ygTXHlhnZZ6IfQwRCK24EFQ6yBxtemHKjNuHEIQ/DlSh+B5DRX/bCHQ/m5c0yrNP -iMKv9N3K4e8Wv/pBVqAsSmF2nSjxpXSraIACqrNSAig8MzUJ8pZq5d/9Le1AXNRa -0aqHbw9YoLQJKACrdY4YbDi1NXU2ADImpnaSON0VJt5sdqA8OrEVOhm02DA4KEsh -JXAp2pR4h3KT82Fuj0dMKB1iKAInXo8OeYwKgcjBxNZK4mPo6JT0ejYnFwhgtPaL -lUm0BpXAqI2eclvhn+LmM9Q/S1mvDCeLdNgQ8rh1+qPBXkCesskyu2hyuY3/H+Wj -Q4PHjF++lCnuRv/GJYIgbUnLxKP4cxsB+jj7SU+ltgADZoLyNeUpQ9QxY8FNbKDx -JKnA7khtmvHjE4PKdy2e4M4mVcMheOtJdjvxhA2SEh9xnzP5G2JSHzE3877evInp -QCJb+bfEG533B5Yrgm85C+sPcxrK684+Dmn+Y3wdQQNJgYgQGZDGDIRY8stWpRX9 -G2dnWQfVtZLmSM5WwVPeI4wa6dxizk/ukylsgk9E5zJjZlPx8Fx2hnTsrNSARl0g -CdbhGhRjHlVFjKI80z+evPXLCYn8B4CwWsg6ExBZSOAvcrN7XZFcalDCT5CqsNpb -Cg/7SXO87v8KL/54fKUc4qsCAwEAAQ== +MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA3bRSY5GD7uJB3jvwqa9Z +ZFpN3uLIGbbPA+yiyf6tJZp3K3rmkVFfEcWqtK1EfHvd9G4rqLYJ9ZcohDZgD/Ww +mqJtaaWh2/7feMPaitp50bYVilpPhaxULx+MIjQn3XXowYvr4fX7N052t4IC95tA +anPEEwZRQJOVMtibAxwI/ofUOCpTPtuK3AoErDxS18JPgjQ34yY4Jsch0de53+Sj +Tk+bJpLUlnY4brm+A23FhwTlE5njrKA+Texrg52KJ8aPhLuVQBcZP1SJsxv2PZru +uD+0xfLDRrZ7BOTpun+1FBhdlxBefIfBQbBBfy7STw+ignTaO+IRIHhcqKZ+12Na ++4GAusmpSiB3LATy82h0nB0PX3hSOLYmFI61px4l/yM50E4brF1NFtd/BpoozZaj +gxjJaznuZXFQ1YSG0JjCeszqdzU+6dexGTr+4Lr87NfLekmUX1nZYy/E5lST65lG +lI4hX1nR5+nVN9ALRYByNz6oJd0IfF4dhoNlGPc1VksskeY8rPZwXITbSifAN7zv +EiwKreFV3t0BLKsvWRu9GdpWy+d2AmelXkNm+QaH2mPM17euMoNZVyzizrZaJkh/ +aHO/UgY1/y7sXFl4akC9hk9ILONCUn6FVI4BXMXb5kZgwDk5tWE5A159LtC579yG +cnxyc9Y9NNFOGrqFqPiu6QUCAwEAAQ== -----END rsa public key----- diff --git a/docs/api/packages/stream.md b/docs/api/packages/stream.md index db8393f9..f9263fa1 100644 --- a/docs/api/packages/stream.md +++ b/docs/api/packages/stream.md @@ -1,6 +1,6 @@ # Stream -Stream 流,该包仅验证简单 stream 实现,功能有限。 +Stream流,该包仅验证简单stream实现,功能有限。
@@ -48,6 +48,8 @@ import ( - [NoneMatch](#NoneMatch) - [Count](#Count) - [ToSlice](#ToSlice) +- [IndexOf](#IndexOf) +- [LastIndexOf](#LastIndexOf)
@@ -938,3 +940,69 @@ func main() { // [1 2 3] } ``` + +### IndexOf + +

返回在stream中找到值的第一个匹配项的索引,如果找不到值,则返回-1。

+ +函数签名: + +```go +func (s Stream[T]) IndexOf(target T, equal func(a, b T) bool) int +``` + +示例:[运行](todo) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/stream" +) + +func main() { + s := stream.FromSlice([]int{1, 2, 3, 2}) + + result1 := s.IndexOf(0, func(a, b int) bool { return a == b }) + result2 := s.IndexOf(2, func(a, b int) bool { return a == b }) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // -1 + // 1 +} +``` + +### LastIndexOf + +

返回在stream中找到值的最后一个匹配项的索引,如果找不到值,则返回-1。

+ +函数签名: + +```go +func (s Stream[T]) LastIndexOf(target T, equal func(a, b T) bool) int +``` + +示例:[运行](todo) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/stream" +) + +func main() { + s := stream.FromSlice([]int{1, 2, 3, 2}) + + result1 := s.LastIndexOf(0, func(a, b int) bool { return a == b }) + result2 := s.LastIndexOf(2, func(a, b int) bool { return a == b }) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // -1 + // 3 +} +``` \ No newline at end of file diff --git a/docs/en/api/packages/stream.md b/docs/en/api/packages/stream.md index 6b5dfb2a..c652a548 100644 --- a/docs/en/api/packages/stream.md +++ b/docs/en/api/packages/stream.md @@ -939,3 +939,69 @@ func main() { // [1 2 3] } ``` + +### IndexOf + +

Returns the index of the first occurrence of the specified element in this stream, or -1 if this stream does not contain the element.

+ +Signature: + +```go +func (s Stream[T]) IndexOf(target T, equal func(a, b T) bool) int +``` + +Example:[Run](todo) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/stream" +) + +func main() { + s := stream.FromSlice([]int{1, 2, 3, 2}) + + result1 := s.IndexOf(0, func(a, b int) bool { return a == b }) + result2 := s.IndexOf(2, func(a, b int) bool { return a == b }) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // -1 + // 1 +} +``` + +### LastIndexOf + +

Returns the index of the last occurrence of the specified element in this stream, or -1 if this stream does not contain the element.

+ +Signature: + +```go +func (s Stream[T]) LastIndexOf(target T, equal func(a, b T) bool) int +``` + +Example:[Run](todo) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/stream" +) + +func main() { + s := stream.FromSlice([]int{1, 2, 3, 2}) + + result1 := s.LastIndexOf(0, func(a, b int) bool { return a == b }) + result2 := s.LastIndexOf(2, func(a, b int) bool { return a == b }) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // -1 + // 3 +} +``` \ No newline at end of file diff --git a/stream/stream.go b/stream/stream.go index 1cf95722..337587fc 100644 --- a/stream/stream.go +++ b/stream/stream.go @@ -393,6 +393,26 @@ func (s Stream[T]) Min(less func(a, b T) bool) (T, bool) { return min, true } +// IndexOf returns the index of the first occurrence of the specified element in this stream, or -1 if this stream does not contain the element. +func (s Stream[T]) IndexOf(target T, equal func(a, b T) bool) int { + for i, v := range s.source { + if equal(v, target) { + return i + } + } + return -1 +} + +// LastIndexOf returns the index of the last occurrence of the specified element in this stream, or -1 if this stream does not contain the element. +func (s Stream[T]) LastIndexOf(target T, equal func(a, b T) bool) int { + for i := len(s.source) - 1; i >= 0; i-- { + if equal(s.source[i], target) { + return i + } + } + return -1 +} + // ToSlice return the elements in the stream. // Play: https://go.dev/play/p/jI6_iZZuVFE func (s Stream[T]) ToSlice() []T { diff --git a/stream/stream_example_test.go b/stream/stream_example_test.go index b854dfd2..913847bb 100644 --- a/stream/stream_example_test.go +++ b/stream/stream_example_test.go @@ -384,3 +384,31 @@ func ExampleStream_Count() { // 3 // 0 } + +func ExampleStream_IndexOf() { + s := FromSlice([]int{1, 2, 3, 2}) + + result1 := s.IndexOf(0, func(a, b int) bool { return a == b }) + result2 := s.IndexOf(2, func(a, b int) bool { return a == b }) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // -1 + // 1 +} + +func ExampleStream_LastIndexOf() { + s := FromSlice([]int{1, 2, 3, 2}) + + result1 := s.LastIndexOf(0, func(a, b int) bool { return a == b }) + result2 := s.LastIndexOf(2, func(a, b int) bool { return a == b }) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // -1 + // 3 +} diff --git a/stream/stream_test.go b/stream/stream_test.go index 63dca4bb..5b91790a 100644 --- a/stream/stream_test.go +++ b/stream/stream_test.go @@ -381,3 +381,22 @@ func TestStream_Min(t *testing.T) { assert.Equal(1, max) assert.Equal(true, ok) } + +func TestStream_IndexOf(t *testing.T) { + assert := internal.NewAssert(t, "TestStream_IndexOf") + + s := FromSlice([]int{4, 2, 1, 3, 4}) + + assert.Equal(-1, s.IndexOf(0, func(a, b int) bool { return a == b })) + assert.Equal(0, s.IndexOf(4, func(a, b int) bool { return a == b })) + assert.Equal(3, s.IndexOf(3, func(a, b int) bool { return a == b })) +} + +func TestStream_LastIndexOf(t *testing.T) { + assert := internal.NewAssert(t, "TestStream_LastIndexOf") + + s := FromSlice([]int{4, 2, 1, 3, 2}) + + assert.Equal(-1, s.LastIndexOf(0, func(a, b int) bool { return a == b })) + assert.Equal(4, s.LastIndexOf(2, func(a, b int) bool { return a == b })) +}