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

Tuple: 增加 Pair 和 Triple 两种类型 #164

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- [reflectx: IsNil 方法](https://github.com/gotomicro/ekit/pull/150)
- [setx: Set接口设计和基于map的实现](https://github.com/gotomicro/ekit/pull/148)
- [setx: TreeSet基于TreeMap实现](https://github.com/gotomicro/ekit/issues/147)
- [tuple: 增加 Pair 和 Triple 两种](https://github.com/ecodeclub/ekit/issues/161)

# v0.0.6
- [queue: 基于semaphore的并发阻塞队列实现](https://github.com/gotomicro/ekit/pull/129)
Expand Down
49 changes: 49 additions & 0 deletions tuple/pair/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2021 ecodeclub
KirinRyuuri marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pair_test

import (
"fmt"
. "github.com/gotomicro/ekit/tuple/pair"
)

func ExamplePair_Copy() {
pair := Pair{
First: 1,
Second: "two"} // <1,"two">
pair = pair.Copy(
Pair{Second: "one"})
fmt.Println(pair.ToString())

// Output: <1,one>
}

func ExamplePair_ToList() {
pair := Pair{
First: 1,
Second: "one"}
fmt.Println(pair.ToList())

//Output: [1 one]
}

func ExamplePair_ToString() {
pair := Pair{
First: 1,
Second: "one"}
fmt.Println(pair.ToString())

//Output: <1,one>
}
44 changes: 44 additions & 0 deletions tuple/pair/pair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package pair

import "fmt"

type Pair struct {
First any
Second any
}

// ToString 将 Pair 转为字符串,格式类似于 <key,value>
func (p Pair) ToString() string {
return fmt.Sprint("<", p.First, ",", p.Second, ">")
}

// ToList 将 Pair 转为数组
func (p Pair) ToList() []any {
return []any{p.First, p.Second}
}

// Copy 传入一个 Pair 来修改对应位置的值
func (p Pair) Copy(toPair Pair) (newPair Pair) {
newPair = Pair{First: p.First, Second: p.Second}
if toPair.First != nil {
newPair.First = toPair.First
}
if toPair.Second != nil {
newPair.Second = toPair.Second
}
return
}
52 changes: 52 additions & 0 deletions tuple/triple/example_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package triple_test

import (
"fmt"
. "github.com/gotomicro/ekit/tuple/triple"
)

func ExampleTriple_Copy() {
triple := Triple{
First: 1,
Second: "one",
Third: "second"} // <1,"one","second">

triple = triple.Copy(
Triple{Third: "first"})

fmt.Println(triple.ToString())

// Output: <1,one,first>
}

func ExampleTriple_ToList() {
triple := Triple{
First: 1,
Second: "one"}
fmt.Println(triple.ToList())

//Output: [1 one]
}

func ExampleTriple_ToString() {
triple := Triple{
First: 1,
Second: "one"}
fmt.Println(triple.ToString())

//Output: <1,one>
}
48 changes: 48 additions & 0 deletions tuple/triple/triple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package triple

import "fmt"

type Triple struct {
First any
Second any
Third any
}

// ToString 将 Triple 转为字符串,格式类似于 <key,value>
func (t Triple) ToString() string {
return fmt.Sprint("<", t.First, ",", t.Second, ",", t.Third, ">")
}

// ToList 将 Triple 转为数组
func (t Triple) ToList() []any {
return []any{t.First, t.Second, t.Third}
}

// Copy 传入一个 Triple 来修改对应位置的值
func (t Triple) Copy(toTriple Triple) (newTriple Triple) {
newTriple = Triple{First: t.First, Second: t.Second}
if toTriple.First != nil {
newTriple.First = toTriple.First
}
if toTriple.Second != nil {
newTriple.Second = toTriple.Second
}
if toTriple.Third != nil {
newTriple.Third = toTriple.Third
}
return
}
20 changes: 20 additions & 0 deletions tuple/tuple.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2021 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package tuple

type Tuple interface {
ToString() string
ToList() []any
}
Loading