operator

package
v2.8.1 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2023 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package operator implements builtin language operators, such as "==" (equals) or "+" (addition), as functions that can be passed to higher order functions.

See also the github.com/tawesoft/golib/v2/operator/checked/integer package which implements operators that are robust against integer overflow.

See also the github.com/tawesoft/golib/v2/operator/reflect package which implements operators that require reflection.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs[R signed](r R) R

Abs returns (0 - r) for r < 0, or r for r >= 0.

func Add

func Add[R real](a R, b R) R

Add returns a + b.

Example
package main

import (
	"fmt"

	"github.com/tawesoft/golib/v2/iter"
	"github.com/tawesoft/golib/v2/must"
	"github.com/tawesoft/golib/v2/operator"
)

func main() {
	// generate a sequence 1, 2, 3, ... 99, 100. This is produced lazily.
	sequence := iter.Take(100, iter.Counter[int](1, 1))

	// reduce applies a function to each element of the sequence. We want
	// addition ("+"), but we need this as a function, so we use operator.Add.
	// Here, [int] is needed to specify which type of the generic function
	// we need. This should match the type of the sequence (in this case, int).
	result := iter.Reduce(0, operator.Add[int], sequence)

	fmt.Printf("sum of numbers from 1 to 100: %d\n", result)

	// Note that the above is given as an example. A better way to sum the
	// numbers from 1 to n is to use Gauss's method or proof by induction and
	// immediately calculate (n+1) * (n/2).
	must.Equal(result, (100+1)*(100/2))

}
Output:

sum of numbers from 1 to 100: 5050

func And

func And[X comparable](p X, q X) bool

And is a logical predicate for performing conjunction.

p q And(p, q)
T T T
T F F
F T F
F F F

func BitwiseAnd

func BitwiseAnd[I constraints.Integer](a I, b I) I

BitwiseAnd returns bitwise AND (i.e. `a & b`) of integer inputs.

func BitwiseNot

func BitwiseNot[I constraints.Integer](i I) I

BitwiseNot returns bitwise complement. This is `m ^ x` with m = "all bits set to 1" for unsigned x, and m = -1 for signed x.

func BitwiseOr

func BitwiseOr[I constraints.Integer](a I, b I) I

BitwiseOr returns bitwise OR (i.e. `a | b`) of integer inputs.

func BitwiseXor

func BitwiseXor[I constraints.Integer](a I, b I) I

BitwiseXor returns bitwise XOR (i.e. `a ^ b`) of integer inputs.

func Cmp

func Cmp[O constraints.Ordered](a O, b O) int

Cmp returns integer 1, 0, or -1 depending on whether a is greater than, equal to, or less than b.

func ConverseImplies

func ConverseImplies[X comparable](p X, q X) bool

ConverseImplies is a logical predicate ConverseImplies(p, q) for "q implies p" (written "q => p" or "p <= q"). That is, if q is true then p must be true.

This is also equivalent to a logical predicate "not p implies not q" (written ¬p => ¬q). That is, if p is false then q must be false.

p q ConverseImplies(p, q)
T T T
T F T
F T F
F F T

func ConverseNotImplies

func ConverseNotImplies[X comparable](p X, q X) bool

ConverseNotImplies is a logical predicate ConverseNotImplies(p, q) for "q does not imply p" (written "q =/=> p" or "p <=/= q"). That is, q is true and p is false.

p q ConverseNotImplies(p, q)
T T F
T F F
F T T
F F F

func Div

func Div[R real](a R, b R) R

Div returns a / b.

func Equal

func Equal[C comparable](a C, b C) bool

Equal returns a == b.

func F added in v2.7.1

func F[X comparable](p X, q X) bool

F is a logical predicate for a contradiction. F(p, q) returns false, regardless of p or q.

p q F(p, q)
T T F
T F F
F T F
F F F

func GT

func GT[O constraints.Ordered](a O, b O) bool

GT returns a > b.

func GTE

func GTE[O constraints.Ordered](a O, b O) bool

GTE returns a >= b.

func Identity

func Identity[X any](x X) X

Identity implements the function f(x) => x.

func Iff

func Iff[X comparable](p X, q X) bool

Iff is a logical predicate Iff(p, q) for the logical biconditional "p iff q" ("p if and only if q"). This is equivalent to Xnor, the negation of Xor. It returns true only if both p and q are true, or both p and q are false.

p q Iff(p, q)
T T T
T F F
F T F
F F T

func Implies

func Implies[X comparable](p X, q X) bool

Implies is a logical predicate Implies(p, q) for "p implies q" (written p => q). That is, if p is true then q must be true.

p q Implies(p, q)
T T T
T F F
F T T
F F T

func In

func In[X comparable](x X, xs ...X) bool

In returns true if x equals any of the following arguments.

func Inv

func Inv[R signed](r R) R

Inv returns (-r)

