operator

package
Version: v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2021 License: BSD-3-Clause, MIT, MIT-0, MIT-0 Imports: 2 Imported by: 0

README

operator - operators as functions

go get -u "tawesoft.co.uk/go"
import "tawesoft.co.uk/go/operator"
Links License Stable?
homedocssrc MIT-0 ✔ yes

About

Package operator implements logical, arithmetic, bitwise and comparison operators as functions (like the Python operator module). Includes unary, binary, and n-ary functions with overflow checked variants.

Examples

Using operators as function arguments

package main

import (
    "fmt"
    "tawesoft.co.uk/go/operator"
)

func foo(op func(int, int) int, a int, b int) int {
    return op(a, b)
}

func fooChecked(op func(int8, int8) (int8, error), a int8, b int8) (int8, error) {
    return op(a, b)
}

func main() {
    fmt.Println(foo(operator.Int.Binary.Add, 5, 3))
    fmt.Println(foo(operator.Int.Binary.Sub, 5, 3))
    
    var result, err = fooChecked(operator.Int8Checked.Binary.Add, 126, 2) // max int8 is 127!
    if err != nil {
        fmt.Printf("error: %v (expected!)\n", err)
    } else {
        fmt.Println(result)
    }
}

Using operators in lookup tables for a command-line calculator program

package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
    
    "tawesoft.co.uk/go/operator"
)

type checkedOperation func(float64, float64) (float64, error)

var reader = bufio.NewReader(os.Stdin)

var operations = map[string]checkedOperation {
    "+": operator.Float64Checked.Binary.Add,
    "-": operator.Float64Checked.Binary.Sub,
    "*": operator.Float64Checked.Binary.Mul,
    "/": operator.Float64Checked.Binary.Div,
}

func getNumber(prompt string) float64 {
    for {
        fmt.Print(prompt)
        var text, _ = reader.ReadString('\n')
        var result, err = strconv.ParseFloat(strings.TrimSpace(text), 64)
        if err != nil {
            fmt.Println("Sorry, try again. (%v)", err)
            continue
        }
        return result
    }
}

func getOperation(prompt string) checkedOperation {
    for {
        fmt.Print(prompt)
        var text, _ = reader.ReadString('\n')
        var operator, ok = operations[strings.TrimSpace(text)]
        if !ok {
            fmt.Println("Sorry, try again.")
            continue
        }
        return operator
    }
}

func main() {
    var firstNumber = getNumber("Enter a number (then press enter): ")
    var operation = getOperation("Enter +, -, * or / (then press enter) for add, subtract, multiply, or divide: ")
    var secondNumber = getNumber("Enter another number (then press enter): ")
    var result, err = operation(firstNumber, secondNumber)
    if err != nil {
        fmt.Printf("Sorry, something went wrong: %v\n", err)
    } else {
        fmt.Printf("The result is %.2f!\n", result)
    }
}

Getting Help

This package is part of tawesoft.co.uk/go, a monorepo for small Go modules maintained by Tawesoft®. Check out that URL for more information about other Go modules from Tawesoft plus community and commercial support options.

Documentation

Overview

Package operator implements logical, arithmetic, bitwise and comparison operators as functions (like the Python operator module). Includes unary, binary, and n-ary functions with overflow checked variants.

Examples

Using operators as function arguments

https://www.tawesoft.co.uk/go/doc/operator/examples/simple/

Using operators in lookup tables for a command-line calculator program

https://www.tawesoft.co.uk/go/doc/operator/examples/calculator/

Package Information

License: MIT-0 (see LICENSE.txt)

Stable: yes

For more information, documentation, source code, examples, support, links, etc. please see https://www.tawesoft.co.uk/go and https://www.tawesoft.co.uk/go/operator

Index

Constants

This section is empty.

Variables

View Source
var Bool = struct {
	Unary  boolUnary
	Binary boolBinary
	Nary   boolNary
}{
	Unary: boolUnary{
		True:     func(_ bool) bool { return true },
		False:    func(_ bool) bool { return false },
		Identity: func(p bool) bool { return p },
		Not:      func(p bool) bool { return !p },
	},

	Binary: boolBinary{
		True:          func(_ bool, _ bool) bool { return true },
		False:         func(_ bool, _ bool) bool { return false },
		P:             func(p bool, _ bool) bool { return p },
		Q:             func(_ bool, q bool) bool { return q },
		NotP:          func(p bool, _ bool) bool { return !p },
		NotQ:          func(_ bool, q bool) bool { return !q },
		Eq:            func(p bool, q bool) bool { return p == q },
		Neq:           func(p bool, q bool) bool { return p != q },
		And:           func(p bool, q bool) bool { return p && q },
		Nand:          func(p bool, q bool) bool { return !(p && q) },
		Or:            func(p bool, q bool) bool { return p || q },
		Nor:           func(p bool, q bool) bool { return !(p || q) },
		Xor:           func(p bool, q bool) bool { return p != q },
		Xnor:          func(p bool, q bool) bool { return p == q },
		Implies:       func(p bool, q bool) bool { return (!p) || q },
		NonImplies:    func(p bool, q bool) bool { return p && (!q) },
		ConImplies:    func(p bool, q bool) bool { return (!q) || p },
		ConNonImplies: func(p bool, q bool) bool { return q && (!p) },
	},

	Nary: boolNary{
		True:  func(_ ...bool) bool { return true },
		False: func(_ ...bool) bool { return false },
		All:   boolNaryAll1,
		Any:   boolNaryAny1,
		None:  boolNaryNone1,
	},
}

