functools

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package functools provides functional programming tools.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(iter any, condition func(any) bool) (result bool, err error)

All returns true if all elements in the iterable satisfy the condition, and false otherwise.

Example
// string
r, _ := All("hello", func(a any) bool {
	return a.(string) > "a"
})
fmt.Println(r)
r, _ = All("hello", func(a any) bool {
	return a.(string) > "g"
})
fmt.Println(r)
// slice
r, _ = All([]int{1, 2, 3}, func(a any) bool {
	return a.(int) > 0
})
fmt.Println(r)
r, _ = All([]int{1, 2, 3}, func(a any) bool {
	return a.(int) > 2
})
fmt.Println(r)
Output:

true
false
true
false

func Any

func Any(iter any, condition func(any) bool) (result bool, err error)

Any returns true if any element in the iterable satisfies the condition, and false otherwise.

Example
// string
r, _ := Any("hello", func(a any) bool {
	return a.(string) > "n"
})
fmt.Println(r)
r, _ = Any("hello", func(a any) bool {
	return a.(string) > "z"
})
fmt.Println(r)
// slice
r, _ = All([]int{1, 2, 3}, func(a any) bool {
	return a.(int) > 0
})
fmt.Println(r)
r, _ = All([]int{1, 2, 3}, func(a any) bool {
	return a.(int) > 3
})
fmt.Println(r)
Output:

true
false
true
false

func CountBy

func CountBy(entry any, condition func(any) bool) (count int)

CountBy counts the number of elements in the given entry that satisfy the given condition, and it will return -1 if the entry is not array, slice or string.

Example
// string
fmt.Println(CountBy("hello", func(a any) bool {
	return a.(string) != "l"
}))
// slice
fmt.Println(CountBy([]int{1, 2, 3, 4, 5}, func(a any) bool {
	return a.(int)%2 == 0
}))
Output:

3
2

func Filter

func Filter(filter, entry any) (output any, err error)

Filter applies a filter function to an entry and the returned output only contains the entries that pass the filter function.

Example
// string
f, _ := Filter(func(a string) bool {
	return a != "l"
}, "hello")
fmt.Println(f)
// slice
f, _ = Filter(func(a int) bool {
	return a%2 == 0
}, []int{1, 2, 3, 4, 5})
fmt.Println(f)
Output:

[h e o]
[2 4]

func Map

func Map(handler, entry any) (output any, err error)

Map applies a conversion function to each element of an input sequence and returns the output list containing the converted elements.

Example
// string
m, _ := Map(func(a string) any {
	return []byte(strings.ToUpper(a))[0]
}, "hello")
fmt.Println(m)
// slice
m, _ = Map(func(a int) string {
	return fmt.Sprintf("user%d", a*2)
}, []int{1, 2, 3, 4, 5})
fmt.Println(m)
Output:

[72 69 76 76 79]
[user2 user4 user6 user8 user10]

func Maps

func Maps(handler any, entries ...any) (output []any, err error)

Maps applies a conversion function to each element of multiple input lists, each element in the returned output list is converted from elements with the same index in all input lists by the handler function.

Example
type User struct {
	ID    int
	Name  string
	Roles []int
}
m, _ := Maps(func(id int, name string, roles []int) User {
	return User{
		ID:    id,
		Name:  name,
		Roles: roles,
	}
}, []int{1, 2, 3}, []string{"a", "b", "c"}, [][]int{{1}, {0}, {0, 1}})
fmt.Println(m)
Output:

[{1 a [1]} {2 b [0]} {3 c [0 1]}]

func Reduce

func Reduce(handler, entry any) (output any, err error)

Reduce applies a function to an input sequence and returns the accumulated result.

Example
arr := []int{1, 2, 3, 4, 5}
// sum
r, _ := Reduce(func(a, b int) int {
	return a + b
}, arr)
fmt.Println(r)
// product
r, _ = Reduce(func(a, b int) int {
	return a * b
}, arr)
fmt.Println(r)
Output:

15
120

Types

This section is empty.

Jump to

Keyboard shortcuts

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