slice

package
v0.2.8 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2022 License: MIT Imports: 1 Imported by: 0

README

Package slice

do something for a slice.

Usage

example

package main

import (
	"log"
	"strconv"
	"strings"

	"github.com/QisFj/godry/slice"
)

func main() {
	is := []int{1, 2, 3, 4, 5, 6}
	ss := slice.Map(is, func(i int, v int) string {
		return strconv.Itoa(v)
	})
	log.Printf("after map: %#v\n", ss)
	ss = slice.Filter(ss, func(_ int, v string) bool {
		return strings.ContainsAny(v, "135")
	})
	log.Printf("after filter: %#v\n", ss)
}

Map

Map a slice to another slice.

Filter

Filter some elements of slice.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone added in v0.2.0

func Clone[T any](src []T) []T

func Filter

func Filter[I any](slice []I, f func(index int, value I) (keep bool)) []I

func FilterOn added in v0.2.0

func FilterOn[I any](slice *[]I, f func(index int, value I) (keep bool))

FilterOn is in-place filter of a slice.

func Foreach

func Foreach(slice interface{}, f func(i int, v interface{}))

func Map added in v0.2.0

func Map[I, O any](input []I, f func(index int, value I) O) []O

func Reduce

func Reduce[I, V any](slice []I, initReduceValue V, f func(reduceValue V, index int, value I) V) (reduceValue V)

func ToMap

func ToMap[I any, K comparable, V any](slice []I, kf func(I) K, vf func(I) V) map[K]V

ToMap convert a slice to a map if vf is nil, will try to use elem of slice as value

func ToMapItself added in v0.2.0

func ToMapItself[I any, K comparable](slice []I, kf func(I) K) map[K]I

ToMapItself convert a slice to a map like ToMap, but the value of map is the elem of slice itself

Types

type BidirectionalManyToManyMap added in v0.2.5

type BidirectionalManyToManyMap[K, V comparable] struct {
	// contains filtered or unexported fields
}

func (*BidirectionalManyToManyMap[K, V]) DeleteKey added in v0.2.5

func (m *BidirectionalManyToManyMap[K, V]) DeleteKey(k K)

func (*BidirectionalManyToManyMap[K, V]) DeleteValue added in v0.2.5

func (m *BidirectionalManyToManyMap[K, V]) DeleteValue(v V)

func (*BidirectionalManyToManyMap[K, V]) Insert added in v0.2.5

func (m *BidirectionalManyToManyMap[K, V]) Insert(k K, v V)

func (*BidirectionalManyToManyMap[K, V]) Keys added in v0.2.5

func (m *BidirectionalManyToManyMap[K, V]) Keys() []K

func (*BidirectionalManyToManyMap[K, V]) KeysOf added in v0.2.5

func (m *BidirectionalManyToManyMap[K, V]) KeysOf(v V) map[K]struct{}

should not write to returned map

func (*BidirectionalManyToManyMap[K, V]) Values added in v0.2.5

func (m *BidirectionalManyToManyMap[K, V]) Values(k K) []V

func (*BidirectionalManyToManyMap[K, V]) ValuesOf added in v0.2.5

func (m *BidirectionalManyToManyMap[K, V]) ValuesOf(k K) map[V]struct{}

should not write to returned map

type KV

type KV[K comparable, V any] struct {
	Key   K
	Value V
}

func KVsOfMap

func KVsOfMap[K comparable, V any](m map[K]V) []KV[K, V]

KVsOfMap return a kv slice from map

Jump to

Keyboard shortcuts

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