collect

package module
v0.0.0-...-0906456 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2024 License: MIT Imports: 1 Imported by: 0

README

Collect

Collect is a Go library for dealing with data manipulation.

Installation

Use the package manager Go Module to install Collect.

go get -u github.com/lemmego/collect

Usage

Typically in your projects, you would import the package like this:

// main.go
package /path/to/your/pkg

import (
    "github.com/lemmego/collect"
)

func main() {
    arr := ....
    collect.Each(arr, ...)
    collect.Map(arr, ...)
    collect.Filter(arr, ...)
    ....
}

See the collect_test.go file to see the example usage of the functions. *Note: Since this is from an adjacent _test.go file under the same package, the package prefix (collect.) wasn't required.*

// collect_test.go
package collect

import (
	"testing"
)

func TestEach(t *testing.T) {
	arr := []int{1, 2, 3}
	Each(arr, func(x int, i int) {
		if x != i+1 {
			t.Errorf("Expected %d, got %d", i+1, x)
		}
	})
}

func TestMap(t *testing.T) {
	arr := []int{1, 2, 3}
	result := Map(arr, func(x int, i int) int {
		return x + i
	})
	if result[0] != 1 {
		t.Errorf("Expected 1, got %d", result[0])
	}
	if result[1] != 3 {
		t.Errorf("Expected 3, got %d", result[1])
	}
	if result[2] != 5 {
		t.Errorf("Expected 5, got %d", result[2])
	}
}

func TestFilter(t *testing.T) {
	arr := []int{1, 2, 3, 4, 5}
	result := Filter(arr, func(x int, i int) bool {
		return x%2 == 0
	})
	if result[0] != 2 {
		t.Errorf("Expected 2, got %d", result[0])
	}
	if result[1] != 4 {
		t.Errorf("Expected 4, got %d", result[1])
	}
}

func TestCount(t *testing.T) {
	arr := []int{1, 2, 3, 4, 5}
	result := Count(arr, func(x int, i int) bool {
		return x%2 == 0
	})
	if result != 2 {
		t.Errorf("Expected 2, got %d", result)
	}
}

func TestFindIndex(t *testing.T) {
	arr := []int{1, 2, 3, 4, 5}
	result := FindIndex(arr, func(x int) bool {
		return x == 3
	})
	if result != 2 {
		t.Errorf("Expected 2, got %d", result)
	}
}

func TestFindLastIndex(t *testing.T) {
	arr := []int{1, 2, 3, 4, 5}
	result := FindLastIndex(arr, func(x int) bool {
		return x == 3
	})
	if result != 2 {
		t.Errorf("Expected 2, got %d", result)
	}
}

func TestFind(t *testing.T) {
	arr := []int{1, 2, 3, 4, 5}
	result, ok := Find(arr, func(x int) bool {
		return x == 3
	})
	if !ok || result != 3 {
		t.Errorf("Expected 3, got %d", result)
	}
}

func TestFindLast(t *testing.T) {
	arr := []int{1, 2, 3, 4, 5}
	result, ok := FindLast(arr, func(x int) bool {
		return x == 3
	})
	if !ok || result != 3 {
		t.Errorf("Expected 3, got %d", result)
	}
}

func TestSome(t *testing.T) {
	arr := []int{1, 2, 3, 4, 5}
	result := Some(arr, func(x int, i int) bool {
		return x == 3
	})
	if !result {
		t.Errorf("Expected true, got %t", result)
	}
}

func TestNone(t *testing.T) {
	arr := []int{1, 2, 3, 4, 5}
	result := None(arr, func(x int, i int) bool {
		return x == 6
	})
	if !result {
		t.Errorf("Expected true, got %t", result)
	}
}

func TestEvery(t *testing.T) {
	arr := []int{2, 4, 6, 8, 10}
	result := Every(arr, func(x int, i int) bool {
		return x%2 == 0
	})
	if !result {
		t.Errorf("Expected true, got %t", result)
	}
}

func TestConcat(t *testing.T) {
	arr := []int{1, 2, 3}
	arr2 := []int{4, 5, 6}
	result := Concat(arr, arr2)
	if len(result) != 6 {
		t.Errorf("Expected 6, got %d", len(result))
	}

	for i, x := range result {
		if x != i+1 {
			t.Errorf("Expected %d, got %d", i+1, x)
		}
	}
}

func TestConcatMap(t *testing.T) {
	arr := []int{1, 2, 3}
	result := ConcatMap(arr, func(x int) []int {
		return []int{x, x * 2}
	})
	t.Log(result)
	if len(result) != 6 {
		t.Errorf("Expected 6, got %d", len(result))
	}
}

func TestReverse(t *testing.T) {
	arr := []int{1, 2, 3}
	result := Reverse(arr)
	if result[0] != 3 {
		t.Errorf("Expected 3, got %d", result[0])
	}
	if result[1] != 2 {
		t.Errorf("Expected 2, got %d", result[1])
	}
	if result[2] != 1 {
		t.Errorf("Expected 1, got %d", result[2])
	}
}

