funcy

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 2 Imported by: 0

README

CI Go Documentation

funcy ("funky")

A minimal Go package providing a few functional utilities using Go generics.

Nothin' too special; largely just for my own learning and casual use, but perhaps this useful to you too.

Test

make

API

Go Documentation

Summary:

package funcy // import "github.com/mdb/funcy"

var ErrNotFound = errors.New("not found")
func Any[E any](s []E, f evaluator[E]) bool
func Contains[E comparable](s []E, v E) bool
func Dedupe[E comparable](s []E) []E
func Filter[E any](s []E, f retainer[E]) []E
func Find[E any](s []E, f finder[E]) (E, error)
func Map[E any](s []E, f mapper[E]) []E
func Quicksort[E constraints.Ordered](arr []E) []E
func Reduce[E any](s []E, init E, f reducer[E]) E
func ReduceRight[E any](s []E, init E, f reducer[E]) E
func Reverse[E any](s []E) []E
func Sort[E constraints.Ordered](s []E) []E

Documentation

Overview

Package funcy provides a collection of functional utilities using generics.

Index

Examples

Constants

This section is empty.

Variables

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

Functions

func Any

func Any[E any](s []E, f evaluator[E]) bool

Any applies the function it's passed to each element in the slice and returns true for the first element for which the function returns true. Otherwise, it returns false.

Example
list := []int{3, 1, 2}

fmt.Println(Any(list, func(item int) bool {
	return item%3 == 0
}))
Output:

true

func Contains

func Contains[E comparable](s []E, v E) bool

Contains returns true if the slice it's passed contains the element it's passed.

Example
list := []string{"foo", "bar", "baz"}

fmt.Println(Contains(list, "foo"))
Output:

true

func Dedupe

func Dedupe[E comparable](s []E) []E

Dedupe returns the slice it's passed with duplicate elements removed.

Example
list := []string{"foo", "foo", "bar"}

fmt.Println(Dedupe(list))
Output:

[foo bar]

func Filter

func Filter[E any](s []E, f retainer[E]) []E

Filter applies the function it's passed to each element in the slice it's passed to filter out unwanted elements and return the resulting, filtered slice.

Example
list := []int{3, 1, 2}

fmt.Println(Filter(list, func(item int) bool {
	return item == 3
}))
//
Output:

[3]

func Find

func Find[E any](s []E, f finder[E]) (E, error)

Find applies the function it's passed to each element in the slice and returns the first element for which the function returns true.

Example
list := []int{3, 1, 2}

fmt.Println(Find(list, func(item int) bool {
	return item%3 == 0
}))
Output:

3 <nil>

func Map

func Map[E any](s []E, f mapper[E]) []E

Map applies the function it's passed to each element in the slice it's passed and returns the resulting slice.

Example
list := []int{3, 1, 2}

fmt.Println(Map(list, func(item int) int {
	return item + 2
}))
Output:

[5 3 4]

func Quicksort

func Quicksort[E constraints.Ordered](arr []E) []E

Quicksort implements the quicksort algorithm to sort the slice it's passed.

func Reduce

func Reduce[E any](s []E, init E, f reducer[E]) E

Reduce executes the function it's passed on each element of the slice it's passed, in order and beginning with the specified initial value, passing in the return value from the calculation on the preceding element. It returns the final result, after operating on all elements in the slice.

Example
list := []int{3, 1, 2}

fmt.Println(Reduce(list, 0, func(cur, val int) int {
	return cur + val
}))
Output:

6

func ReduceRight

func ReduceRight[E any](s []E, init E, f reducer[E]) E

ReduceRight executes the function it's passed on each element of the slice it's passed, in reverse order and beginning with the specified initial value, passing in the return value from the calculation on the preceding element. It returns the final result, after operating on all elements in the slice.

Example
list := []int{2, 5, 10}

fmt.Println(ReduceRight(list, 20, func(cur, val int) int {
	return cur - val
}))
Output:

3

func Reverse

func Reverse[E any](s []E) []E

Reverse returns the slice it's passed in reverse order.

Example
list := []string{"foo", "bar", "baz"}

fmt.Println(Reverse(list))
Output:

[baz bar foo]

func Sort

func Sort[E constraints.Ordered](s []E) []E

Sort returns the slice it's passed with all elements in ascending sorted order. It uses Quicksort.

Example
list := []int{3, 1, 2}

fmt.Println(Sort(list))
Output:

[1 2 3]

Types

This section is empty.

Jump to

Keyboard shortcuts

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