op

package
v0.0.0-...-89aa834 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 1 Imported by: 1

Documentation

Overview

Package op provides a set of generic functions that extend and complement Go's built-in operators and basic operations. It includes utilities for conditional logic, comparisons, and type manipulations.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Addr

func Addr[T any](x T) *T

Addr returns the address of x.

func Assert

func Assert(cond bool, msgs ...any)

Assert panics if cond is false.

func Assertf

func Assertf(cond bool, format string, args ...any)

Assertf panics with a formatted message if cond is false.

func Bin

func Bin[T comparable](x T) int

Bin converts a comparable value to a binary number (0 or 1). It returns 0 if the input is equal to its zero value, and 1 otherwise.

func Deref

func Deref[T any](p *T) T

Deref returns the value of p if it is not nil, otherwise it returns the zero value of T.

func DerefOr

func DerefOr[T any](p *T, x T) T

DerefOr returns the value of p if it is not nil, otherwise it returns x.

func DerefOrFunc

func DerefOrFunc[T any](p *T, x func() T) T

DerefOr returns the value of p if it is not nil, otherwise it returns result of calling x().

func First

func First[T any](first T, _ ...any) T

First returns the first argument. It extracts the first value from a set of arguments.

func Identity

func Identity[T any](v T) func() T

Identity returns a function that returns the input value.

func If

func If[T any](condition bool, a, b T) T

If returns a if condition is true, otherwise returns b. It provides a generic ternary operation for any type.

Example
package main

import (
	"fmt"

	"github.com/gopherd/core/op"
)

func main() {
	condition := true
	fmt.Println(op.If(condition, "It's true", "It's false"))
	condition = false
	fmt.Println(op.If(condition, "It's true", "It's false"))
}
Output:

It's true
It's false

func IfFunc

func IfFunc[T any](condition bool, a T, b func() T) T

IfFunc returns a if condition is true, otherwise returns the result of calling b().

func IfFunc2

func IfFunc2[T any](condition bool, a, b func() T) T

IfFunc2 returns the result of calling a() if condition is true, otherwise returns the result of calling b(). It allows for lazy evaluation of both alternatives.

func Must

func Must(err error)

Must panics if err is not nil.

func MustResult

func MustResult[T any](value T, err error) T

MustResult panics if err is not nil, otherwise it returns value. It is a convenient way to handle errors in a single line.

func MustResult2

func MustResult2[T1, T2 any](value1 T1, value2 T2, err error) (T1, T2)

MustResult2 panics if err is not nil, otherwise it returns value1 and value2.

func Or

func Or[T comparable](a, b T) T

Or returns b if a is the zero value for T, otherwise returns a.

Example
package main

import (
	"fmt"

	"github.com/gopherd/core/op"
)

func main() {
	fmt.Println(op.Or(0, 1))
	fmt.Println(op.Or(2, 3))
	fmt.Println(op.Or("", "default"))
	fmt.Println(op.Or("value", "default"))
}
Output:

1
2
default
value
Example (Sort)
package main

import (
	"fmt"
	"slices"

	"github.com/gopherd/core/op"
)

func main() {
	type Order struct {
		Product  string
		Customer string
		Price    float64
	}
	orders := []Order{
		{"foo", "alice", 1.00},
		{"bar", "bob", 3.00},
		{"baz", "carol", 4.00},
		{"foo", "alice", 2.00},
		{"bar", "carol", 1.00},
		{"foo", "bob", 4.00},
	}
	// Sort by customer first, product second, and last by higher price
	slices.SortFunc(orders, func(a, b Order) int {
		customerCmp := op.If(a.Customer < b.Customer, -1, op.If(a.Customer > b.Customer, 1, 0))
		productCmp := op.If(a.Product < b.Product, -1, op.If(a.Product > b.Product, 1, 0))
		priceCmp := op.If(b.Price < a.Price, -1, op.If(b.Price > a.Price, 1, 0))

		return op.Or(op.Or(customerCmp, productCmp), priceCmp)
	})
	for _, order := range orders {
		fmt.Printf("%s %s %.2f\n", order.Customer, order.Product, order.Price)
	}
}
Output:

alice foo 2.00
alice foo 1.00
bob bar 3.00
bob foo 4.00
carol bar 1.00
carol baz 4.00

func OrFunc

func OrFunc[T comparable](a T, b func() T) T

OrFunc returns the result of calling b() if a is the zero value for T, otherwise returns a. It allows for lazy evaluation of the alternative value.

func Result

func Result(value any, err error) any

Result returns err if it is not nil, otherwise it returns value.

func ReverseCompare

func ReverseCompare[T any](cmp func(T, T) int) func(T, T) int

ReverseCompare returns a comparison function that reverses the order of the original comparison function.

func Second

func Second[T1, T2 any](first T1, second T2, _ ...any) T2

Second returns the second argument. It extracts the second value from a set of arguments.

func SetDefault

func SetDefault[T comparable](a *T, b T) T

SetDefault sets the value of a to b if a is the zero value for T. It returns the final value of a.

func SetDefaultFunc

func SetDefaultFunc[T comparable](a *T, b func() T) T

SetDefaultFunc sets the value of a to the result of calling b() if a is the zero value for T. It returns the final value of a.

func Third

func Third[T1, T2, T3 any](first T1, second T2, third T3, _ ...any) T3

Third returns the third argument. It extracts the third value from a set of arguments.

func Zero

func Zero[T any]() T

Zero returns the zero value of type T.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL