Skip to content

Function

function 函数包控制函数执行流程,包含部分函数式编程。

源码:

用法:

go
import (
    "github.com/duke-git/lancet/v2/function"
)

目录

文档

After

创建一个函数,当他被调用n或更多次之后将马上触发fn

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    fn := function.After(2, func() {
        fmt.Println("hello")
    })

    fn()
    fn()

    // Output:
    // hello
}

Before

创建一个函数,调用次数不超过n次,之后再调用这个函数,将返回一次最后调用fn的结果

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    fn := function.Before(2, func() {
        fmt.Println("hello")
    })

    fn()
    fn()
    fn()
    fn()

    // Output:
    // hello
    // hello
}

CurryFn

创建柯里化函数

函数签名:

go
type CurryFn[T any] func(...T) T
func (cf CurryFn[T]) New(val T) func(...T) T

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

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

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

    result := add1(2)

    fmt.Println(result)

    // Output:
    // 3
}

Compose

从右至左组合函数列表fnList,返回组合后的函数

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

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

    result := transform("aBCde")

    fmt.Println(result)

    // Output:
    // ABCDE
}

Debounce

创建一个函数的去抖动版本。该去抖动函数仅在上次调用后的指定延迟时间过去之后才会调用原始函数。支持取消去抖动。

函数签名:

go
func Debounce(fn func(), delay time.Duration) (debouncedFn func(), cancelFn func())

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    callCount := 0
    fn := func() {
        callCount++
    }

    debouncedFn, _ := function.Debounce(fn, 500*time.Millisecond)

    for i := 0; i < 10; i++ {
        debouncedFn()
        time.Sleep(50 * time.Millisecond)
    }

    time.Sleep(1 * time.Second)
    fmt.Println(callCount)

    debouncedFn()

    time.Sleep(1 * time.Second)
    fmt.Println(callCount)

    // Output:
    // 1
    // 2
}

Debounced

创建一个函数的去抖动版本。

⚠️ 本函数已弃用. 使用 Debounce 代替.

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    count := 0

    add := func() {
        count++
    }

    debouncedAdd := function.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
}

Delay

延迟delay时间后调用函数

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

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

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

    // Output:
    // hello
}

Schedule

每次持续时间调用函数,直到关闭返回的 bool chan

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    count := 0

    increase := func() {
        count++
    }

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

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

    fmt.Println(count)

    // Output:
    // 2
}

Pipeline

执行函数pipeline.

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    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 := function.Pipeline(addOne, double, square)

    result := fn(2)

    fmt.Println(result)

    // Output:
    // 36
}

Watcher

Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。

函数签名:

go
type Watcher struct {
    startTime int64
    stopTime  int64
    excuting  bool
}
func NewWatcher() *Watcher
func (w *Watcher) Start()
func (w *Watcher) Stop()
func (w *Watcher) Reset()
func (w *Watcher) GetElapsedTime() time.Duration

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    w := function.NewWatcher()

    w.Start()

    longRunningTask()

    fmt.Println(w.excuting) //true

    w.Stop()

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

    fmt.Println(eapsedTime)

    w.Reset()

}

func longRunningTask() {
    var slice []int64
    for i := 0; i < 10000000; i++ {
        slice = append(slice, int64(i))
    }
}

And

返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑and操作。只有当所有谓词判断函数对于给定的值都返回true时,返回true, 否则返回false。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    isNumericAndLength5 := function.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
}

Or

返回一个复合谓词判断函数,该判断函数表示一组谓词的逻辑or操作。只有当所有谓词判断函数对于给定的值都返回false时,返回false, 否则返回true。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    containsDigitOrSpecialChar := function.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
}

Negate

返回一个谓词函数,该谓词函数表示当前谓词的逻辑否定。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    // 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 := function.Negate(function.Or(isUpperCase, isLowerCase))

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

    // Output:
    // false
    // true
}

Nor

返回一个组合谓词函数,表示给定值上所有谓词逻辑非或 (nor) 的结果。只有当所有谓词函数对给定值都返回false时,该组合谓词函数才返回true。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

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

    fmt.Println(match("dbcdckkeee"))


    match = function.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
}

Nand

返回一个复合谓词函数,表示给定谓词函数列表的逻辑非与 (NAND)。仅当列表中所有函数对给定参数返回false时,才返回true,否则返回false。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    isNumericAndLength5 := function.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
}

Xnor

返回一个复合谓词函数,表示给定一组谓词函数的逻辑异或 (XNOR)。只有当所有 谓词函数对给参数都返回true或false时,该谓词函数才返回true。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

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

    match := function.Xnor(isEven, isPositive)

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

    // Output:
    // true
    // true
    // false
}

AcceptIf

AcceptIf函数会返回另一个函数,该函数的签名与 apply 函数相同,但同时还会包含一个布尔值来表示成功或失败。

函数签名:

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

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    adder := function.AcceptIf(
        function.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
}

Throttle

创建一个函数的节流版本。返回的函数保证在每个时间间隔内最多只会被调用一次。

函数签名:

go
func Throttle(fn func(), interval time.Duration) func()

示例:运行

go
package main

import (
    "fmt"
    "github.com/duke-git/lancet/v2/function"
)

func main() {
    callCount := 0

    fn := func() {
        callCount++
    }

    throttledFn := function.Throttle(fn, 1*time.Second)

    for i := 0; i < 5; i++ {
        throttledFn()
    }

    time.Sleep(1 * time.Second)

    fmt.Println(callCount)

    // Output:
    // 1
}