Bool implements operations on one (unary), two (binary), or many (nary) arguments of type bool.

View Source
var ErrorNaN = fmt.Errorf("Not a number (NaN)")

ErrorNaN is the return type used for an operation on a float that is Not a Number in a checked function

View Source
var ErrorOverflow = fmt.Errorf("Overflow")

ErrorOverflow is the return type used for integer overflow in a checked function

View Source
var ErrorUndefined = fmt.Errorf("Undefined operation")

ErrorOverflow is the return type used for an undefined operation in a checked function such as shifting a negative integer left or divide by zero

View Source
var Float32 = struct {
	Unary  float32Unary
	Binary float32Binary
	Nary   float32Nary
	Reduce func(operatorIdentity float32, operator func(float32, float32) float32, elements ...float32) float32
}{
	Unary: float32Unary{
		Identity: func(a float32) float32 { return a },
		Abs:      float32UnaryAbs,
		Negation: func(a float32) float32 { return -a },
		Zero:     func(a float32) bool { return a == 0 },
		NonZero:  func(a float32) bool { return a != 0 },
		Positive: float32UnaryPositive,
		Negative: float32UnaryNegative,
	},

	Binary: float32Binary{
		Add: func(a float32, b float32) float32 { return a + b },
		Sub: func(a float32, b float32) float32 { return a - b },
		Mul: func(a float32, b float32) float32 { return a * b },
		Div: func(a float32, b float32) float32 { return a / b },

		Eq:  func(a float32, b float32) bool { return a == b },
		Neq: func(a float32, b float32) bool { return a != b },
		Lt:  func(a float32, b float32) bool { return a < b },
		Lte: func(a float32, b float32) bool { return a <= b },
		Gt:  func(a float32, b float32) bool { return a > b },
		Gte: func(a float32, b float32) bool { return a >= b },
	},

	Nary: float32Nary{
		Add: float32NaryAdd,
		Mul: float32NaryMul,
	},

	Reduce: float32Reduce,
}

Float32 implements operations on one (unary), two (binary), or many (nary) arguments of type float32.

View Source
var Float32Checked = struct {
	Unary  float32UnaryChecked
	Binary float32BinaryChecked
	Nary   float32NaryChecked
	Reduce func(operatorIdentity float32, operator func(float32, float32) (float32, error), elements ...float32) (float32, error)
}{
	Unary: float32UnaryChecked{
		Abs:      float32UnaryCheckedAbs,
		Negation: float32UnaryCheckedNegation,
	},

	Binary: float32BinaryChecked{
		Add: float32BinaryCheckedAdd,
		Sub: float32BinaryCheckedSub,
		Mul: float32BinaryCheckedMul,
		Div: float32BinaryCheckedDiv,
	},

	Nary: float32NaryChecked{
		Add: float32NaryCheckedAdd,
		Mul: float32NaryCheckedMul,
	},

	Reduce: float32CheckedReduce,
}

Float32Checked implements operations on one (unary), two (binary), or many (nary) arguments of type float32, returning an error in cases such as overflow or an undefined operation.

View Source
var Float64 = struct {
	Unary  float64Unary
	Binary float64Binary
	Nary   float64Nary
	Reduce func(operatorIdentity float64, operator func(float64, float64) float64, elements ...float64) float64
}{
	Unary: float64Unary{
		Identity: func(a float64) float64 { return a },
		Abs:      float64UnaryAbs,
		Negation: func(a float64) float64 { return -a },
		Zero:     func(a float64) bool { return a == 0 },
		NonZero:  func(a float64) bool { return a != 0 },
		Positive: float64UnaryPositive,
		Negative: float64UnaryNegative,
	},

	Binary: float64Binary{
		Add: func(a float64, b float64) float64 { return a + b },
		Sub: func(a float64, b float64) float64 { return a - b },
		Mul: func(a float64, b float64) float64 { return a * b },
		Div: func(a float64, b float64) float64 { return a / b },

		Eq:  func(a float64, b float64) bool { return a == b },
		Neq: func(a float64, b float64) bool { return a != b },
		Lt:  func(a float64, b float64) bool { return a < b },
		Lte: func(a float64, b float64) bool { return a <= b },
		Gt:  func(a float64, b float64) bool { return a > b },
		Gte: func(a float64, b float64) bool { return a >= b },
	},

	Nary: float64Nary{
		Add: float64NaryAdd,
		Mul: float64NaryMul,
	},

	Reduce: float64Reduce,
}

Float64 implements operations on one (unary), two (binary), or many (nary) arguments of type float64.