func TestUniq(t *testing.T) {
	arr := []int{1, 2, 3, 2, 1}
	result := Uniq(arr)
	if len(result) != 3 {
		t.Errorf("Expected 3, got %d", len(result))
	}
}

func TestUniqBy(t *testing.T) {
	arr := []int{1, 2, 3, 2, 1}
	result := UniqBy(arr, func(x int) int {
		return x
	})
	if len(result) != 3 {
		t.Errorf("Expected 3, got %d", len(result))
	}
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Concat

func Concat[T any](xs []T, ys []T) []T

func ConcatMap

func ConcatMap[T any, U any](xs []T, f func(T) []U) []U

func Count

func Count[T any](xs []T, f func(T, int) bool) int

func Each

func Each[T any](xs []T, f func(T, int))

func Every

func Every[T any](xs []T, f func(T, int) bool) bool

func Filter

func Filter[T any](xs []T, f func(T, int) bool) []T

func Find

func Find[T any](xs []T, f func(T) bool) (T, bool)

func FindIndex

func FindIndex[T any](xs []T, f func(T) bool) int

func FindLast

func FindLast[T any](xs []T, f func(T) bool) (T, bool)

func FindLastIndex

func FindLastIndex[T any](xs []T, f func(T) bool) int

func Map

func Map[T any, U any](xs []T, f func(T, int) U) []U

func None

func None[T any](xs []T, f func(T, int) bool) bool

func Reduce

func Reduce[T any](xs []T, f func(T, T, int) T, initial T) T

func Reverse

func Reverse[T any](xs []T) []T

func Some

func Some[T any](xs []T, f func(T, int) bool) bool

func Uniq

func Uniq[T comparable](xs []T) []T

func UniqBy

func UniqBy[T any, U comparable](xs []T, f func(T) U) []T

Types

type MapCollection

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

func NewMap

func NewMap[K comparable, V any](xs map[K]V) *MapCollection[K, V]

func (*MapCollection[K, V]) All

func (mc *MapCollection[K, V]) All() map[K]V

func (*MapCollection[K, V]) At

func (mc *MapCollection[K, V]) At(i K) V

func (*MapCollection[K, V]) Each

func (mc *MapCollection[K, V]) Each(f func(V, K)) *MapCollection[K, V]

func (*MapCollection[K, V]) Filter

func (mc *MapCollection[K, V]) Filter(f func(V, K) bool) *MapCollection[K, V]

func (*MapCollection[K, V]) Find

func (mc *MapCollection[K, V]) Find(f func(V) bool) (V, error)

func (*MapCollection[K, V]) Get

func (mc *MapCollection[K, V]) Get() map[K]V

func (*MapCollection[K, V]) Items

func (mc *MapCollection[K, V]) Items() map[K]V

func (*MapCollection[K, V]) Keys

func (mc *MapCollection[K, V]) Keys() *SliceCollection[K]

func (*MapCollection[K, V]) Len

func (mc *MapCollection[K, V]) Len() int

func (*MapCollection[K, V]) Map

func (mc *MapCollection[K, V]) Map(f func(V, K) V) *MapCollection[K, V]

func (*MapCollection[K, V]) Reduce

func (mc *MapCollection[K, V]) Reduce(f func(V, V, K) V, initial V) V

func (*MapCollection[K, V]) Values

func (mc *MapCollection[K, V]) Values() *SliceCollection[V]

type SliceCollection

type SliceCollection[T any] struct {
	// contains filtered or unexported fields
}

func NewSlice

func NewSlice[T any](xs []T) *SliceCollection[T]

func (*SliceCollection[T]) All

func (sc *SliceCollection[T]) All() []T

func (*SliceCollection[T]) At

func (sc *SliceCollection[T]) At(i int) T

func (*SliceCollection[T]) Each

func (sc *SliceCollection[T]) Each(f func(T, int)) *SliceCollection[T]

func (*SliceCollection[T]) Filter

func (sc *SliceCollection[T]) Filter(f func(T, int) bool) *SliceCollection[T]

func (*SliceCollection[T]) Find

func (sc *SliceCollection[T]) Find(f func(T) bool) (T, error)

func (*SliceCollection[T]) FindIndex

func (sc *SliceCollection[T]) FindIndex(f func(T) bool) int

func (*SliceCollection[T]) FindLast

func (sc *SliceCollection[T]) FindLast(f func(T) bool) (T, bool)

func (*SliceCollection[T]) FindLastIndex

func (sc *SliceCollection[T]) FindLastIndex(f func(T) bool) int

func (*SliceCollection[T]) Get

func (sc *SliceCollection[T]) Get() []T

func (*SliceCollection[T]) Items

func (sc *SliceCollection[T]) Items() []T

func (*SliceCollection[T]) Len

func (sc *SliceCollection[T]) Len() int

func (*SliceCollection[T]) Map

func (sc *SliceCollection[T]) Map(f func(T, int) T) *SliceCollection[T]

func (*SliceCollection[T]) Reduce

func (sc *SliceCollection[T]) Reduce(f func(T, T, int) T, initial T) T

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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