condition

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package condition contains some functions for conditional judgment. eg. And, Or, TernaryOperator ... The implementation of this package refers to the implementation of carlmjohnson's truthy package, you may find more useful information in truthy(https://github.com/carlmjohnson/truthy), thanks carlmjohnson.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func And

func And[T, U any](a T, b U) bool

And returns true if both a and b are truthy. Play: https://go.dev/play/p/W1SSUmt6pvr

Example
result1 := And(0, 1)
result2 := And(0, "")
result3 := And(0, "0")
result4 := And(1, "0")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

false
false
false
true

func Bool

func Bool[T any](value T) bool

Bool returns the truthy value of anything. If the value's type has a Bool() bool method, the method is called and returned. If the type has an IsZero() bool method, the opposite value is returned. Slices and maps are truthy if they have a length greater than zero. All other types are truthy if they are not their zero value. Play: https://go.dev/play/p/ETzeDJRSvhm

Example
// bool
result1 := Bool(false)
result2 := Bool(true)
fmt.Println(result1)
fmt.Println(result2)

// integer
result3 := Bool(0)
result4 := Bool(1)
fmt.Println(result3)
fmt.Println(result4)

// string
result5 := Bool("")
result6 := Bool(" ")
fmt.Println(result5)
fmt.Println(result6)

// slice
var nums = []int{}
result7 := Bool(nums)
nums = append(nums, 1, 2)
result8 := Bool(nums)
fmt.Println(result7)
fmt.Println(result8)
Output:

false
true
false
true
false
true
false
true

func Nand

func Nand[T, U any](a T, b U) bool

Nand returns false if both a and b are truthy. Play: https://go.dev/play/p/vSRMLxLIbq8

Example
result1 := Nand(0, 0)
result2 := Nand(1, 0)
result3 := Nand(0, 1)
result4 := Nand(1, 1)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
true
false

func Nor

func Nor[T, U any](a T, b U) bool

Nor returns true if neither a nor b is truthy. Play: https://go.dev/play/p/g2j08F_zZky

Example
result1 := Nor(0, 0)
result2 := Nor(1, 1)
result3 := Nor(0, 1)
result4 := Nor(1, 0)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
false
false
false

func Or

func Or[T, U any](a T, b U) bool

Or returns false if neither a nor b is truthy. Play: https://go.dev/play/p/UlQTxHaeEkq

Example
result1 := Or(0, "")
result2 := Or(0, 1)
result3 := Or(0, "0")
result4 := Or(1, "0")

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

false
true
true
true

func TernaryOperator

func TernaryOperator[T, U any](isTrue T, ifValue U, elseValue U) U

TernaryOperator checks the value of param `isTrue`, if true return ifValue else return elseValue. Play: https://go.dev/play/p/ElllPZY0guT

Example
conditionTrue := 2 > 1
result1 := TernaryOperator(conditionTrue, 0, 1)
fmt.Println(result1)

conditionFalse := 2 > 3
result2 := TernaryOperator(conditionFalse, 0, 1)
fmt.Println(result2)
Output:

0
1

func Xnor

func Xnor[T, U any](a T, b U) bool

Xnor returns true if both a and b or neither a nor b are truthy. Play: https://go.dev/play/p/OuDB9g51643

Example
result1 := Xnor(0, 0)
result2 := Xnor(1, 1)
result3 := Xnor(0, 1)
result4 := Xnor(1, 0)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

true
true
false
false

func Xor

func Xor[T, U any](a T, b U) bool

Xor returns true if a or b but not both is truthy. Play: https://go.dev/play/p/gObZrW7ZbG8

Example
result1 := Xor(0, 0)
result2 := Xor(1, 1)
result3 := Xor(0, 1)
result4 := Xor(1, 0)

fmt.Println(result1)
fmt.Println(result2)
fmt.Println(result3)
fmt.Println(result4)
Output:

false
false
true
true

Types

This section is empty.

Jump to

Keyboard shortcuts

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