View Source
var Float64Checked = struct {
	Unary  float64UnaryChecked
	Binary float64BinaryChecked
	Nary   float64NaryChecked
	Reduce func(operatorIdentity float64, operator func(float64, float64) (float64, error), elements ...float64) (float64, error)
}{
	Unary: float64UnaryChecked{
		Abs:      float64UnaryCheckedAbs,
		Negation: float64UnaryCheckedNegation,
	},

	Binary: float64BinaryChecked{
		Add: float64BinaryCheckedAdd,
		Sub: float64BinaryCheckedSub,
		Mul: float64BinaryCheckedMul,
		Div: float64BinaryCheckedDiv,
	},

	Nary: float64NaryChecked{
		Add: float64NaryCheckedAdd,
		Mul: float64NaryCheckedMul,
	},

	Reduce: float64CheckedReduce,
}

Float64Checked implements operations on one (unary), two (binary), or many (nary) arguments of type float64, returning an error in cases such as overflow or an undefined operation.

View Source
var Int = struct {
	Unary  intUnary
	Binary intBinary
	Nary   intNary
	Reduce func(operatorIdentity int, operator func(int, int) int, elements ...int) int
}{
	Unary: intUnary{
		Identity: func(a int) int { return a },
		Abs:      intUnaryAbs,
		Negation: func(a int) int { return -a },
		Zero:     func(a int) bool { return a == 0 },
		NonZero:  func(a int) bool { return a != 0 },
		Positive: intUnaryPositive,
		Negative: intUnaryNegative,
	},

	Binary: intBinary{
		Add: func(a int, b int) int { return a + b },
		Sub: func(a int, b int) int { return a - b },
		Mul: func(a int, b int) int { return a * b },
		Div: func(a int, b int) int { return a / b },

		Eq:  func(a int, b int) bool { return a == b },
		Neq: func(a int, b int) bool { return a != b },
		Lt:  func(a int, b int) bool { return a < b },
		Lte: func(a int, b int) bool { return a <= b },
		Gt:  func(a int, b int) bool { return a > b },
		Gte: func(a int, b int) bool { return a >= b },

		And:    func(a int, b int) int { return a & b },
		Or:     func(a int, b int) int { return a | b },
		Xor:    func(a int, b int) int { return a ^ b },
		AndNot: func(a int, b int) int { return a &^ b },
		Mod:    func(a int, b int) int { return a % b },

		Shl: func(a int, b uint) int { return a << b },
		Shr: func(a int, b uint) int { return a >> b },
	},

	Nary: intNary{
		Add: intNaryAdd,
		Mul: intNaryMul,
	},

	Reduce: intReduce,
}

Int implements operations on one (unary), two (binary), or many (nary) arguments of type int.

View Source
var Int16 = struct {
	Unary  int16Unary
	Binary int16Binary
	Nary   int16Nary
	Reduce func(operatorIdentity int16, operator func(int16, int16) int16, elements ...int16) int16
}{
	Unary: int16Unary{
		Identity: func(a int16) int16 { return a },
		Abs:      int16UnaryAbs,
		Negation: func(a int16) int16 { return -a },
		Zero:     func(a int16) bool { return a == 0 },
		NonZero:  func(a int16) bool { return a != 0 },
		Positive: int16UnaryPositive,
		Negative: int16UnaryNegative,
	},

	Binary: int16Binary{
		Add: func(a int16, b int16) int16 { return a + b },
		Sub: func(a int16, b int16) int16 { return a - b },
		Mul: func(a int16, b int16) int16 { return a * b },
		Div: func(a int16, b int16) int16 { return a / b },

		Eq:  func(a int16, b int16) bool { return a == b },
		Neq: func(a int16, b int16) bool { return a != b },
		Lt:  func(a int16, b int16) bool { return a < b },
		Lte: func(a int16, b int16) bool { return a <= b },
		Gt:  func(a int16, b int16) bool { return a > b },
		Gte: func(a int16, b int16) bool { return a >= b },

		And:    func(a int16, b int16) int16 { return a & b },
		Or:     func(a int16, b int16) int16 { return a | b },
		Xor:    func(a int16, b int16) int16 { return a ^ b },
		AndNot: func(a int16, b int16) int16 { return a &^ b },
		Mod:    func(a int16, b int16) int16 { return a % b },

		Shl: func(a int16, b uint) int16 { return a << b },
		Shr: func(a int16, b uint) int16 { return a >> b },
	},

	Nary: int16Nary{
		Add: int16NaryAdd,
		Mul: int16NaryMul,
	},

	Reduce: int16Reduce,
}

Int16 implements operations on one (unary), two (binary), or many (nary) arguments of type int16.

View Source
var Int16Checked = struct {
	Unary  int16UnaryChecked
	Binary int16BinaryChecked
	Nary   int16NaryChecked
	Reduce func(operatorIdentity int16, operator func(int16, int16) (int16, error), elements ...int16) (int16, error)
}{
	Unary: int16UnaryChecked{
		Abs:      int16UnaryCheckedAbs,
		Negation: int16UnaryCheckedNegation,
	},

	Binary: int16BinaryChecked{
		Add: int16BinaryCheckedAdd,
		Sub: int16BinaryCheckedSub,
		Mul: int16BinaryCheckedMul,
		Div: int16BinaryCheckedDiv,
		Shl: int16BinaryCheckedShl,
	},

	Nary: int16NaryChecked{
		Add: int16NaryCheckedAdd,
		Mul: int16NaryCheckedMul,
	},

	Reduce: int16CheckedReduce,
}

