sutil

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2022 License: MIT Imports: 1 Imported by: 0

README

Sutil

PkgGoDev Build Status

Sutil is slice util: a slice snippet collection for Go.

Requires go >= 1.18.

go get -u -v github.com/kakilangit/sutil

It's spatula in Indonesian/Javanese language.

Check the tests to see how to use generic slices with sutil.

Documentation

Overview

Package sutil

Sutil is slice util. It's spatula in Indonesian/Javanese language.

A personal collection of slice snippet.

Index

Constants

View Source
const (
	// ErrInvalidSlice is invalid string slice
	ErrInvalidSlice = Error("invalid slice")
	// ErrInvalidLimit is invalid limit
	ErrInvalidLimit = Error("invalid limit value")
)

Variables

This section is empty.

Functions

func Content

func Content[T any](ss []T, page, limit int) []T

Content returns the content of the []T based on page and limit.

func Equal

func Equal[T comparable](ss, compared []T) bool

Equal check slice equality.

func Filter

func Filter[T any](ss []T, fn func(int, T) bool) []T

Filter filters values from a slice using a filter function.

func Map

func Map[T, U any](ss []T, fn func(int, T) U) []U

Map turns a []T to a []U using a mapping function.

func Reduce

func Reduce[T, U any](ss []T, initializer U, f func(U, T) U) U

Reduce reduces a []T to a single value using a reduction function.

func Split

func Split[T any](ss []T, limit int) ([][]T, error)

Split splits []T into []T with maximum member of[]T is limit.

Can be used for pagination or break the parameters used in SQL IN statements. Manipulating slice is faster than append via iteration.

Example:

input := []string{"7892141641500", "7892141600279", "7892141600422", "7892141640145", "7892141650236", "7892141650274", "7892141650311"}
limit := 2

pages, err := sutil.Split(input, limit)
if err != nil {
	fmt.Println(err)
}
fmt.Println(pages)

Will return:

[[7892141641500 7892141600279] [7892141600422 7892141640145] [7892141650236 7892141650274] [7892141650311]]

func TotalPage

func TotalPage[T any](ss []T, limit int) int

TotalPage returns the total page for any given slice based on the limit provided.

func Unique

func Unique[T comparable](ss []T) []T

Unique make []T unique.

Types

type Error

type Error string

Error type will conform errorer interface and use it as error constant.

func (Error) Error

func (e Error) Error() string

Error method to conform errorer interface.

Jump to

Keyboard shortcuts

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