gunc

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: May 18, 2024 License: LGPL-2.1 Imports: 1 Imported by: 1

README

Go Reference

gunc is a collection of functional programming and array programming utilities for Go slices.

No dependencies, just the standard library and generics!

Features

No more for loops for simple range number slices

s1 := gunc.Range(-3, 3) // [-3, -2, -1, 0, 1, 2, 3]
s2 := gunc.RangeWithStep(0, 1, 0.2) // [0.0, 0.2, 0.4, 0.6, 0.8, 1.0]

The classics: apply, filter, reduce

// Also check out gunc.ApplyInPlace()!
s1 = gunc.Apply(s1, func(e int) int { return e + 3 }) // [0, 1, 2, 3, 4, 5, 6]
s1 = gunc.Filter(s1, func(e int) bool { return e%3 == 0 }) // [0, 3, 6]
res := gunc.Reduce(s1, func(a, b int) int { return a + b }) // 9

Want the functions to use indexes also? ApplyWithIndex(), FilterWithIndex() and ReduceWithIndex() got you covered. Wanna keep Reduce()'s elements? Scan() is here from APL land.

License

MIT is lame. LGPL is the way to go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply[S ~[]E, E any, R any](slice S, fn func(E) R) []R

Apply applies function fn to each element of the slice and returns it.

The returning slice can be of a differnt type.

func ApplyInPlace added in v1.1.0

func ApplyInPlace[S ~[]E, E any](slice *S, fn func(E) E)

Like Apply but works on the slice directly/in-place.

func ApplyInPlaceWithIndex added in v1.1.0

func ApplyInPlaceWithIndex[S ~[]E, E any](slice *S, fn func(int, E) E)

Like ApplyInPlace, but function fn takes the index of the element as a parameter.

func ApplyWithIndex

func ApplyWithIndex[S ~[]E, E any, R any](slice S, fn func(int, E) R) []R

Like Apply, but function fn takes the index of the element as a parameter.

func Filter

func Filter[S ~[]E, E any](slice S, fn func(E) bool) S

Filter creates a new slice with only the values for which fn returns true.

func FilterWithIndex

func FilterWithIndex[S ~[]E, E any](slice S, fn func(int, E) bool) S

Like Filter, but function fn takes the index of the element as a parameter.

func Range

func Range[N Number](from N, to N) []N

Alias for RangeWithStep(from, to, 1).

func RangeWithStep

func RangeWithStep[N Number](from N, to N, step N) []N

Creates a slice of numbers with a specific step in an inclusive range [from, to]. Supports descending ranges.

Panics if step is negative (should still be positive for descending ranges).

func Reduce

func Reduce[S ~[]E, E any](slice S, fn func(E, E) E) E

Return value after applying dyadic function fn to each neighboring element from left to right, where a is the element on the left and b on the right.

func ReduceRight added in v1.1.0

func ReduceRight[S ~[]E, E any](slice S, fn func(E, E) E) E

Like Reduce but from right to left. a is the element on the right while b is the one on the left.

func Reverse

func Reverse[S ~[]E, E any](s S) S

Reverse wraps slices.Reverse, but returns the slice instead.

func Scan

func Scan[S ~[]E, E any](slice S, fn func(E, E) E) S

Reduce, but keep intermediate values. Based on APL's scan.

func ScanWithIndex

func ScanWithIndex[S ~[]E, E any](
	slice S, fn func(e1 E, i1 int, e2 E, i2 int) E) S

Like Scan, but function fn takes the index of the element as a parameter.

Types

type Number

type Number interface {
	float32 | float64 |
		uint | uint8 | uint16 | uint32 | uint64 |
		int | int8 | int16 | int32 | int64
}

A number is a number

Jump to

Keyboard shortcuts

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