Int16Checked implements operations on one (unary), two (binary), or many (nary) arguments of type int16, returning an error in cases such as overflow or an undefined operation.

View Source
var Int32 = struct {
	Unary  int32Unary
	Binary int32Binary
	Nary   int32Nary
	Reduce func(operatorIdentity int32, operator func(int32, int32) int32, elements ...int32) int32
}{
	Unary: int32Unary{
		Identity: func(a int32) int32 { return a },
		Abs:      int32UnaryAbs,
		Negation: func(a int32) int32 { return -a },
		Zero:     func(a int32) bool { return a == 0 },
		NonZero:  func(a int32) bool { return a != 0 },
		Positive: int32UnaryPositive,
		Negative: int32UnaryNegative,
	},

	Binary: int32Binary{
		Add: func(a int32, b int32) int32 { return a + b },
		Sub: func(a int32, b int32) int32 { return a - b },
		Mul: func(a int32, b int32) int32 { return a * b },
		Div: func(a int32, b int32) int32 { return a / b },

		Eq:  func(a int32, b int32) bool { return a == b },
		Neq: func(a int32, b int32) bool { return a != b },
		Lt:  func(a int32, b int32) bool { return a < b },
		Lte: func(a int32, b int32) bool { return a <= b },
		Gt:  func(a int32, b int32) bool { return a > b },
		Gte: func(a int32, b int32) bool { return a >= b },

		And:    func(a int32, b int32) int32 { return a & b },
		Or:     func(a int32, b int32) int32 { return a | b },
		Xor:    func(a int32, b int32) int32 { return a ^ b },
		AndNot: func(a int32, b int32) int32 { return a &^ b },
		Mod:    func(a int32, b int32) int32 { return a % b },

		Shl: func(a int32, b uint) int32 { return a << b },
		Shr: func(a int32, b uint) int32 { return a >> b },
	},

	Nary: int32Nary{
		Add: int32NaryAdd,
		Mul: int32NaryMul,
	},

	Reduce: int32Reduce,
}

Int32 implements operations on one (unary), two (binary), or many (nary) arguments of type int32.

View Source
var Int32Checked = struct {
	Unary  int32UnaryChecked
	Binary int32BinaryChecked
	Nary   int32NaryChecked
	Reduce func(operatorIdentity int32, operator func(int32, int32) (int32, error), elements ...int32) (int32, error)
}{
	Unary: int32UnaryChecked{
		Abs:      int32UnaryCheckedAbs,
		Negation: int32UnaryCheckedNegation,
	},

	Binary: int32BinaryChecked{
		Add: int32BinaryCheckedAdd,
		Sub: int32BinaryCheckedSub,
		Mul: int32BinaryCheckedMul,
		Div: int32BinaryCheckedDiv,
		Shl: int32BinaryCheckedShl,
	},

	Nary: int32NaryChecked{
		Add: int32NaryCheckedAdd,
		Mul: int32NaryCheckedMul,
	},

	Reduce: int32CheckedReduce,
}

Int32Checked implements operations on one (unary), two (binary), or many (nary) arguments of type int32, returning an error in cases such as overflow or an undefined operation.

View Source
var Int64 = struct {
	Unary  int64Unary
	Binary int64Binary
	Nary   int64Nary
	Reduce func(operatorIdentity int64, operator func(int64, int64) int64, elements ...int64) int64
}{
	Unary: int64Unary{
		Identity: func(a int64) int64 { return a },
		Abs:      int64UnaryAbs,
		Negation: func(a int64) int64 { return -a },
		Zero:     func(a int64) bool { return a == 0 },
		NonZero:  func(a int64) bool { return a != 0 },
		Positive: int64UnaryPositive,
		Negative: int64UnaryNegative,
	},

	Binary: int64Binary{
		Add: func(a int64, b int64) int64 { return a + b },
		Sub: func(a int64, b int64) int64 { return a - b },
		Mul: func(a int64, b int64) int64 { return a * b },
		Div: func(a int64, b int64) int64 { return a / b },

		Eq:  func(a int64, b int64) bool { return a == b },
		Neq: func(a int64, b int64) bool { return a != b },
		Lt:  func(a int64, b int64) bool { return a < b },
		Lte: func(a int64, b int64) bool { return a <= b },
		Gt:  func(a int64, b int64) bool { return a > b },
		Gte: func(a int64, b int64) bool { return a >= b },

		And:    func(a int64, b int64) int64 { return a & b },
		Or:     func(a int64, b int64) int64 { return a | b },
		Xor:    func(a int64, b int64) int64 { return a ^ b },
		AndNot: func(a int64, b int64) int64 { return a &^ b },
		Mod:    func(a int64, b int64) int64 { return a % b },

		Shl: func(a int64, b uint) int64 { return a << b },
		Shr: func(a int64, b uint) int64 { return a >> b },
	},

	Nary: int64Nary{
		Add: int64NaryAdd,
		Mul: int64NaryMul,
	},

	Reduce: int64Reduce,
}

