types

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 2 Imported by: 2

README

Types

Go Report Card GoDoc codecov

Go implementation for generic types, imitating Ruby types

Data structures

Slice

A slice of comparable type that can hold any value

Example
func ExampleSlice() {
	a := Slice[int]{1, 2, 3, 4, 5, 6}

	// multiply every element by 100
	a = a.Map(func(e int) int {
		return e * 100
	})

	// select any element <= 300
	a = a.KeepIf(func(e int) bool {
		return e <= 300
	})

	fmt.Print(a)

	// Output: [100 200 300]
}
Slice Methods available:
func (a Slice[T]) All(block func(T) bool) bool
func (a Slice[T]) Any(block func(T) bool) bool
func (a Slice[T]) At(index int) *T
func (a Slice[T]) CountBy(block func(T) bool) (count int)
func (a Slice[T]) CountElement(element T) (count int)
func (a Slice[T]) Cycle(count int, block func(T))
func (a Slice[T]) Delete(element T) Slice[T]
func (a Slice[T]) DeleteAt(index int) Slice[T]
func (a Slice[T]) DeleteIf(block func(T) bool) Slice[T]
func (a Slice[T]) Drop(count int) Slice[T]
func (a Slice[T]) Each(block func(T))
func (a Slice[T]) EachIndex(block func(int))
func (a Slice[T]) Fetch(index int, defaultValue T) T
func (a Slice[T]) Fill(element T, start int, length int) Slice[T]
func (a Slice[T]) FillWith(start int, length int, block func(int) T) Slice[T]
func (a Slice[T]) First() *T
func (a Slice[T]) Firsts(count int) Slice[T]
func (a Slice[T]) Include(element T) bool
func (a Slice[T]) Index(element T) int
func (a Slice[T]) IndexBy(block func(T) bool) int
func (a Slice[T]) Insert(index int, elements ...T) Slice[T]
func (a Slice[T]) IsEmpty() bool
func (a Slice[T]) IsEq(other Slice[T]) bool
func (a Slice[T]) KeepIf(block func(T) bool) Slice[T]
func (a Slice[T]) Last() *T
func (a Slice[T]) Lasts(count int) Slice[T]
func (a Slice[T]) Len() int
func (a Slice[T]) Map(block func(T) T) Slice[T]
func (a Slice[T]) Max(block func(T) int) T
func (a Slice[T]) Min(block func(T) int) T
func (a Slice[T]) Pop() (Slice[T], T)
func (a Slice[T]) Push(element T) Slice[T]
func (a Slice[T]) Reduce(block func(T) bool) Slice[T]
func (a Slice[T]) Reverse() Slice[T]
func (a Slice[T]) Select(block func(T) bool) Slice[T]
func (a Slice[T]) SelectUntil(block func(T) bool) Slice[T]
func (a Slice[T]) Shift() (T, Slice[T])
func (a Slice[T]) Shuffle() Slice[T]
func (a Slice[T]) Unshift(element T) Slice[T]

Documentation

Overview

Package types provides a generic data types similar to that of Ruby

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map added in v0.0.4

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

Map is a wrapper for sync.Map with same methods (except for CompareAnd*) Check: https://pkg.go.dev/sync#Map

func (*Map[K, V]) Delete added in v0.0.4

func (m *Map[K, V]) Delete(k K)

Delete see sync.Map#Delete

func (*Map[K, V]) Load added in v0.0.4

func (m *Map[K, V]) Load(k K) (v V, ok bool)

func (*Map[K, V]) LoadAndDelete added in v0.0.4

func (m *Map[K, V]) LoadAndDelete(k K) (v V, loaded bool)

LoadAndDelete see sync.Map#LoadAndDelete

func (*Map[K, V]) LoadOrStore added in v0.0.4

func (m *Map[K, V]) LoadOrStore(k K, v V) (actual V, loaded bool)

LoadOrStore see sync.Map#LoadOrStore

func (*Map[K, V]) Range added in v0.0.4

func (m *Map[K, V]) Range(f func(k K, v V) bool)

Range see sync.Map#Range

func (*Map[K, V]) Store added in v0.0.4

func (m *Map[K, V]) Store(k K, v V)

func (*Map[K, V]) Swap added in v0.0.4

func (m *Map[K, V]) Swap(k K, v V) (previous V, loaded bool)

Swap see sync.Map#Swap

type Slice

type Slice[T comparable] []T

Slice is an alias for a slice of variables that vary in types Slice can hold any comparable data type

Example
a := Slice[int]{1, 2, 3, 4, 5, 6}

// multiply every element by 100
a = a.Map(func(e int) int {
	return e * 100
})

// select any element <= 300
a = a.KeepIf(func(e int) bool {
	return e <= 300
})

fmt.Print(a)
Output:

[100 200 300]

func (Slice[T]) All

func (a Slice[T]) All(block func(T) bool) bool

All returns true if "block" returned true for all elements in Slice and false otherwise

func (Slice[T]) Any

func (a Slice[T]) Any(block func(T) bool) bool

Any returns true if "block" returned true for any of the Slice elements and false otherwise

func (Slice[T]) At

func (a Slice[T]) At(index int) *T

At returns element by index, a negative index counts from the end of the Slice if index is out of range it returns nil

func (Slice[T]) CountBy

func (a Slice[T]) CountBy(block func(T) bool) (count int)

