slice

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

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

Go to latest
Published: Jan 5, 2022 License: BSD-2-Clause Imports: 1 Imported by: 30

README

slice

slice is a simple Go package to provide generic versions of Map, Reduce and Filter on slices. I mainly wrote it as an exercise to get more familar with Go 1.18's new type parameterization feature, but decided to publish it in case it is useful for others.

package main

import (
	"fmt"
	"math"

	"github.com/akrennmair/slice"
)

func main() {
	someNumbers := []float64{1.0, 2.0, 4.0, 23.5, 42.9}
	fmt.Printf("sqrt(%v) = %v\n", someNumbers, slice.Map(someNumbers, func(v float64) float64 {
		return math.Sqrt(v)
	}))

	someStrings := []string{"hello", "to", "the", "golang", "community"}
	fmt.Printf("len(%v) = %v\n", someStrings, slice.Map(someStrings, func(s string) int {
		return len(s)
	}))
	fmt.Printf("total length: %d\n", slice.Reduce(slice.Map(someStrings, func(s string) int {
		return len(s)
	}), func(acc, i int) int {
		return acc + i
	}))

	moreNumbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	fmt.Printf("even numbers: %v\n", slice.Filter(moreNumbers, func(i int) bool {
		return i%2 == 0
	}))
}

channel

An additional package github.com/akrennmair/slice/channel is also provided that contains Map, Reduce and Filter functions that operate on chans.

Authors

License

See the file LICENSE for license information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Filter

func Filter[T any](input []T, pred func(T) bool) (output []T)

Filter returns a new slice with all elements from the from the input elements for which the provided predicate function returns true.

func Map

func Map[T1, T2 any](input []T1, f func(T1) T2) (output []T2)

Map returns a new slice populated with the result of calling the provided function on every element in the provided input slice.

func MapConcurrent

func MapConcurrent[T1, T2 any](input []T1, f func(T1) T2) (output []T2)

MapConcurrent does the same as Map, but concurrently.

func MapConcurrentWithContext

func MapConcurrentWithContext[T1, T2 any](ctx context.Context, input []T1, f func(T1) T2) (output []T2)

MapConcurrentWithContext does the same as Map, but concurrently, and receives a context.Context to be cancellable.

func Reduce

func Reduce[T1, T2 any](input []T1, f func(T2, T1) T2) T2

Reduce executes a provided function on each element of the slice in order, passing the return value of the previous function call on the preceding slice element. The final result of running the provided function across all slice elements is returned.

func ReduceWithInitialValue

func ReduceWithInitialValue[T1, T2 any](input []T1, acc T2, f func(T2, T1) T2) T2

ReduceWithInitialValue executes a provided function on each element of the slice in order, passing the return value of the previous function call on the preceding slice element. Unlike Reduce, the initial value of the accumulator can be provided as an argument. The final result of running the provided function across all slice elements is returned.

func ToChan

func ToChan[T any](in []T) <-chan T

ToChan returns a chan that produces all elements of the provided input slice in order.

func ToChanWithContext

func ToChanWithContext[T any](ctx context.Context, in []T) <-chan T

ToChanWithContext does the same as ToChan, but is cancellable through the provided context.Context object.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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