Int64 implements operations on one (unary), two (binary), or many (nary) arguments of type int64.

View Source
var Int64Checked = struct {
	Unary  int64UnaryChecked
	Binary int64BinaryChecked
	Nary   int64NaryChecked
	Reduce func(operatorIdentity int64, operator func(int64, int64) (int64, error), elements ...int64) (int64, error)
}{
	Unary: int64UnaryChecked{
		Abs:      int64UnaryCheckedAbs,
		Negation: int64UnaryCheckedNegation,
	},

	Binary: int64BinaryChecked{
		Add: int64BinaryCheckedAdd,
		Sub: int64BinaryCheckedSub,
		Mul: int64BinaryCheckedMul,
		Div: int64BinaryCheckedDiv,
		Shl: int64BinaryCheckedShl,
	},

	Nary: int64NaryChecked{
		Add: int64NaryCheckedAdd,
		Mul: int64NaryCheckedMul,
	},

	Reduce: int64CheckedReduce,
}

Int64Checked implements operations on one (unary), two (binary), or many (nary) arguments of type int64, returning an error in cases such as overflow or an undefined operation.

View Source
var Int8 = struct {
	Unary  int8Unary
	Binary int8Binary
	Nary   int8Nary
	Reduce func(operatorIdentity int8, operator func(int8, int8) int8, elements ...int8) int8
}{
	Unary: int8Unary{
		Identity: func(a int8) int8 { return a },
		Abs:      int8UnaryAbs,
		Negation: func(a int8) int8 { return -a },
		Zero:     func(a int8) bool { return a == 0 },
		NonZero:  func(a int8) bool { return a != 0 },
		Positive: int8UnaryPositive,
		Negative: int8UnaryNegative,
	},

	Binary: int8Binary{
		Add: func(a int8, b int8) int8 { return a + b },
		Sub: func(a int8, b int8) int8 { return a - b },
		Mul: func(a int8, b int8) int8 { return a * b },
		Div: func(a int8, b int8) int8 { return a / b },

		Eq:  func(a int8, b int8) bool { return a == b },
		Neq: func(a int8, b int8) bool { return a != b },
		Lt:  func(a int8, b int8) bool { return a < b },
		Lte: func(a int8, b int8) bool { return a <= b },
		Gt:  func(a int8, b int8) bool { return a > b },
		Gte: func(a int8, b int8) bool { return a >= b },

		And:    func(a int8, b int8) int8 { return a & b },
		Or:     func(a int8, b int8) int8 { return a | b },
		Xor:    func(a int8, b int8) int8 { return a ^ b },
		AndNot: func(a int8, b int8) int8 { return a &^ b },
		Mod:    func(a int8, b int8) int8 { return a % b },

		Shl: func(a int8, b uint) int8 { return a << b },
		Shr: func(a int8, b uint) int8 { return a >> b },
	},

	Nary: int8Nary{
		Add: int8NaryAdd,
		Mul: int8NaryMul,
	},

	Reduce: int8Reduce,
}

Int8 implements operations on one (unary), two (binary), or many (nary) arguments of type int8.

View Source
var Int8Checked = struct {
	Unary  int8UnaryChecked
	Binary int8BinaryChecked
	Nary   int8NaryChecked
	Reduce func(operatorIdentity int8, operator func(int8, int8) (int8, error), elements ...int8) (int8, error)
}{
	Unary: int8UnaryChecked{
		Abs:      int8UnaryCheckedAbs,
		Negation: int8UnaryCheckedNegation,
	},

	Binary: int8BinaryChecked{
		Add: int8BinaryCheckedAdd,
		Sub: int8BinaryCheckedSub,
		Mul: int8BinaryCheckedMul,
		Div: int8BinaryCheckedDiv,
		Shl: int8BinaryCheckedShl,
	},

	Nary: int8NaryChecked{
		Add: int8NaryCheckedAdd,
		Mul: int8NaryCheckedMul,
	},

	Reduce: int8CheckedReduce,
}

Int8Checked implements operations on one (unary), two (binary), or many (nary) arguments of type int8, returning an error in cases such as overflow or an undefined operation.

View Source
var IntChecked = struct {
	Unary  intUnaryChecked
	Binary intBinaryChecked
	Nary   intNaryChecked
	Reduce func(operatorIdentity int, operator func(int, int) (int, error), elements ...int) (int, error)
}{
	Unary: intUnaryChecked{
		Abs:      intUnaryCheckedAbs,
		Negation: intUnaryCheckedNegation,
	},

	Binary: intBinaryChecked{
		Add: intBinaryCheckedAdd,
		Sub: intBinaryCheckedSub,
		Mul: intBinaryCheckedMul,
		Div: intBinaryCheckedDiv,
		Shl: intBinaryCheckedShl,
	},

	Nary: intNaryChecked{
		Add: intNaryCheckedAdd,
		Mul: intNaryCheckedMul,
	},

	Reduce: intCheckedReduce,
}

