Skip to content

Commit 13757e6

Browse files
author
yangyile
committed
增加新的逻辑
1 parent d57eef9 commit 13757e6

File tree

5 files changed

+294
-2
lines changed

5 files changed

+294
-2
lines changed

README.md

Lines changed: 180 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,6 @@ The `tern` package provides the following functions based on condition types and
7070
| `FFV` | `func() bool` | Function returning value | Direct value |
7171
| `FFF` | `func() bool` | Function returning value | Function returning value |
7272

73-
Additionally, utility functions like `BV`, `BF`, `FV`, and `FF` simplify zero-value fallbacks.
74-
7573
### Lazy Evaluation Example
7674

7775
Using deferred execution ensures unnecessary computations are avoided:
@@ -104,6 +102,186 @@ func main() {
104102
}
105103
```
106104

105+
以下是对 `BV``BF``FV``FF` 函数的详细表格说明:
106+
107+
---
108+
109+
### Utility Functions for Zero-Value Fallbacks
110+
111+
| **Function** | **Condition Type** | **Primary Value** | **Fallback Value** | **Description** |
112+
|--------------|--------------------|--------------------------|------------------------|--------------------------------------------------------------------------------------------------------------------------------------|
113+
| `BV` | `bool` | Direct value | Zero value of type `T` | Returns the first value if the condition is true, otherwise returns the zero value of type `T`. |
114+
| `BF` | `bool` | Function returning value | Zero value of type `T` | Evaluates the function only if the condition is true, otherwise returns the zero value of type `T`. |
115+
| `FV` | `func() bool` | Direct value | Zero value of type `T` | Evaluates the condition function and returns the first value if true, otherwise returns the zero value of type `T`. |
116+
| `FF` | `func() bool` | Function returning value | Zero value of type `T` | Evaluates the condition function and returns the result of the first function if true, otherwise returns the zero value of type `T`. |
117+
118+
---
119+
120+
### Examples
121+
122+
#### Using `BV`
123+
124+
```go
125+
result := tern.BV(false, "value")
126+
// Output: (empty string, as the condition is false)
127+
```
128+
129+
#### Using `BF`
130+
131+
```go
132+
result := tern.BF(false, func() string { return "value" })
133+
// Output: (empty string, as the condition is false)
134+
```
135+
136+
#### Using `FV`
137+
138+
```go
139+
condition := func() bool { return true }
140+
result := tern.FV(condition, "value")
141+
// Output: "value" (as the condition function returns true)
142+
```
143+
144+
#### Using `FF`
145+
146+
```go
147+
condition := func() bool { return false }
148+
result := tern.FF(condition, func() string { return "value" })
149+
// Output: (empty string, as the condition function returns false)
150+
```
151+
152+
---
153+
154+
### Additional Utility Functions in `terngo` Package
155+
156+
The `terngo` subpackage extends the functionality of `tern` with additional utility functions, specifically designed for comparing values to their zero value. These functions allow for more expressive handling of default values when one of the inputs might be zero.
157+
158+
| **Function** | **Comparison Type** | **Primary Value** | **Fallback Value** | **Description** |
159+
|--------------|---------------------|-------------------|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
160+
| `VV` | Direct comparison | Direct value | Direct value | Returns the first value if it is not the zero value of type `T`, otherwise returns the second value. |
161+
| `VF` | Direct comparison | Direct value | Function returning value | Returns the first value if it is not the zero value of type `T`, otherwise evaluates and returns the result of the fallback function. |
162+
163+
---
164+
165+
### Examples
166+
167+
#### Using `VV`
168+
169+
```go
170+
package main
171+
172+
import (
173+
"fmt"
174+
"github.com/yyle88/tern/terngo"
175+
)
176+
177+
func main() {
178+
result := terngo.VV("non-zero", "fallback")
179+
fmt.Println(result) // Output: "non-zero"
180+
181+
result = terngo.VV("", "fallback")
182+
fmt.Println(result) // Output: "fallback"
183+
}
184+
```
185+
186+
#### Using `VF`
187+
188+
```go
189+
package main
190+
191+
import (
192+
"fmt"
193+
"github.com/yyle88/tern/terngo"
194+
)
195+
196+
func main() {
197+
fallbackFunc := func() string { return "fallback from func" }
198+
199+
result := terngo.VF("non-zero", fallbackFunc)
200+
fmt.Println(result) // Output: "non-zero"
201+
202+
result = terngo.VF("", fallbackFunc)
203+
fmt.Println(result) // Output: "fallback from func"
204+
}
205+
```
206+
207+
---
208+
209+
### Key Benefits of `VV` and `VF`
210+
211+
- **Zero-Value Handling**: These functions leverage `tern.Zero[T]()` to check if the primary value is a zero value.
212+
- **Fallback Options**: `VF` provides flexibility by allowing dynamic evaluation of the fallback value through a function.
213+
214+
---
215+
216+
By including these functions in the `terngo` package, developers gain even more expressive tools to simplify conditional logic in Go.
217+
218+
### Additional Utility Functions for Pointer Modification in `terngo`
219+
220+
The `terngo` subpackage introduces `PV` and `PF`, two specialized functions for safely modifying a pointer's value when it holds the zero value of its type. These functions ensure safer and more expressive manipulation of pointers while providing fallback options.
221+
222+
| **Function** | **Pointer Check** | **Fallback Value** | **Description** |
223+
|--------------|-------------------------|--------------------------|---------------------------------------------------------------------------------------------------------------------------------------|
224+
| `PV` | Pointer to direct value | Direct value | Sets the pointed-to value to the fallback value if it is the zero value of type `T`. Panics if the pointer is `nil`. |
225+
| `PF` | Pointer to direct value | Function returning value | Sets the pointed-to value to the result of the fallback function if it is the zero value of type `T`. Panics if the pointer is `nil`. |
226+
227+
---
228+
229+
### Examples
230+
231+
#### Using `PV`
232+
233+
```go
234+
package main
235+
236+
import (
237+
"fmt"
238+
"github.com/yyle88/tern/terngo"
239+
)
240+
241+
func main() {
242+
var value int
243+
terngo.PV(&value, 42)
244+
fmt.Println(value) // Output: 42
245+
246+
value = 7
247+
terngo.PV(&value, 99)
248+
fmt.Println(value) // Output: 7
249+
}
250+
```
251+
252+
#### Using `PF`
253+
254+
```go
255+
package main
256+
257+
import (
258+
"fmt"
259+
"github.com/yyle88/tern/terngo"
260+
)
261+
262+
func main() {
263+
var value int
264+
fallbackFunc := func() int { return 42 }
265+
266+
terngo.PF(&value, fallbackFunc)
267+
fmt.Println(value) // Output: 42
268+
269+
value = 7
270+
terngo.PF(&value, fallbackFunc)
271+
fmt.Println(value) // Output: 7
272+
}
273+
```
274+
275+
---
276+
277+
### Key Features of `PV` and `PF`
278+
279+
- **Pointer Safety**: These functions panic if the provided pointer is `nil`, ensuring reliable execution.
280+
- **Zero-Value Check**: Use `tern.Zero[T]()` to determine whether the current value is the zero value of its type.
281+
- **Dynamic Fallback**: `PF` allows for fallback values to be generated dynamically via a function.
282+
283+
By leveraging `PV` and `PF`, developers can seamlessly handle zero-value assignments for pointer-based logic, maintaining both clarity and robustness in their Go code.
284+
107285
## Why Use `tern`?
108286

109287
1. **Readability**: Simplifies conditional logic, making the code easier to follow.

terngo/znew.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package terngo
2+
3+
import "github.com/yyle88/tern"
4+
5+
func VV[T comparable](a T, b T) T {
6+
if a != tern.Zero[T]() {
7+
return a
8+
}
9+
return b
10+
}
11+
12+
func VF[T comparable](a T, b func() T) T {
13+
if a != tern.Zero[T]() {
14+
return a
15+
}
16+
return b()
17+
}

terngo/znew_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package terngo_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"github.com/yyle88/tern/terngo"
8+
)
9+
10+
func TestVV(t *testing.T) {
11+
require.Equal(t, 1, terngo.VV(0, 1))
12+
require.Equal(t, 1, terngo.VV(1, 2))
13+
require.Equal(t, "a", terngo.VV("", "a"))
14+
require.Equal(t, "a", terngo.VV("a", "b"))
15+
}
16+
17+
func TestVF(t *testing.T) {
18+
require.Equal(t, 1, terngo.VF(0, func() int {
19+
return 1
20+
}))
21+
require.Equal(t, 1, terngo.VF(1, func() int {
22+
return 2
23+
}))
24+
require.Equal(t, "a", terngo.VF("", func() string {
25+
return "a"
26+
}))
27+
require.Equal(t, "a", terngo.VF("a", func() string {
28+
return "b"
29+
}))
30+
}

terngo/zset.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package terngo
2+
3+
import "github.com/yyle88/tern"
4+
5+
func PV[T comparable](p *T, b T) {
6+
if *p == tern.Zero[T]() {
7+
*p = b
8+
}
9+
}
10+
11+
func PF[T comparable](p *T, b func() T) {
12+
if *p == tern.Zero[T]() {
13+
*p = b()
14+
}
15+
}

terngo/zset_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package terngo_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
"github.com/yyle88/tern/terngo"
8+
)
9+
10+
func TestPV(t *testing.T) {
11+
a := 0
12+
terngo.PV(&a, 1)
13+
require.Equal(t, 1, a)
14+
15+
b := 1
16+
terngo.PV(&b, 2)
17+
require.Equal(t, 1, b)
18+
19+
c := ""
20+
terngo.PV(&c, "a")
21+
require.Equal(t, "a", c)
22+
23+
s := "a"
24+
terngo.PV(&s, "b")
25+
require.Equal(t, "a", s)
26+
}
27+
28+
func TestPF(t *testing.T) {
29+
a := 0
30+
terngo.PF(&a, func() int {
31+
return 1
32+
})
33+
require.Equal(t, 1, a)
34+
35+
b := 1
36+
terngo.PF(&b, func() int {
37+
return 2
38+
})
39+
require.Equal(t, 1, b)
40+
41+
c := ""
42+
terngo.PF(&c, func() string {
43+
return "a"
44+
})
45+
require.Equal(t, "a", c)
46+
47+
s := "a"
48+
terngo.PF(&s, func() string {
49+
return "b"
50+
})
51+
require.Equal(t, "a", s)
52+
}

0 commit comments

Comments
 (0)