- Notifications
You must be signed in to change notification settings - Fork2
tern is a lightweight Go package for simple, concise ternary expressions, enabling clear conditional logic.
License
yyle88/tern
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
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
Thetern
package provides multiple helper functions tailored to handle various conditional scenarios.
package mainimport ("fmt""github.com/yyle88/tern")funcmain() {// Basic conditional selectionresult:=tern.BVV(true,"Option A","Option B")fmt.Println(result)// Output: Option A// Deferred execution for fallback valueresult=tern.BVF(false,"Default",func()string {return"Computed Fallback" })fmt.Println(result)// Output: Computed Fallback// Handling zero values as fallbackresult=tern.BV(false,"Fallback")fmt.Println(result)// Output: (empty string)}
Here is an overview of the functions provided by thetern
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:
funcexpensiveComputation()string {fmt.Println("Computing...")return"Heavy Result"}result:=tern.BVF(false,"Default",expensiveComputation)// Output: Default (expensiveComputation is not executed)
The package providesZero[T]()
, a utility that returns the zero value for any generic type:
package mainimport ("fmt""github.com/yyle88/tern")funcmain() {fmt.Println(tern.Zero[int]())// Output: 0fmt.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 typeT |
BF | bool | Function returning value | Zero value of typeT |
FV | func() bool | Direct value | Zero value of typeT |
FF | func() bool | Function returning value | Zero value of typeT |
Thezerotern
subpackage extendstern
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 mainimport ("fmt""github.com/yyle88/tern/zerotern")funcmain() {// Direct comparisonresult:=zerotern.VV("non-zero","fallback")fmt.Println(result)// Output: non-zero// Fallback with functionresult=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 mainimport ("fmt""github.com/yyle88/tern/zerotern")funcmain() {varvalueintzerotern.SetPV(&value,42)fmt.Println(value)// Output: 42value=7zerotern.SetPF(&value,func()int {return99 })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 onGitHub.
This project is licensed under the MIT License. See theLICENSE file for details.
Feel free to contribute or improve the package! Stars and pull requests are always welcome!
Thank you for usingtern
!
Give me stars! Thank you!!!
About
tern is a lightweight Go package for simple, concise ternary expressions, enabling clear conditional logic.