function

package
v2.3.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package function implements some functions for control the function execution and some is for functional programming.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func AcceptIf added in v2.3.0

func AcceptIf[T any](predicate func(T) bool, apply func(T) T) func(T) (T, bool)

AcceptIf returns another function of the same signature as the apply function but also includes a bool value to indicate success or failure. A predicate function that takes an argument of type T and returns a bool. An apply function that also takes an argument of type T and returns a modified value of the same type. Play: https://go.dev/play/p/XlXHHtzCf7d

Example
adder := AcceptIf(
	And(
		func(x int) bool {
			return x > 10
		}, func(x int) bool {
			return x%2 == 0
		}),
	func(x int) int {
		return x + 1
	},
)

result, ok := adder(20)
fmt.Println(result)
fmt.Println(ok)

result, ok = adder(21)
fmt.Println(result)
fmt.Println(ok)
Output:

21
true
0
false

func After

func After(n int, fn any) func(args ...any) []reflect.Value

After creates a function that invokes func once it's called n or more times. Play: https://go.dev/play/p/8mQhkFmsgqs

Example
fn := After(2, func() {
	fmt.Println("test")
})

fn()
fn()
Output:

test

func And added in v2.3.0

func And[T any](predicates ...func(T) bool) func(T) bool

And returns a composed predicate that represents the logical AND of a list of predicates. It evaluates to true only if all predicates evaluate to true for the given value. Play: https://go.dev/play/p/dTBHJMQ0zD2

Example
isNumericAndLength5 := And(
	func(s string) bool { return strings.ContainsAny(s, "0123456789") },
	func(s string) bool { return len(s) == 5 },
)

fmt.Println(isNumericAndLength5("12345"))
fmt.Println(isNumericAndLength5("1234"))
fmt.Println(isNumericAndLength5("abcde"))
Output:

true
false
false

func Before

func Before(n int, fn any) func(args ...any) []reflect.Value

Before creates a function that invokes func once it's called less than n times. Play: https://go.dev/play/p/0HqUDIFZ3IL

Example
fn := Before(2, func() {
	fmt.Println("test")
})

fn()
fn()
fn()
fn()
Output:

test
test

func Compose

func Compose[T any](fnList ...func(...T) T) func(...T) T

Compose compose the functions from right to left. Play: https://go.dev/play/p/KKfugD4PKYF

Example
toUpper := func(strs ...string) string {
	return strings.ToUpper(strs[0])
}
toLower := func(strs ...string) string {
	return strings.ToLower(strs[0])
}
transform := Compose(toUpper, toLower)

result := transform("aBCde")

fmt.Println(result)
Output:

ABCDE

func Debounced

func Debounced(fn func(), duration time.Duration) func()

Debounced creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked. Play: https://go.dev/play/p/absuEGB_GN7

Example
count := 0
add := func() {
	count++
}

debouncedAdd := Debounced(add, 50*time.Microsecond)

debouncedAdd()
debouncedAdd()
debouncedAdd()
debouncedAdd()

time.Sleep(100 * time.Millisecond)

fmt.Println(count)

debouncedAdd()

time.Sleep(100 * time.Millisecond)

fmt.Println(count)
Output:

1
2

func Delay

func Delay(delay time.Duration, fn any, args ...any)

Delay make the function execution after delayed time. Play: https://go.dev/play/p/Ivtc2ZE-Tye

Example
var print = func(s string) {
	fmt.Println(s)
}

Delay(2*time.Second, print, "hello")
Output:

hello

func Nand added in v2.3.0

func Nand[T any](predicates ...func(T) bool) func(T) bool

Nand returns a composed predicate that represents the logical NAND of a list of predicates. It evaluates to true only if all predicates evaluate to false for the given value. Play: https://go.dev/play/p/Rb-FdNGpgSO

Example
isNumericAndLength5 := Nand(
	func(s string) bool { return strings.ContainsAny(s, "0123456789") },
	func(s string) bool { return len(s) == 5 },
)

fmt.Println(isNumericAndLength5("12345"))
fmt.Println(isNumericAndLength5("1234"))
fmt.Println(isNumericAndLength5("abcdef"))
Output:

false
false
true

func Negate added in v2.3.0

func Negate[T any](predicate func(T) bool) func(T) bool

Negate returns a predicate that represents the logical negation of this predicate. Play: https://go.dev/play/p/jbI8BtgFnVE

Example
// Define some simple predicates for demonstration
isUpperCase := func(s string) bool {
	return strings.ToUpper(s) == s
}
isLowerCase := func(s string) bool {
	return strings.ToLower(s) == s
}
isMixedCase := Negate(Or(isUpperCase, isLowerCase))

