sugar

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2023 License: MIT Imports: 1 Imported by: 0

README

What go-sugaring is?

This is the package with functions from Python/other language, that you are searching for in Go (Golang)

Disclaimer

Most of the functions from this lib became possible to be written because of generics. So, it is make sense to use the library only if your project is ready to use go >=1.18

Installation and usage

  1. go get github.com/waclawthedev/go-sugaring
  2. Call the function. For example:
print(sugar.Contains([]string{"a", "b", "c"}, "a"))
//output: true

Already implemented functions:

  1. sugar.Contains - check if slice contains element
  2. sugar.IndexesOf - get the slice with occurrences (indexes) of element in slice
  3. sugar.Map - Map function. Python analog
  4. sugar.Filter - Filter function. Python analog
  5. sugar.Reduce - Reduce function. Python analog
  6. sugar.Flatten - Transforms matrix to slice
  7. sugar.Min - min element in slice
  8. sugar.Max - max element in slice
  9. sugar.Keys - get keys of map as slice
  10. sugar.Values - get values of map as slice

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[K constraints.Ordered](s []K, e K) bool

Contains returns true if array of K contains e

func Filter

func Filter[K any](s []K, filterFunc func(K) bool) (result []K)

Filter returns array with elements of s if filterFunc returned true for according element

func Flatten

func Flatten[K any](s [][]K) (result []K)

Flatten returns slice based on matrix values

func IndexesOf

func IndexesOf[K constraints.Ordered](s []K, e K) (indexes []int)

IndexesOf returns []int with indexes of occurrences e in array s

func Keys

func Keys[T constraints.Ordered, K any](m map[T]K) (result []T)

Keys returns array of T containing keys of map[T]K

func Map

func Map[K any](s []K, mapFunc func(K) K) (result []K)

Map returns array with every element of s passed through the mapFunc

func Max

func Max[K constraints.Ordered](s []K) (result K)

Max returns max element of array

func Min

func Min[K constraints.Ordered](s []K) (result K)

Min returns min element of array

func Reduce

func Reduce[K any](s []K, reduceFunc func(K, K) K) (result K)

Reduce returns one K var that is result of cumulative execution of reduceFunc on every element of s

func SplitByN added in v1.0.2

func SplitByN[T any](s []T, n int) [][]T

SplitByN splits a slice s into sub-slices of maximum size n. Returns nil if n is less than or equal to 0.

func Values

func Values[T constraints.Ordered, K any](m map[T]K) (result []K)

Values returns array of T containing values of map[T]K

Types

This section is empty.

Jump to

Keyboard shortcuts

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