Documentation ¶
Overview ¶
Package operator implements builtin language operators, such as "==" (equals) or "+" (addition), as functions that can be passed to higher order functions.
The `operator/checked` subpackage implements operators that are robust against integer overflow.
The `operator/reflect` subpackage implements operators that require reflection.
Index ¶
- func Abs[R Number](r R) R
- func Add[R Number](a R, b R) R
- func And[X comparable](p X, q X) bool
- func BitwiseAnd[I constraints.Integer](a I, b I) I
- func BitwiseNot[I constraints.Integer](i I) I
- func BitwiseOr[I constraints.Integer](a I, b I) I
- func BitwiseXor[I constraints.Integer](a I, b I) I
- func Cmp[O constraints.Ordered](a O, b O) int
- func ConverseImplies[X comparable](p X, q X) bool
- func ConverseNotImplies[X comparable](p X, q X) bool
- func Div[R Number](a R, b R) R
- func Equal[C comparable](a C, b C) bool
- func F[X comparable](p X, q X) bool
- func GT[O constraints.Ordered](a O, b O) bool
- func GTE[O constraints.Ordered](a O, b O) bool
- func Identity[X any](x X) X
- func Iff[X comparable](p X, q X) bool
- func Implies[X comparable](p X, q X) bool
- func In[X comparable](x X, xs ...X) bool
- func Inv[R Signed](r R) R
- func IsNegative[R Number](r R) bool
- func IsNonZero[X comparable](x X) bool
- func IsPositive[R Number](r R) bool
- func IsStrictlyNegative[R Number](r R) bool
- func IsStrictlyPositive[R Number](r R) bool
- func IsZero[X comparable](x X) bool
- func LT[O constraints.Ordered](a O, b O) bool
- func LTE[O constraints.Ordered](a O, b O) bool
- func Mod[I constraints.Integer](a I, b I) I
- func Mul[R Number](a R, b R) R
- func Nand[X comparable](p X, q X) bool
- func Nor[X comparable](p X, q X) bool
- func Not[X comparable](x X) bool
- func NotEqual[C comparable](a C, b C) bool
- func NotImplies[X comparable](p X, q X) bool
- func NotP[X comparable](p X, q X) bool
- func NotQ[X comparable](p X, q X) bool
- func Or[X comparable](p X, q X) bool
- func P[X comparable](p X, q X) bool
- func Pow[I constraints.Integer](a, b I) I
- func Q[X comparable](p X, q X) bool
- func ShiftLeft[I constraints.Integer](a I, b I) I
- func ShiftRight[I constraints.Integer](a I, b I) I
- func Sub[R Number](a R, b R) R
- func T[X comparable](p X, q X) bool
- func Ternary[X any](q bool, a X, b X) X
- func True[X comparable](x X) bool
- func Xor[X comparable](p X, q X) bool
- func Zero[T any]() T
- type Number
- type Signed
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Add ¶
func Add[R Number](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. If q is false, then the statement is true regardless of p.
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 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 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. If p is false, then the statement is true regardless of q.
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 IsNonZero ¶
func IsNonZero[X comparable](x X) bool
IsNonZero returns true iff x is not equal to the zero value of its type.
func IsStrictlyNegative ¶
IsStrictlyNegative returns true iff r < 0.
func IsStrictlyPositive ¶
IsStrictlyPositive returns true iff 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 Mod ¶
func Mod[I constraints.Integer](a I, b I) I
Mod returns a mod b (i.e. `a % b`) of integer inputs.
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 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.
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 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 ¶
Ternary returns a if q is true, or b if q is false.
Note that this does not short-circuit, so both arguments are evaluated as inputs to this function.
func True ¶
func True[X comparable](x X) bool
True returns true if x represents a truthy value. An input is considered truthy iff it is not compare equal to the zero value for that type.
Note that, for example, an empty string and a nil string both compare equal in Go.
x True(x) T T F F
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
Types ¶
type Number ¶ added in v2.13.0
type Number interface { constraints.Integer | constraints.Float }
Number represents any number type that you can support arithmetic using standard Go operators (like a + b, or a ^ b) - i.e. integers & floats.
type Signed ¶ added in v2.13.0
type Signed interface { constraints.Signed | constraints.Float }
Signed represents any number type that may encode both positive and negative values and that support arithmetic on using standard Go operators (like a + b, or a ^ b) - i.e. signed integers and floats.