fmt.Println(isMixedCase("ABC"))
fmt.Println(isMixedCase("AbC"))
Output:

false
true

func Nor added in v2.3.0

func Nor[T any](predicates ...func(T) bool) func(T) bool

Nor returns a composed predicate that represents the logical NOR of a list of predicates. It evaluates to true only if all predicates evaluate to false for the given value. Play: https://go.dev/play/p/2KdCoBEOq84

Example
match := Nor(
	func(s string) bool { return strings.ContainsAny(s, "0123456789") },
	func(s string) bool { return len(s) == 5 },
)

fmt.Println(match("dbcdckkeee"))

match = Nor(
	func(s string) bool { return strings.ContainsAny(s, "0123456789") },
	func(s string) bool { return len(s) == 5 },
)

fmt.Println(match("0123456789"))
Output:

true
false

func Or added in v2.3.0

func Or[T any](predicates ...func(T) bool) func(T) bool

Or returns a composed predicate that represents the logical OR of a list of predicates. It evaluates to true if at least one of the predicates evaluates to true for the given value. Play: https://go.dev/play/p/LitCIsDFNDA

Example
containsDigitOrSpecialChar := Or(
	func(s string) bool { return strings.ContainsAny(s, "0123456789") },
	func(s string) bool { return strings.ContainsAny(s, "!@#$%") },
)

fmt.Println(containsDigitOrSpecialChar("hello!"))
fmt.Println(containsDigitOrSpecialChar("hello"))
Output:

true
false

func Pipeline added in v2.1.8

func Pipeline[T any](funcs ...func(T) T) func(T) T

Pipeline takes a list of functions and returns a function whose param will be passed into the functions one by one. Play: https://go.dev/play/p/mPdUVvj6HD6

Example
addOne := func(x int) int {
	return x + 1
}
double := func(x int) int {
	return 2 * x
}
square := func(x int) int {
	return x * x
}

fn := Pipeline(addOne, double, square)

result := fn(2)

fmt.Println(result)
Output:

36

func Schedule

func Schedule(d time.Duration, fn any, args ...any) chan bool

Schedule invoke function every duration time, util close the returned bool channel. Play: https://go.dev/play/p/hbON-Xeyn5N

Example
count := 0

increase := func() {
	count++
}

stop := Schedule(2*time.Second, increase)

time.Sleep(2 * time.Second)
close(stop)

fmt.Println(count)
Output:

2

func Xnor added in v2.3.0

func Xnor[T any](predicates ...func(T) bool) func(T) bool

Xnor returns a composed predicate that represents the logical XNOR of a list of predicates. It evaluates to true only if all predicates evaluate to true or false for the given value. Play: https://go.dev/play/p/FJxko8SFbqc

Example
isEven := func(i int) bool { return i%2 == 0 }
isPositive := func(i int) bool { return i > 0 }

match := Xnor(isEven, isPositive)

fmt.Println(match(2))
fmt.Println(match(-3))
fmt.Println(match(3))
Output:

true
true
false

Types

type CurryFn added in v2.1.13

type CurryFn[T any] func(...T) T

CurryFn is for make curry function

func (CurryFn[T]) New added in v2.1.13

func (cf CurryFn[T]) New(val T) func(...T) T

New make a curry function for specific value. Play: https://go.dev/play/p/5HopfDwANKX

Example
add := func(a, b int) int {
	return a + b
}

var addCurry CurryFn[int] = func(values ...int) int {
	return add(values[0], values[1])
}
add1 := addCurry.New(1)

result := add1(2)

fmt.Println(result)
Output:

3

type Watcher

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

Watcher is used for record code excution time Play: https://go.dev/play/p/l2yrOpCLd1I

Example
w := NewWatcher()

w.Start()

longRunningTask()

w.Stop()

// eapsedTime := w.GetElapsedTime().Milliseconds()

fmt.Println("foo")

w.Reset()
Output:

foo

func NewWatcher added in v2.1.13

func NewWatcher() *Watcher

Start the watch timer.

func (*Watcher) GetElapsedTime

func (w *Watcher) GetElapsedTime() time.Duration

GetElapsedTime get excute elapsed time.

func (*Watcher) Reset

func (w *Watcher) Reset()

Reset the watch timer.

func (*Watcher) Start

func (w *Watcher) Start()

Start the watch timer.

func (*Watcher) Stop

func (w *Watcher) Stop()

Stop the watch timer.

Jump to

Keyboard shortcuts

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