goslices

package module
v1.0.0 Latest Latest
Warning

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

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

README

linters test codecov release

go-slices

The goal in mind for this go-slices module is to provide basic generics to cover all the common operations a developer will typically perform on a slice.

Installation

go get github.com/skippyza/go-slices

Examples

Filter even numbers

goslices.Filter([]int{1, 2, 3, 4, 5}, func(i int) bool { return i%2 == 0 })
// [2 4]

Double all the numbers

double := func(i int) int { return i*2 }
goslices.Map([]int{8, 14, 23}, double)
// [16 28 46]

Sum all the numbers in a slice

sum := func(a, b int) int { return a+b }
goslices.Reduce([]int{5, 1, 2, 2}, sum, 0)
// 10

Usage

See DOCUMENTATION for more info.

Documentation

Overview

go-slices provides generic functions for working with slices

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("item not found")

Functions

func All

func All[S ~[]T, T any](arr S, fn PredicateFn[T]) bool
Example
package main

import (
	"fmt"

	goslices "github.com/skippyza/go-slices"
)

func main() {
	isEven := func(i int) bool { return i%2 == 0 }

	result := goslices.All([]int{2, 4, 6, 8, 10}, isEven)
	fmt.Println(result)
	result = goslices.All([]int{1, 2, 4, 6, 8, 9, 10}, isEven)
	fmt.Println(result)

}
Output:

true
false

func Any

func Any[S ~[]T, T any](arr S, fn PredicateFn[T]) bool
Example
package main

import (
	"fmt"

	goslices "github.com/skippyza/go-slices"
)

func main() {
	isEven := func(i int) bool { return i%2 == 0 }

	result := goslices.Any([]int{1, 2, 3, 4, 5}, isEven)
	fmt.Println(result)
	result = goslices.Any([]int{1, 3, 5}, isEven)
	fmt.Println(result)

}
Output:

true
false

func Each

func Each[S ~[]T, T any](arr S, fn func(T))
Example
package main

import (
	"fmt"

	goslices "github.com/skippyza/go-slices"
)

func main() {
	printLine := func(s string) { fmt.Println(s) }

	goslices.Each([]string{"test 1", "test 2", "test 3"}, printLine)

}
Output:

test 1
test 2
test 3

func Filter

func Filter[S ~[]T, T any](arr S, fn PredicateFn[T]) S
Example
package main

import (
	"fmt"

	goslices "github.com/skippyza/go-slices"
)

func main() {
	isEven := func(i int) bool { return i%2 == 0 }
	result := goslices.Filter([]int{1, 2, 3, 4, 5}, isEven)
	fmt.Println(result)

}
Output:

[2 4]

func Find

func Find[S ~[]T, T any](arr S, fn PredicateFn[T]) (T, error)
Example
package main

import (
	"fmt"

	goslices "github.com/skippyza/go-slices"
)

func main() {
	isEven := func(i int) bool { return i%2 == 0 }

	result, err := goslices.Find([]int{1, 3, 4, 6, 7}, isEven)
	fmt.Println(result, err)
	result, err = goslices.Find([]int{1, 3, 5, 7}, isEven)
	fmt.Println(result, err)

}
Output:

4 <nil>
0 item not found

func Map

func Map[S ~[]T, T any, U any](arr S, fn MapFn[T, U]) []U
Example
package main

import (
	"fmt"
	"strconv"

	goslices "github.com/skippyza/go-slices"
)

func main() {
	withPrefix := func(i int) string { return "pre-" + strconv.Itoa(i) }

	result := goslices.Map([]int{1, 2, 3}, withPrefix)
	fmt.Println(result)

}
Output:

[pre-1 pre-2 pre-3]

func Reduce

func Reduce[S ~[]T, T any, U any](arr S, fn AccumulatorFn[T, U], initial U) U
Example
package main

import (
	"fmt"

	goslices "github.com/skippyza/go-slices"
)

func main() {
	sum := func(last, cur int) int { return last + cur }

	result := goslices.Reduce([]int{2, 4, 6, 8}, sum, 0)
	fmt.Println(result)

}
Output:

20

Types

type AccumulatorFn

type AccumulatorFn[T any, U any] func(U, T) U

type MapFn

type MapFn[T any, U any] func(T) U

type PredicateFn

type PredicateFn[T any] func(T) bool

Jump to

Keyboard shortcuts

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