IntChecked implements operations on one (unary), two (binary), or many (nary) arguments of type int, returning an error in cases such as overflow or an undefined operation.

View Source
var Uint = struct {
	Unary  uintUnary
	Binary uintBinary
	Nary   uintNary
	Reduce func(operatorIdentity uint, operator func(uint, uint) uint, elements ...uint) uint
}{
	Unary: uintUnary{
		Identity: func(a uint) uint { return a },
		Not:      func(a uint) uint { return ^a },
		Zero:     func(a uint) bool { return a == 0 },
		NonZero:  func(a uint) bool { return a != 0 },
		Positive: uintUnaryPositive,
		Negative: uintUnaryNegative,
	},

	Binary: uintBinary{
		Add: func(a uint, b uint) uint { return a + b },
		Sub: func(a uint, b uint) uint { return a - b },
		Mul: func(a uint, b uint) uint { return a * b },
		Div: func(a uint, b uint) uint { return a / b },

		Eq:  func(a uint, b uint) bool { return a == b },
		Neq: func(a uint, b uint) bool { return a != b },
		Lt:  func(a uint, b uint) bool { return a < b },
		Lte: func(a uint, b uint) bool { return a <= b },
		Gt:  func(a uint, b uint) bool { return a > b },
		Gte: func(a uint, b uint) bool { return a >= b },

		And:    func(a uint, b uint) uint { return a & b },
		Or:     func(a uint, b uint) uint { return a | b },
		Xor:    func(a uint, b uint) uint { return a ^ b },
		AndNot: func(a uint, b uint) uint { return a &^ b },
		Mod:    func(a uint, b uint) uint { return a % b },

		Shl: func(a uint, b uint) uint { return a << b },
		Shr: func(a uint, b uint) uint { return a >> b },
	},

	Nary: uintNary{
		Add: uintNaryAdd,
		Mul: uintNaryMul,
	},

	Reduce: uintReduce,
}

Uint implements operations on one (unary), two (binary), or many (nary) arguments of type uint.

View Source
var Uint16 = struct {
	Unary  uint16Unary
	Binary uint16Binary
	Nary   uint16Nary
	Reduce func(operatorIdentity uint16, operator func(uint16, uint16) uint16, elements ...uint16) uint16
}{
	Unary: uint16Unary{
		Identity: func(a uint16) uint16 { return a },
		Not:      func(a uint16) uint16 { return ^a },
		Zero:     func(a uint16) bool { return a == 0 },
		NonZero:  func(a uint16) bool { return a != 0 },
		Positive: uint16UnaryPositive,
		Negative: uint16UnaryNegative,
	},

	Binary: uint16Binary{
		Add: func(a uint16, b uint16) uint16 { return a + b },
		Sub: func(a uint16, b uint16) uint16 { return a - b },
		Mul: func(a uint16, b uint16) uint16 { return a * b },
		Div: func(a uint16, b uint16) uint16 { return a / b },

		Eq:  func(a uint16, b uint16) bool { return a == b },
		Neq: func(a uint16, b uint16) bool { return a != b },
		Lt:  func(a uint16, b uint16) bool { return a < b },
		Lte: func(a uint16, b uint16) bool { return a <= b },
		Gt:  func(a uint16, b uint16) bool { return a > b },
		Gte: func(a uint16, b uint16) bool { return a >= b },

		And:    func(a uint16, b uint16) uint16 { return a & b },
		Or:     func(a uint16, b uint16) uint16 { return a | b },
		Xor:    func(a uint16, b uint16) uint16 { return a ^ b },
		AndNot: func(a uint16, b uint16) uint16 { return a &^ b },
		Mod:    func(a uint16, b uint16) uint16 { return a % b },

		Shl: func(a uint16, b uint) uint16 { return a << b },
		Shr: func(a uint16, b uint) uint16 { return a >> b },
	},

	Nary: uint16Nary{
		Add: uint16NaryAdd,
		Mul: uint16NaryMul,
	},

	Reduce: uint16Reduce,
}

Uint16 implements operations on one (unary), two (binary), or many (nary) arguments of type uint16.

View Source
var Uint16Checked = struct {
	Unary  uint16UnaryChecked
	Binary uint16BinaryChecked
	Nary   uint16NaryChecked
	Reduce func(operatorIdentity uint16, operator func(uint16, uint16) (uint16, error), elements ...uint16) (uint16, error)
}{
	Unary: uint16UnaryChecked{},

	Binary: uint16BinaryChecked{
		Add: uint16BinaryCheckedAdd,
		Sub: uint16BinaryCheckedSub,
		Mul: uint16BinaryCheckedMul,
		Div: uint16BinaryCheckedDiv,
		Shl: uint16BinaryCheckedShl,
	},

	Nary: uint16NaryChecked{
		Add: uint16NaryCheckedAdd,
		Mul: uint16NaryCheckedMul,
	},

	Reduce: uint16CheckedReduce,
}

Uint16Checked implements operations on one (unary), two (binary), or many (nary) arguments of type uint16, returning an error in cases such as overflow or an undefined operation.

