slicekit

package
v0.185.0 Latest Latest
Warning

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

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Map

func Map[O, I any, FN mapperFunc[O, I]](s []I, fn FN) ([]O, error)

Map will do a mapping from an input type into an output type.

Example
package main

import (
	tsk "go.llib.dev/frameless/pkg/slicekit"
	"strconv"
	"strings"
)

func main() {
	var x = []string{"a", "b", "c"}
	_ = tsk.Must(tsk.Map[string](x, strings.ToUpper)) // []string{"A", "B", "C"}

	var ns = []string{"1", "2", "3"}
	_, err := tsk.Map[int](ns, strconv.Atoi) // []int{1, 2, 3}
	if err != nil {
		panic(err)
	}
}

func Must

func Must[T any](v T, err error) T
Example
package main

import (
	"fmt"
	tsk "go.llib.dev/frameless/pkg/slicekit"
)

func main() {
	var x = []int{1, 2, 3}
	x = tsk.Must(tsk.Map[int](x, func(v int) int {
		return v * 2
	}))

	v := tsk.Must(tsk.Reduce[int](x, 42, func(output int, current int) int {
		return output + current
	}))

	fmt.Println("result:", v)
}

func Reduce

func Reduce[O, I any, FN reducerFunc[O, I]](s []I, initial O, fn FN) (O, error)

Reduce iterates over a slice, combining elements using the reducer function.

Example
package main

import (
	"fmt"
	tsk "go.llib.dev/frameless/pkg/slicekit"
)

func main() {
	var x = []string{"a", "b", "c"}
	got, err := tsk.Reduce[string](x, "|", func(o string, i string) string {
		return o + i
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(got) // "|abc"
}

Types

This section is empty.

Jump to

Keyboard shortcuts

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