collect

package
v0.0.0-...-500603a Latest Latest
Warning

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

Go to latest
Published: Oct 26, 2024 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Discard

func Discard[S ~[]E, E any](s S, f func(E) bool) []E

Discard removes elements in the slice s that satisfy the predicate f. If the input slice is nil, it returns nil.

Example
package main

import (
	"fmt"

	"github.com/kosadoge/kit/collect"
)

func main() {
	nums := []int{-5, -3, -1, 1, 3, 5}
	positive := collect.Discard(nums, func(n int) bool { return n < 0 })

	fmt.Printf("%#v\n", positive)
}
Output:

[]int{1, 3, 5}

func Keep

func Keep[S ~[]E, E any](s S, f func(E) bool) []E

Keep retains elements in the slice s that satisfy the predicate f. If the input slice is nil, it returns nil.

Example
package main

import (
	"fmt"

	"github.com/kosadoge/kit/collect"
)

func main() {
	nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	even := collect.Keep(nums, func(n int) bool { return n%2 == 0 })

	fmt.Printf("%#v\n", even)
}
Output:

[]int{2, 4, 6, 8, 10}

func Map

func Map[S ~[]E, E, T any](s S, f func(E) T) []T

Map applies a function f to each element of a slice s and returns a new slice of the results. If the input slice is nil, it returns nil.

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/kosadoge/kit/collect"
)

func main() {
	nums := []float64{1.1, 2.2, 3.3, 4.4, 5.5}
	strs := collect.Map(nums, func(n float64) string { return strconv.FormatFloat(n, 'f', -1, 64) })

	fmt.Printf("%#v\n", strs)
}
Output:

[]string{"1.1", "2.2", "3.3", "4.4", "5.5"}
Example (ExtractStructFields)
package main

import (
	"fmt"

	"github.com/kosadoge/kit/collect"
)

func main() {
	type Box struct {
		ID    int64
		Value string
	}

	boxes := []Box{
		{ID: 1, Value: "apple"},
		{ID: 2, Value: "banana"},
		{ID: 3, Value: "cat"},
		{ID: 4, Value: "dog"},
	}

	ids := collect.Map(boxes, func(b Box) int64 { return b.ID })
	values := collect.Map(boxes, func(b Box) string { return b.Value })

	fmt.Printf("%#v\n", ids)
	fmt.Printf("%#v\n", values)
}
Output:

[]int64{1, 2, 3, 4}
[]string{"apple", "banana", "cat", "dog"}
Example (ReuseFunc)
package main

import (
	"fmt"
	"strconv"

	"github.com/kosadoge/kit/collect"
)

func main() {
	nums := []int{1, 2, 3, 4, 5}
	strs := collect.Map(nums, strconv.Itoa)

	fmt.Printf("%#v\n", strs)
}
Output:

[]string{"1", "2", "3", "4", "5"}

func Unique

func Unique[S ~[]E, E comparable](s S) []E

Unique returns a new slice with duplicate elements removed. If the input slice is nil, it returns nil.

Example
package main

import (
	"fmt"

	"github.com/kosadoge/kit/collect"
)

func main() {
	fruits := []string{"🍎", "🍌", "🍉", "🍏", "🍎", "🍎", "🍇", "🍉", "🍌", "🍏"}
	types := collect.Unique(fruits)

	fmt.Printf("%#v\n", types)
}
Output:

[]string{"🍎", "🍌", "🍉", "🍏", "🍇"}

Types

This section is empty.

Jump to

Keyboard shortcuts

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