counters

package
v2.0.0-...-4b7107c Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2023 License: MIT Imports: 1 Imported by: 0

README

Counter

Description

A set of counters which helps avoid reinvented wheels.

Usage

package main

import counters "github.com/sam-caldwell/go/v2/projects/counter"

var count counters.Simple
count.Increment() //output: 0
count.Increment() //output: 1
count.Increment() //output: 2
count.Value(0) //output: 2

var countIf counters.Conditional
countif.Increment(true) //output: 0
countif.Value()          //output: 1
countif.Increment(false) //output: 1
countif.Decrement(true) //output:1

var count = CreateFunc(1, 1)
fmt.Println(count()) // prints 2
fmt.Println(count()) // prints 3
fmt.Println(count()) // prints 4

count = CreateFunc(10, -1)
fmt.Println(count()) // prints 9
fmt.Println(count()) // prints 8
fmt.Println(count()) // prints 7

countOk(ok bool) - returns incremented count if input is true or -1 if false

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FloatCounter32

func FloatCounter32(start, increment float32) func() float32

FloatCounter32 - count by increment from a starting value and return the new value along the way.

func FloatCounter64

func FloatCounter64(start, increment float64) func() float64

FloatCounter64 - count by increment from a starting value and return the new value along the way.

func IntegerCounter

func IntegerCounter(start, increment int) func() int

IntegerCounter - count by increment from a starting value and return the new value along the way.

Types

type Conditional

type Conditional struct {
	// contains filtered or unexported fields
}

Conditional - tracks the conditional count.

func (*Conditional) Decrement

func (counter *Conditional) Decrement() (int, error)

Decrement - Decrement unconditionally to floor (0)

func (*Conditional) DecrementIf

func (counter *Conditional) DecrementIf(ok bool) (v int, err error)

DecrementIf - decrement to floor (0) if true otherwise return -1

func (*Conditional) Increment

func (counter *Conditional) Increment() (v int, err error)

Increment - Increment value

func (*Conditional) IncrementIf

func (counter *Conditional) IncrementIf(ok bool) (v int, err error)

IncrementIf - increment if ok is true otherwise return -1

func (*Conditional) Value

func (counter *Conditional) Value() int

Value - return the current value of the counter

type Simple

type Simple struct {
	// contains filtered or unexported fields
}

Simple - Track the counter state

func (*Simple) Decrement

func (counter *Simple) Decrement() int

Decrement - return the current value then decrement

func (*Simple) Increment

func (counter *Simple) Increment() int

Increment - return the current value then increment

func (*Simple) Value

func (counter *Simple) Value() int

Value - return the current value of the counter

Jump to

Keyboard shortcuts

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