View Source
var Uint32 = struct {
	Unary  uint32Unary
	Binary uint32Binary
	Nary   uint32Nary
	Reduce func(operatorIdentity uint32, operator func(uint32, uint32) uint32, elements ...uint32) uint32
}{
	Unary: uint32Unary{
		Identity: func(a uint32) uint32 { return a },
		Not:      func(a uint32) uint32 { return ^a },
		Zero:     func(a uint32) bool { return a == 0 },
		NonZero:  func(a uint32) bool { return a != 0 },
		Positive: uint32UnaryPositive,
		Negative: uint32UnaryNegative,
	},

	Binary: uint32Binary{
		Add: func(a uint32, b uint32) uint32 { return a + b },
		Sub: func(a uint32, b uint32) uint32 { return a - b },
		Mul: func(a uint32, b uint32) uint32 { return a * b },
		Div: func(a uint32, b uint32) uint32 { return a / b },

		Eq:  func(a uint32, b uint32) bool { return a == b },
		Neq: func(a uint32, b uint32) bool { return a != b },
		Lt:  func(a uint32, b uint32) bool { return a < b },
		Lte: func(a uint32, b uint32) bool { return a <= b },
		Gt:  func(a uint32, b uint32) bool { return a > b },
		Gte: func(a uint32, b uint32) bool { return a >= b },

		And:    func(a uint32, b uint32) uint32 { return a & b },
		Or:     func(a uint32, b uint32) uint32 { return a | b },
		Xor:    func(a uint32, b uint32) uint32 { return a ^ b },
		AndNot: func(a uint32, b uint32) uint32 { return a &^ b },
		Mod:    func(a uint32, b uint32) uint32 { return a % b },

		Shl: func(a uint32, b uint) uint32 { return a << b },
		Shr: func(a uint32, b uint) uint32 { return a >> b },
	},

	Nary: uint32Nary{
		Add: uint32NaryAdd,
		Mul: uint32NaryMul,
	},

	Reduce: uint32Reduce,
}

Uint32 implements operations on one (unary), two (binary), or many (nary) arguments of type uint32.

View Source
var Uint32Checked = struct {
	Unary  uint32UnaryChecked
	Binary uint32BinaryChecked
	Nary   uint32NaryChecked
	Reduce func(operatorIdentity uint32, operator func(uint32, uint32) (uint32, error), elements ...uint32) (uint32, error)
}{
	Unary: uint32UnaryChecked{},

	Binary: uint32BinaryChecked{
		Add: uint32BinaryCheckedAdd,
		Sub: uint32BinaryCheckedSub,
		Mul: uint32BinaryCheckedMul,
		Div: uint32BinaryCheckedDiv,
		Shl: uint32BinaryCheckedShl,
	},

	Nary: uint32NaryChecked{
		Add: uint32NaryCheckedAdd,
		Mul: uint32NaryCheckedMul,
	},

	Reduce: uint32CheckedReduce,
}

Uint32Checked implements operations on one (unary), two (binary), or many (nary) arguments of type uint32, returning an error in cases such as overflow or an undefined operation.

View Source
var Uint64 = struct {
	Unary  uint64Unary
	Binary uint64Binary
	Nary   uint64Nary
	Reduce func(operatorIdentity uint64, operator func(uint64, uint64) uint64, elements ...uint64) uint64
}{
	Unary: uint64Unary{
		Identity: func(a uint64) uint64 { return a },
		Not:      func(a uint64) uint64 { return ^a },
		Zero:     func(a uint64) bool { return a == 0 },
		NonZero:  func(a uint64) bool { return a != 0 },
		Positive: uint64UnaryPositive,
		Negative: uint64UnaryNegative,
	},

	Binary: uint64Binary{
		Add: func(a uint64, b uint64) uint64 { return a + b },
		Sub: func(a uint64, b uint64) uint64 { return a - b },
		Mul: func(a uint64, b uint64) uint64 { return a * b },
		Div: func(a uint64, b uint64) uint64 { return a / b },

		Eq:  func(a uint64, b uint64) bool { return a == b },
		Neq: func(a uint64, b uint64) bool { return a != b },
		Lt:  func(a uint64, b uint64) bool { return a < b },
		Lte: func(a uint64, b uint64) bool { return a <= b },
		Gt:  func(a uint64, b uint64) bool { return a > b },
		Gte: func(a uint64, b uint64) bool { return a >= b },

		And:    func(a uint64, b uint64) uint64 { return a & b },
		Or:     func(a uint64, b uint64) uint64 { return a | b },
		Xor:    func(a uint64, b uint64) uint64 { return a ^ b },
		AndNot: func(a uint64, b uint64) uint64 { return a &^ b },
		Mod:    func(a uint64, b uint64) uint64 { return a % b },

		Shl: func(a uint64, b uint) uint64 { return a << b },
		Shr: func(a uint64, b uint) uint64 { return a >> b },
	},

	Nary: uint64Nary{
		Add: uint64NaryAdd,
		Mul: uint64NaryMul,
	},

	Reduce: uint64Reduce,
}

Uint64 implements operations on one (unary), two (binary), or many (nary) arguments of type uint64.

