tern
is a lightweight and versatile Go package designed to streamline conditional logic with concise ternary expressions, helping you write clean, expressive, and maintainable code with ease.
- Generic Support: Fully leverages Go’s generics for type safety and flexibility across various data types.
- Flexible Logic: Provides robust support for both boolean conditions and conditional functions.
- Lazy Evaluation: Optimizes performance by computing values only when necessary through deferred execution.
- Zero Value Handling: Offers utilities to return default zero values for any type when no fallback is supplied.
go get github.com/yyle88/tern
The tern
package provides multiple helper functions tailored to handle various conditional scenarios.
package main
import (
"fmt"
"github.com/yyle88/tern"
)
func main() {
// Basic conditional selection
result := tern.BVV(true, "Option A", "Option B")
fmt.Println(result) // Output: Option A
// Deferred execution for fallback value
result = tern.BVF(false, "Default", func() string { return "Computed Fallback" })
fmt.Println(result) // Output: Computed Fallback
// Handling zero values as fallback
result = tern.BV(false, "Fallback")
fmt.Println(result) // Output: (empty string)
}
Here is an overview of the functions provided by the tern
package:
Function | Condition Type | Primary Value | Fallback Value |
---|---|---|---|
BVV |
bool |
Direct value | Direct value |
BVF |
bool |
Direct value | Function returning value |
BFV |
bool |
Function returning value | Direct value |
BFF |
bool |
Function returning value | Function returning value |
FVV |
func() bool |
Direct value | Direct value |
FVF |
func() bool |
Direct value | Function returning value |
FFV |
func() bool |
Function returning value | Direct value |
FFF |
func() bool |
Function returning value | Function returning value |
Deferred execution ensures unnecessary computations are avoided, making your code more efficient:
func expensiveComputation() string {
fmt.Println("Computing...")
return "Heavy Result"
}
result := tern.BVF(false, "Default", expensiveComputation)
// Output: Default (expensiveComputation is not executed)
The package provides Zero[T]()
, a utility that returns the zero value for any generic type:
package main
import (
"fmt"
"github.com/yyle88/tern"
)
func main() {
fmt.Println(tern.Zero[int]()) // Output: 0
fmt.Println(tern.Zero[string]()) // Output: (empty string)
}
The package includes functions that automatically handle zero values when the condition is not met:
Function | Condition Type | Primary Value | Fallback Value |
---|---|---|---|
BV |
bool |
Direct value | Zero value of type T |
BF |
bool |
Function returning value | Zero value of type T |
FV |
func() bool |
Direct value | Zero value of type T |
FF |
func() bool |
Function returning value | Zero value of type T |
The zerotern
subpackage extends tern
with specialized utilities for comparing values to their zero value, adding more control for fallback scenarios.
Function | Comparison Type | Primary Value | Fallback Value |
---|---|---|---|
VV |
Direct comparison | Direct value | Direct value |
VF |
Direct comparison | Direct value | Function returning value |
package main
import (
"fmt"
"github.com/yyle88/tern/zerotern"
)
func main() {
// Direct comparison
result := zerotern.VV("non-zero", "fallback")
fmt.Println(result) // Output: non-zero
// Fallback with function
result = zerotern.VF("", func() string { return "fallback func" })
fmt.Println(result) // Output: fallback func
}
Function | Pointer Handling | Fallback Value |
---|---|---|
SetPV |
Pointer to direct value | Direct value |
SetPF |
Pointer to direct value | Function returning value |
package main
import (
"fmt"
"github.com/yyle88/tern/zerotern"
)
func main() {
var value int
zerotern.SetPV(&value, 42)
fmt.Println(value) // Output: 42
value = 7
zerotern.SetPF(&value, func() int { return 99 })
fmt.Println(value) // Output: 7
}
- Improved Readability: Simplifies conditional logic with concise and clear expressions.
- Performance Optimization: Lazy evaluation avoids unnecessary computations.
- Type Safety: Leverages Go’s generics for maximum flexibility and reliability.
- Versatility: Supports a wide range of scenarios, including pointer handling and zero-value fallbacks.
Contributions are welcome! Feel free to report issues, suggest improvements, or submit pull requests on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.
Feel free to contribute or improve the package! Stars and pull requests are always welcome!
Thank you for using tern
!
Give me stars! Thank you!!!