Categorygithub.com/yyle88/tern
repositorypackage
0.0.4
Repository: https://github.com/yyle88/tern.git
Documentation: pkg.go.dev

# Packages

No description provided by the author
No description provided by the author

# README

GitHub Workflow Status (branch) GoDoc Coverage Status Supported Go Versions GitHub Release Go Report Card

tern

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.

README

中文说明

Features

  • 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.

Installation

go get github.com/yyle88/tern

Usage

The tern package provides multiple helper functions tailored to handle various conditional scenarios.

Basic Usage

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)
}

Function Overview

Here is an overview of the functions provided by the tern package:

FunctionCondition TypePrimary ValueFallback Value
BVVboolDirect valueDirect value
BVFboolDirect valueFunction returning value
BFVboolFunction returning valueDirect value
BFFboolFunction returning valueFunction returning value
FVVfunc() boolDirect valueDirect value
FVFfunc() boolDirect valueFunction returning value
FFVfunc() boolFunction returning valueDirect value
FFFfunc() boolFunction returning valueFunction returning value

Lazy Evaluation Example

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)

Working with Zero Values

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)
}

Handling Zero-Value Fallbacks

The package includes functions that automatically handle zero values when the condition is not met:

FunctionCondition TypePrimary ValueFallback Value
BVboolDirect valueZero value of type T
BFboolFunction returning valueZero value of type T
FVfunc() boolDirect valueZero value of type T
FFfunc() boolFunction returning valueZero value of type T

Additional Utilities in the zerotern Package

The zerotern subpackage extends tern with specialized utilities for comparing values to their zero value, adding more control for fallback scenarios.

FunctionComparison TypePrimary ValueFallback Value
VVDirect comparisonDirect valueDirect value
VFDirect comparisonDirect valueFunction returning value

Example: Using VV and VF

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
}

Pointer-Based Utility Functions in zerotern

FunctionPointer HandlingFallback Value
SetPVPointer to direct valueDirect value
SetPFPointer to direct valueFunction returning value

Example: Using SetPV and SetPF

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
}

Why Choose tern?

  1. Improved Readability: Simplifies conditional logic with concise and clear expressions.
  2. Performance Optimization: Lazy evaluation avoids unnecessary computations.
  3. Type Safety: Leverages Go’s generics for maximum flexibility and reliability.
  4. Versatility: Supports a wide range of scenarios, including pointer handling and zero-value fallbacks.

Contributing

Contributions are welcome! Feel free to report issues, suggest improvements, or submit pull requests on GitHub.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Contributing

Feel free to contribute or improve the package! Stars and pull requests are always welcome!

Thank you for using tern!


See stars

see stars

Give me stars! Thank you!!!