View Source
var Uint64Checked = struct {
	Unary  uint64UnaryChecked
	Binary uint64BinaryChecked
	Nary   uint64NaryChecked
	Reduce func(operatorIdentity uint64, operator func(uint64, uint64) (uint64, error), elements ...uint64) (uint64, error)
}{
	Unary: uint64UnaryChecked{},

	Binary: uint64BinaryChecked{
		Add: uint64BinaryCheckedAdd,
		Sub: uint64BinaryCheckedSub,
		Mul: uint64BinaryCheckedMul,
		Div: uint64BinaryCheckedDiv,
		Shl: uint64BinaryCheckedShl,
	},

	Nary: uint64NaryChecked{
		Add: uint64NaryCheckedAdd,
		Mul: uint64NaryCheckedMul,
	},

	Reduce: uint64CheckedReduce,
}

Uint64Checked implements operations on one (unary), two (binary), or many (nary) arguments of type uint64, returning an error in cases such as overflow or an undefined operation.

View Source
var Uint8 = struct {
	Unary  uint8Unary
	Binary uint8Binary
	Nary   uint8Nary
	Reduce func(operatorIdentity uint8, operator func(uint8, uint8) uint8, elements ...uint8) uint8
}{
	Unary: uint8Unary{
		Identity: func(a uint8) uint8 { return a },
		Not:      func(a uint8) uint8 { return ^a },
		Zero:     func(a uint8) bool { return a == 0 },
		NonZero:  func(a uint8) bool { return a != 0 },
		Positive: uint8UnaryPositive,
		Negative: uint8UnaryNegative,
	},

	Binary: uint8Binary{
		Add: func(a uint8, b uint8) uint8 { return a + b },
		Sub: func(a uint8, b uint8) uint8 { return a - b },
		Mul: func(a uint8, b uint8) uint8 { return a * b },
		Div: func(a uint8, b uint8) uint8 { return a / b },

		Eq:  func(a uint8, b uint8) bool { return a == b },
		Neq: func(a uint8, b uint8) bool { return a != b },
		Lt:  func(a uint8, b uint8) bool { return a < b },
		Lte: func(a uint8, b uint8) bool { return a <= b },
		Gt:  func(a uint8, b uint8) bool { return a > b },
		Gte: func(a uint8, b uint8) bool { return a >= b },

		And:    func(a uint8, b uint8) uint8 { return a & b },
		Or:     func(a uint8, b uint8) uint8 { return a | b },
		Xor:    func(a uint8, b uint8) uint8 { return a ^ b },
		AndNot: func(a uint8, b uint8) uint8 { return a &^ b },
		Mod:    func(a uint8, b uint8) uint8 { return a % b },

		Shl: func(a uint8, b uint) uint8 { return a << b },
		Shr: func(a uint8, b uint) uint8 { return a >> b },
	},

	Nary: uint8Nary{
		Add: uint8NaryAdd,
		Mul: uint8NaryMul,
	},

	Reduce: uint8Reduce,
}

Uint8 implements operations on one (unary), two (binary), or many (nary) arguments of type uint8.

View Source
var Uint8Checked = struct {
	Unary  uint8UnaryChecked
	Binary uint8BinaryChecked
	Nary   uint8NaryChecked
	Reduce func(operatorIdentity uint8, operator func(uint8, uint8) (uint8, error), elements ...uint8) (uint8, error)
}{
	Unary: uint8UnaryChecked{},

	Binary: uint8BinaryChecked{
		Add: uint8BinaryCheckedAdd,
		Sub: uint8BinaryCheckedSub,
		Mul: uint8BinaryCheckedMul,
		Div: uint8BinaryCheckedDiv,
		Shl: uint8BinaryCheckedShl,
	},

	Nary: uint8NaryChecked{
		Add: uint8NaryCheckedAdd,
		Mul: uint8NaryCheckedMul,
	},

	Reduce: uint8CheckedReduce,
}

Uint8Checked implements operations on one (unary), two (binary), or many (nary) arguments of type uint8, returning an error in cases such as overflow or an undefined operation.

View Source
var UintChecked = struct {
	Unary  uintUnaryChecked
	Binary uintBinaryChecked
	Nary   uintNaryChecked
	Reduce func(operatorIdentity uint, operator func(uint, uint) (uint, error), elements ...uint) (uint, error)
}{
	Unary: uintUnaryChecked{},

	Binary: uintBinaryChecked{
		Add: uintBinaryCheckedAdd,
		Sub: uintBinaryCheckedSub,
		Mul: uintBinaryCheckedMul,
		Div: uintBinaryCheckedDiv,
		Shl: uintBinaryCheckedShl,
	},

	Nary: uintNaryChecked{
		Add: uintNaryCheckedAdd,
		Mul: uintNaryCheckedMul,
	},

	Reduce: uintCheckedReduce,
}

UintChecked implements operations on one (unary), two (binary), or many (nary) arguments of type uint, returning an error in cases such as overflow or an undefined operation.

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
examples
calculator
Using operators in lookup tables for a command-line calculator program
Using operators in lookup tables for a command-line calculator program
simple
Using operators as function arguments
Using operators as function arguments

Jump to

Keyboard shortcuts

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