function

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2023 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 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 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: Todo

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 Pipeline

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(1*time.Second, increase)

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

fmt.Println(count)
Output:

3

Types

type CurryFn

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

CurryFn is for make curry function

func (CurryFn[T]) New

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

New make a curry function for specific value. Play: Todo

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: Todo

Example
w := NewWatcher()

w.Start()

longRunningTask()

w.Stop()

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

fmt.Println("foo")

w.Reset()
Output:

foo

func NewWatcher

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