fn

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

README

go-fn

A simple Go library that provides various functional programming features.

  • Lightweight
  • No dependencies
  • No custom types

Installation

go get github.com/Wing924/go-fn

Usage

package main

import (
	"fmt"
	"github.com/Wing924/go-fn"
)

func main() {
	slice := []int{1, 2, 3}

	// Map
	result := fn.Map(slice, func(i int) int {
		return i * 2
	})
	fmt.Println(result) // [2 4 6]
	
	// Reduce
    sum := fn.Reduce(slice, 0, func(acc, i int) int {
        return acc + i
    })
    fmt.Println(sum) // 6
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Any

func Any[S ~[]T, T any](slice S, f func(value T) bool) bool

Any test whether at least one element in the slice passes the test implemented by the provided function.

func At

func At[S ~[]T, T any](slice S, index int) T

At returns the element at the specified index in the slice. Negative index count back from the last item in the slice.

func AtOptional

func AtOptional[S ~[]T, T any](slice S, index int, defaultValue T) T

AtOptional returns the element at the specified index in the slice or a default value if the index is out of bounds. Negative index count back from the last item in the slice.

func Every

func Every[S ~[]T, T any](slice S, f func(value T) bool) bool

Every test whether all elements in the slice pass the test implemented by the provided function.

func Fill

func Fill[S ~[]T, T any](slice S, value T)

Fill changes all elements in the slice to a static value.

func Filter

func Filter[S ~[]T, T any](slice S, f func(value T) bool) []T

Filter creates a new slice with all elements that pass the test implemented by the provided function.

func GetOptional

func GetOptional[M ~map[K]V, K comparable, V any](m M, key K, defaultValue V) V

GetOptional returns the value associated with the key in the map m. If the key is not present, it returns the default value.

func Keys

func Keys[M ~map[K]V, K comparable, V any](m M) []K

Keys returns the keys of the map m. The keys will be in an indeterminate order.

func Map

func Map[S ~[]T, T any, U any](slice S, f func(value T) U) []U

Map applies a function to each element in the slice and returns a new slice with the results.

func MapInPlace

func MapInPlace[S ~[]T, T any](slice S, f func(value T) T)

MapInPlace applies a function to each element in the slice and modifies the slice in place.

func MapInPlaceWithIndex

func MapInPlaceWithIndex[S ~[]T, T any](slice S, f func(value T, index int) T)

MapInPlaceWithIndex applies a function to each element in the slice and modifies the slice in place.

func MapWithIndex

func MapWithIndex[S ~[]T, T any, U any](slice S, f func(value T, index int) U) []U

MapWithIndex applies a function to each element in the slice and returns a new slice with the results.

func Merge

func Merge[M ~map[K]V, K comparable, V any](original M, overrides ...M) M

Merge returns a new map that combines the contents of the original and the contents of the given maps.

func MergeInPlace

func MergeInPlace[M ~map[K]V, K comparable, V any](original M, overrides ...M)

MergeInPlace adds the contents of the given maps to the original.

func Reduce

func Reduce[S ~[]T, T any, U any](s S, init U, f func(accumulator U, currentValue T) U) U

Reduce applies a function against an accumulator and each element in the slice (from left to right) to reduce it to a single value.

func ReduceRight

func ReduceRight[S ~[]T, T any, U any](s S, init U, f func(accumulator U, currentValue T) U) U

ReduceRight applies a function against an accumulator and each element in the slice (from right to left) to reduce it to a single value.

func ReduceRightWithIndex

func ReduceRightWithIndex[S ~[]T, T any, U any](slice S, init U, f func(accumulator U, currentValue T, currentIndex int) U) U

ReduceRightWithIndex applies a function against an accumulator and each element in the slice (from right to left) to reduce it to a single value.

func ReduceWithIndex

func ReduceWithIndex[S ~[]T, T any, U any](s S, init U, f func(accumulator U, currentValue T, currentIndex int) U) U

ReduceWithIndex applies a function against an accumulator and each element in the slice (from left to right) to reduce it to a single value.

func SortedKeys

func SortedKeys[M ~map[K]V, K cmp.Ordered, V any](m M) []K

SortedKeys returns the keys of the map m in sorted order.

func SortedKeysFunc

func SortedKeysFunc[M ~map[K]V, K comparable, V any](m M, cmp func(a, b K) int) []K

SortedKeysFunc returns the keys of the map m in sorted order using the given comparison function.

func Values

func Values[M ~map[K]V, K comparable, V any](m M) []V

Values returns the values of the map m. The values will be in an indeterminate order.

Types

type Entry

type Entry[K comparable, V any] struct {
	Key   K
	Value V
}

Entry represents a key-value pair in a map.

func Entries

func Entries[M ~map[K]V, K comparable, V any](m M) []Entry[K, V]

Entries returns the (key, value) pairs of the map m. The pairs will be in an indeterminate order.

Jump to

Keyboard shortcuts

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