func IsNegative

func IsNegative[R real](r R) bool

IsNegative returns true iff r <= 0.

func IsNonZero

func IsNonZero[X comparable](x X) bool

IsNonZero returns true iff x is not equal to the zero value of its type.

func IsPositive

func IsPositive[R real](r R) bool

IsPositive returns true iff r >= 0.

func IsStrictlyNegative

func IsStrictlyNegative[R real](r R) bool

IsStrictlyNegative returns true iff r < 0, but not if r == 0.

func IsStrictlyPositive

func IsStrictlyPositive[R real](r R) bool

IsStrictlyPositive returns true iff r > 0, but not if r == 0.

func IsZero

func IsZero[X comparable](x X) bool

IsZero returns true iff x is equal to the zero value of its type.

func LT

func LT[O constraints.Ordered](a O, b O) bool

LT returns a < b.

func LTE

func LTE[O constraints.Ordered](a O, b O) bool

LTE returns a <= b.

func Mod

func Mod[I constraints.Integer](a I, b I) I

Mod returns a mod b (i.e. `a % b`) of integer inputs.

func Mul

func Mul[R real](a R, b R) R

Mul returns a * b.

func Nand

func Nand[X comparable](p X, q X) bool

Nand is a logical predicate that is the inverse of And.

p q Nand(p, q)
T T F
T F T
F T T
F F T

func Nor

func Nor[X comparable](p X, q X) bool

Nor is a logical predicate for neither. It is the inverse of Or.

p q Nor(p, q)
T T F
T F F
F T F
F F T

func Not

func Not[X comparable](x X) bool

Not returns the logical negation of x.

See True for details regarding the truthiness of input values.

x Not(x)
T F
F T

func NotEqual

func NotEqual[C comparable](a C, b C) bool

NotEqual returns a != b.

func NotImplies

func NotImplies[X comparable](p X, q X) bool

NotImplies is a logical predicate for nonimplication. NotImplies(p, q) computes "p does not imply q" (written "p =/=> q" or "¬(p => q)"). That is, p is true and q is false.

p q NotImplies(p, q)
T T F
T F T
F T F
F F F

func NotP

func NotP[X comparable](p X, q X) bool

NotP is a logical predicate. NotP(p, q) returns Not(p), regardless of q.

p q NotP(p, q)
T T F
T F F
F T T
F F T

func NotQ

func NotQ[X comparable](p X, q X) bool

NotQ is a logical predicate. NotQ(p, q) returns Not(q), regardless of p.

p q NotQ(p, q)
T T F
T F T
F T F
F F T

func Or

func Or[X comparable](p X, q X) bool

Or is a logical predicate for performing disjunction.

p q Or(p, q)
T T T
T F T
F T T
F F F

func P

func P[X comparable](p X, q X) bool

P is a logical predicate. P(p, q) returns p, regardless of q.

p q P(p, q)
T T T
T F T
F T F
F F F

func Pow

func Pow[I constraints.Integer](a, b I) I

Pow returns a to the power of b for integer inputs. The result is undefined for negative values of b.

func Q

func Q[X comparable](p X, q X) bool

Q is a logical predicate. Q(p, q) returns q, regardless of p.

p q Q(p, q)
T T T
T F F
F T T
F F F

func ShiftLeft

func ShiftLeft[I constraints.Integer](a I, b I) I

ShiftLeft returns bitwise shift left (i.e. `a << b`) of integer inputs.

func ShiftRight

func ShiftRight[I constraints.Integer](a I, b I) I

ShiftRight returns bitwise shift right (i.e. `a >> b`) of integer inputs.

func Sub

func Sub[R real](a R, b R) R

Sub returns a - b.

func T added in v2.7.1

func T[X comparable](p X, q X) bool

T is a logical predicate for a tautology. T(p, q) returns true, regardless of p or q.

p q T(p, q)
T T T
T F T
F T T
F F T

func Ternary

func Ternary[X any](q bool, a X, b X) X

Ternary returns a if q is true, or b if q is false.

func True

func True[X comparable](x X) bool

True returns true if x represents a truthy value.

An input is considered true iff it is not the zero value for that type.

This means that the boolean value true, non-zero numbers, and non-empty strings are truthy, but the boolean value false, the number zero, and the empty string are not.

func Xor

func Xor[X comparable](p X, q X) bool

Xor is a logical predicate for exclusive or. Xor(p, q) returns true for p or q, but not for both p and q.

p q Xor(p, q)
T T F
T F T
F T T
F F F

func Zero

func Zero[T any]() T

Zero returns the zero value for any type.

Types

This section is empty.

Directories

Path Synopsis
checked
integer
Package integer (operator/checked/integer) implements operations on integers that are robust in the event of integer overflow.
Package integer (operator/checked/integer) implements operations on integers that are robust in the event of integer overflow.

Jump to

Keyboard shortcuts

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