CountBy returns number of elements which "block" returns true for

func (Slice[T]) CountElement

func (a Slice[T]) CountElement(element T) (count int)

CountElement returns number of elements equal to "element" in Slice

func (Slice[T]) Cycle

func (a Slice[T]) Cycle(count int, block func(T))

Cycle will cycle through Slice elements "count" times passing each element to "block" function

func (Slice[T]) Delete

func (a Slice[T]) Delete(element T) Slice[T]

Delete will remove all elements that are equal to the passed element

func (Slice[T]) DeleteAt

func (a Slice[T]) DeleteAt(index int) Slice[T]

DeleteAt will delete an element by index

func (Slice[T]) DeleteIf

func (a Slice[T]) DeleteIf(block func(T) bool) Slice[T]

DeleteIf will delete all elements which "block" returns true for

func (Slice[T]) Drop

func (a Slice[T]) Drop(count int) Slice[T]

Drop will return an array without the first "count" elements from the beginning

func (Slice[T]) Each

func (a Slice[T]) Each(block func(T))

Each will execute "block" for each element in array

func (Slice[T]) EachIndex

func (a Slice[T]) EachIndex(block func(int))

EachIndex will execute "block" for each element index in array

func (Slice[T]) Fetch

func (a Slice[T]) Fetch(index int, defaultValue T) T

Fetch will return the element in "index", if it doesn't exist it returns the passed "defaultValue"

func (Slice[T]) Fill

func (a Slice[T]) Fill(element T, start int, length int) Slice[T]

Fill will replace elements inplace starting from "start" counting "length" elements with the passed "element" parameter, will return same array object

func (Slice[T]) FillWith

func (a Slice[T]) FillWith(start int, length int, block func(int) T) Slice[T]

FillWith will replace elements from start counting "length" items, passing every index to block and replacing the element inplace with the return value

func (Slice[T]) First

func (a Slice[T]) First() *T

First returns first element of array

func (Slice[T]) Firsts

func (a Slice[T]) Firsts(count int) Slice[T]

Firsts will return an array holding the first "count" elements of the array

func (Slice[T]) Include

func (a Slice[T]) Include(element T) bool

Include will return true if element found in the array

func (Slice[T]) Index

func (a Slice[T]) Index(element T) int

Index returns the index of the first element in array that is equal to "element", returns -1 if the elements if not found

func (Slice[T]) IndexBy

func (a Slice[T]) IndexBy(block func(T) bool) int

IndexBy returns first element that block returns true for, -1 otherwise

func (Slice[T]) Insert

func (a Slice[T]) Insert(index int, elements ...T) Slice[T]

Insert will insert a set of elements in the index and will return a new array

func (Slice[T]) IsEmpty

func (a Slice[T]) IsEmpty() bool

IsEmpty will return true of array empty, false otherwise

func (Slice[T]) IsEq

func (a Slice[T]) IsEq(other Slice[T]) bool

IsEq returns true if array the "other" array

func (Slice[T]) KeepIf

func (a Slice[T]) KeepIf(block func(T) bool) Slice[T]

KeepIf will return an array contains all elements where "block" returned true for them

func (Slice[T]) Last

func (a Slice[T]) Last() *T

Last returns last element of array

func (Slice[T]) Lasts

func (a Slice[T]) Lasts(count int) Slice[T]

Lasts will return an array holding the lasts "count" elements of the array

func (Slice[T]) Len

func (a Slice[T]) Len() int

Len returns number of elements in array

func (Slice[T]) Map

func (a Slice[T]) Map(block func(T) T) Slice[T]

Map will return a new array replacing every element from current array with the return value of the block

func (Slice[T]) Max

func (a Slice[T]) Max(block func(T) int) T

Max returns the element the returned the highest value when passed to block

func (Slice[T]) Min

func (a Slice[T]) Min(block func(T) int) T

Min returns the element the returned the lowest value when passed to block

func (Slice[T]) Pop

func (a Slice[T]) Pop() (Slice[T], T)

Pop removes the last element from the array, returning new array and the element

func (Slice[T]) Push

func (a Slice[T]) Push(element T) Slice[T]

Push appends an element to the array returning a new modified array

func (Slice[T]) Reduce

func (a Slice[T]) Reduce(block func(T) bool) Slice[T]

Reduce is an alias for KeepIf

func (Slice[T]) Reverse

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

Reverse will reverse the array in reverse returning the array reference again

func (Slice[T]) Select

func (a Slice[T]) Select(block func(T) bool) Slice[T]

Select is an alias for KeepIf

func (Slice[T]) SelectUntil added in v0.0.2

func (a Slice[T]) SelectUntil(block func(T) bool) Slice[T]

SelectUntil selects from the start of the slice until the block returns true, excluding the item that returned true.

func (Slice[T]) Shift

func (a Slice[T]) Shift() (T, Slice[T])

Shift will remove the first element of the array returning the element and a modified array

func (Slice[T]) Shuffle

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

Shuffle will randomly shuffle an array elements order returning array reference again

func (Slice[T]) Unique added in v0.0.3

func (a Slice[T]) Unique() Slice[T]

Unique returns a unique list of elements in the slice. order or the result is same order of the items in the source slice

func (Slice[T]) Unshift

func (a Slice[T]) Unshift(element T) Slice[T]

Unshift adds an element to the array returning a new modified array

Jump to

Keyboard shortcuts

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