package
0.13.0
Repository: https://github.com/tawesoft/go.git
Documentation: pkg.go.dev

# README

operator - operators as functions

go get -u "tawesoft.co.uk/go"
import "tawesoft.co.uk/go/operator"

##FROZEN - PLEASE MIGRATE

These packages are moving to https://github.com/tawesoft/golib.

This is to increase security against possible supply chain attacks such as our domain name expiring in the future and being registered by someone else.

Please migrate to https://github.com/tawesoft/golib (when available) instead.

Most programs relying on a package in this monorepo, such as the dialog or lxstrconv packages, will continue to work for the foreseeable future.

Rarely used packages have been hidden for now - they are in the git commit history at https://github.com/tawesoft/go if you need to resurrect one.

LinksLicenseStable?
homedocssrcMIT-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.

# Packages

No description provided by the author

# Variables

Bool implements operations on one (unary), two (binary), or many (nary) arguments of type bool.
ErrorNaN is the return type used for an operation on a float that is Not a Number in a checked function.
ErrorOverflow is the return type used for integer overflow in a checked function.
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.
Float32 implements operations on one (unary), two (binary), or many (nary) arguments of type float32.
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.
Float64 implements operations on one (unary), two (binary), or many (nary) arguments of type float64.
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.
Int implements operations on one (unary), two (binary), or many (nary) arguments of type int.
Int16 implements operations on one (unary), two (binary), or many (nary) arguments of type int16.
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.
Int32 implements operations on one (unary), two (binary), or many (nary) arguments of type int32.
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.
Int64 implements operations on one (unary), two (binary), or many (nary) arguments of type int64.
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.
Int8 implements operations on one (unary), two (binary), or many (nary) arguments of type int8.
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.
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.
Uint implements operations on one (unary), two (binary), or many (nary) arguments of type uint.
Uint16 implements operations on one (unary), two (binary), or many (nary) arguments of type uint16.
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.
Uint32 implements operations on one (unary), two (binary), or many (nary) arguments of type uint32.
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.
Uint64 implements operations on one (unary), two (binary), or many (nary) arguments of type uint64.
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.
Uint8 implements operations on one (unary), two (binary), or many (nary) arguments of type uint8.
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.
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.