ffn

package
v0.0.0-...-1535236 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 0 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](collection []T, f func(T) bool) bool

All returns true if all elements in collection return true when passed to f, otherwise it returns false.

Example
collection := []int{1, 2, 3}

res := All(collection, func(i int) bool {
	return i > 0
})

fmt.Println(res)
Output:

true

func KeyBy

func KeyBy[T any, K comparable](collection []T, keygen func(T) K) map[K]T

KeyBy calls keygen on each element of collection, setting the result of f as the key in the returned map, and the element as the value.

Example
collection := []string{"first", "second", "last"}

res := KeyBy(collection, func(s string) int {
	return len(s)
})

fmt.Println(res)
Output:

map[4:last 5:first 6:second]

func Map

func Map[T any, R any](collection []T, mapper func(T) R) []R

Map calls mapper on each element on collection, returning a new collection with the results of each call to mapper.

Example
collection := []int{1, 2, 3}

res := Map(collection, func(i int) int {
	return i * 2
})

fmt.Println(res)
Output:

[2 4 6]

func Reduce

func Reduce[T, R any](collection []T, reducer func(T, R) R, initial R) R

Reduce accepts a slice of values, a function that accepts an element of the slice and an accumulater, and an initial value for the accumulator. The result of each call to reducer is passed as the value to the next iteration.

Example
collection := []int{1, 2, 3}

res := Reduce(collection, func(i, acc int) int {
	return acc + i
}, 0)

fmt.Println(res)
Output:

6

func Reject

func Reject[T any](collection []T, rejecter func(T) bool) []T

Reject calls rejecter on each element of collection, returning a new slice for all elements where f returns false.

Example
collection := []int{1, 2, 3, 4}

res := Reject(collection, func(i int) bool {
	return i%2 == 0
})

fmt.Println(res)
Output:

[1 3]

func Select

func Select[T any](collection []T, selecter func(T) bool) []T

Select calls selecter on each element of collection, returning a new slice for all elements where f returns true.

Example
collection := []int{1, 2, 3, 4}

res := Select(collection, func(i int) bool {
	return i%2 == 0
})

fmt.Println(res)
Output:

[2 4]

Types

This section is empty.

Jump to

Keyboard shortcuts

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