slice

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2022 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

slice is a package of Slice.

Example (AllAny)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 2, 3, 4, 5)
	b := s.All(func(i int) bool {
		return i <= 5
	})
	fmt.Println(b)
	b = s.Any(func(i int) bool {
		return i == 3
	})
	fmt.Println(b)
}
Output:

true
true
Example (BinarySearch)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
	found := s.BinarySearch(3, func(i1, i2 int) int {
		if i1 < i2 {
			return -1
		} else if i1 > i2 {
			return 1
		}
		return 0
	})
	fmt.Println(found)
	notFound := s.BinarySearch(8, func(i1, i2 int) int {
		if i1 < i2 {
			return -1
		} else if i1 > i2 {
			return 1
		}
		return 0
	})
	fmt.Println(notFound)
}
Output:

1
-1
Example (Chunk)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 3, 5, 7, 9, 11, 13, 15, 17, 19)
	result := s.Chunk(2)
	slice.Of(result...).Foreach(func(s slice.Slice[int]) {
		fmt.Println(s.JoinToString(", "))
	})
}
Output:

1, 3
5, 7
9, 11
13, 15
17, 19
Example (Count)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
	evens := s.Count(func(i int) bool {
		return i%2 == 0
	})
	fmt.Println(evens)
}
Output:

5
Example (Filter)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
	r := s.Filter(func(i int) bool {
		return i > 5
	}).JoinToString(", ")
	fmt.Println(r)
}
Output:

6, 7, 8, 9, 10, 11
Example (FirstOf)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
	r := s.FirstOf(func(i int) bool {
		return i > 5
	})
	fmt.Println(r)
}
Output:

6
Example (Map)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)
	r := s.Map(func(index, value int) int {
		return value * 2
	})
	fmt.Println(r.JoinToString(", "))
}
Output:

2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22
Example (Max)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6)
	r := s.Max(func(i1, i2 int) int {
		if i1 > i2 {
			return -1
		} else if i1 < i2 {
			return 1
		}
		return 0
	})
	fmt.Println(r)
}
Output:

11
Example (Min)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6)
	r := s.Min(func(i1, i2 int) int {
		if i1 > i2 {
			return -1
		} else if i1 < i2 {
			return 1
		}
		return 0
	})
	fmt.Println(r)
}
Output:

1
Example (Random)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6)
	r := s.Random()
	fmt.Println(r)
}
Output:

random value of 1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6
Example (Reduce)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6)
	sum := s.Reduce(func(i1, i2 int) int {
		return i1 + i2
	}, 0)
	fmt.Println(sum)
}
Output:

66
Example (Sort)
package main

import (
	"fmt"

	"github.com/snowmerak/generics-for-go/v2/collections/slice"
)

func main() {
	s := slice.Of(1, 11, 2, 10, 3, 9, 4, 8, 5, 7, 6)
	r := s.Sort(func(i1, i2 int) int {
		if i1 < i2 {
			return -1
		} else if i1 > i2 {
			return 1
		}
		return 0
	})
	fmt.Println(r.JoinToString(", "))
}
Output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GroupBy

func GroupBy[T any, K comparable](slice Slice[T], f func(T) K) map[K]Slice[T]

GroupBy returns a map of the elements in the slice grouped by the result of the function.

func Map

func Map[T, R any](slice Slice[T], f func(T) R) []R

Map returns a Slice with the result of mapping the elements.

Types

type Slice

type Slice[T any] []T

Slice is a []T.

func New

func New[T any]() Slice[T]

New creates a new Slice.

func Of

func Of[T any](values ...T) Slice[T]

Of returns a Slice with the given elements.

func Zip

func Zip[T any](slices ...[]T) []Slice[T]

Zip returns a Slice of elements with same index.

func (Slice[T]) All

func (slice Slice[T]) All(f func(T) bool) bool

All returns true if all elements in the slice satisfy the predicate.

func (Slice[T]) Any

func (slice Slice[T]) Any(f func(T) bool) bool

Any returns true if any element in the slice satisfies the predicate.

func (Slice[T]) BinarySearch

func (slice Slice[T]) BinarySearch(value T, f func(T, T) int) int

BinarySearch returns the index of the element in the slice, or -1 if not found.

func (Slice[T]) Chunk

func (slice Slice[T]) Chunk(size int) []Slice[T]

Chunk returns a slice of slices in length of size.

func (Slice[T]) Clone

func (slice Slice[T]) Clone() Slice[T]

Clone returns a copy of the slice.

func (Slice[T]) Count

func (slice Slice[T]) Count(f func(T) bool) int

Count returns the number of elements satisfy the predicate in the slice.

func (Slice[T]) Filter

func (slice Slice[T]) Filter(f func(T) bool) Slice[T]

Filter returns a new slice containing all elements satisfy the predicate in the slice.

func (Slice[T]) FilterIndex

func (slice Slice[T]) FilterIndex(f func(int) bool) Slice[T]

FilterIndex returns a new slice containing all elements satisfy the predicate with index in the slice.

func (Slice[T]) FirstIndexOf

func (slice Slice[T]) FirstIndexOf(f func(T) bool) int

FirstIndexOf returns the index of the first element in the slice that satisfies the predicate.

func (Slice[T]) FirstOf

func (slice Slice[T]) FirstOf(f func(T) bool) (r T)

FirstOf returns the first element in the slice that satisfies the predicate.

func (Slice[T]) Foreach

func (slice Slice[T]) Foreach(f func(T))

Foreach calls the function f for each element in the slice.

func (Slice[T]) JoinToString

func (slice Slice[T]) JoinToString(sep string) string

JoinToString returns a string with all elements joined by sep.

func (Slice[T]) LastIndexOf

func (slice Slice[T]) LastIndexOf(f func(T) bool) int

LastIndexOf returns the index of the last element in the slice that satisfies the predicate.

func (Slice[T]) LastOf

func (slice Slice[T]) LastOf(f func(T) bool) (r T)

LastOf returns the last element in the slice that satisfies the predicate.

func (Slice[T]) Map

func (slice Slice[T]) Map(f func(int, T) T) Slice[T]

Map applies a function to each element in the slice.

func (Slice[T]) Max

func (slice Slice[T]) Max(f func(T, T) int) (r T)

Max returns the maximum element in the slice.

func (Slice[T]) Min

func (slice Slice[T]) Min(f func(T, T) int) (r T)

Min returns the minimum element in the slice.

func (Slice[T]) Random

func (slice Slice[T]) Random() T

Random returns a random element in the slice.

func (Slice[T]) Reduce

func (slice Slice[T]) Reduce(f func(T, T) T, initial T) T

Reduce applies a function against an initial and each element in the slice.

func (Slice[T]) Reverse

func (slice Slice[T]) Reverse() Slice[T]

Reverse returns a new slice with elements in reverse order.

func (Slice[T]) Shuffle

func (slice Slice[T]) Shuffle() Slice[T]

Shuffle returns itself with elements in random order.

func (Slice[T]) Sort

func (slice Slice[T]) Sort(f func(T, T) int) Slice[T]

Sort returns a new slice with elements in sorted order.

Jump to

Keyboard shortcuts

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