Documentation ¶
Overview ¶
Package fun provides simple and useful functional (and other) utilities that often has to be made by hand. Utilizes power of Go 1.18 generics.
Index ¶
- func All[T comparable](iterable []T) bool
- func Any[T comparable](iterable []T) bool
- func Equal[T comparable](a, b []T) bool
- func Filter[T any](fn func(item T) bool, iterable []T) []T
- func Index[T comparable](slice []T, value T) int
- func IndexAB[T comparable](slice []T, value T, a, b int) (int, error)
- func Less[T constraints.Ordered](less, big T) bool
- func Map[IN, OUT any](fn func(IN) OUT, data []IN) []OUT
- func Max[T any](less func(less, big T) bool, values ...T) T
- func Min[T any](less func(less, big T) bool, values ...T) T
- func Reduce[A, I any](fn func(acc A, item I) A, iterable []I, init A) A
- func Reverse[T any](slice []T)
- func Reversed[T any](slice []T) []T
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func All ¶ added in v0.0.8
func All[T comparable](iterable []T) bool
All returns true if all the values in iterable are non-nil, false otherwise.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { result1 := fun.All([]string{"hello", "gentlemen", "!"}) result2 := fun.All([]string{"hello", "gentlemen", ""}) fmt.Println(result1) fmt.Println(result2) }
Output: true false
func Any ¶ added in v0.0.8
func Any[T comparable](iterable []T) bool
Any returns true if there's any non-nil values in iterable, false otherwise.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { result1 := fun.Any([]string{"", "a", ""}) result2 := fun.Any([]string{"", "", ""}) fmt.Println(result1) fmt.Println(result2) }
Output: true false
func Equal ¶
func Equal[T comparable](a, b []T) bool
Equal compares two flat slices that contain comparable type.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { result1 := fun.Equal([]int{1, 2, 3, 4}, []int{1, 2, 3, 4}) result2 := fun.Equal([]int{1, 2, 3, 4}, []int{1, 2, 3, 4, 5}) result3 := fun.Equal([]int{1, 2, 3, 4}, []int{1, 2, 3, 1}) fmt.Println(result1) fmt.Println(result2) fmt.Println(result3) }
Output: true false false
func Filter ¶
Filter returns new slice of items, that fn(item) == true.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { result := fun.Filter(func(item string) bool { return item[0] == 'c' }, []string{"cunt", "sucka", "car", "tree"}) fmt.Println(result) }
Output: [cunt car]
func Index ¶
func Index[T comparable](slice []T, value T) int
Index returns the index of first encountered "value" in slice. If value is not found -1 is returned.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { // Note: only first encounter is returned result := fun.Index([]string{"Hello", "gentlemen", "!", "gentlemen"}, "gentlemen") fmt.Println(result) }
Output: 1
func IndexAB ¶
func IndexAB[T comparable](slice []T, value T, a, b int) (int, error)
IndexAB returns the index of first encountered "value" in slice within interval [a, b). If [a, b) interval is incorrect returns 0 and error.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { // Note: only first encounter is returned result, _ := fun.IndexAB([]string{"Hello", "gentlemen", "!", "gentlemen"}, "gentlemen", 2, 4) fmt.Println(result) }
Output: 3
func Less ¶
func Less[T constraints.Ordered](less, big T) bool
Less is default comparator function for builtin data-types for Min or Max.
func Map ¶
func Map[IN, OUT any](fn func(IN) OUT, data []IN) []OUT
Map takes function that takes a value of type IN and returns a value of type OUT and a slice of type IN. Returns slice of type OUT.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { result := fun.Map(func(n int) int64 { return int64(n * n) }, []int{1, 2, 3, 4}) fmt.Println(result) }
Output: [1 4 9 16]
func Max ¶
Max returns maximum out of values. To compare values "less" function needs to be provided. Basic example is Less. If no values provided returns zero value of T.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { type Test struct { A int } result1 := fun.Max(fun.Less[int], []int{1, 2, -1, 3}...) result2 := fun.Max( func(less, bigger Test) bool { return less.A < bigger.A }, []Test{{1}, {3}, {-2}, {5}}..., ) fmt.Println(result1) fmt.Println(result2) }
Output: 3 {5}
func Min ¶
Min returns minimum out of values. To compare values "less" function needs to be provided. Basic example is Less. If no values provided returns zero value of T.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { type Test struct { A int } result1 := fun.Min(fun.Less[int], []int{1, 2, -1, 3}...) result2 := fun.Min( func(less, bigger Test) bool { return less.A < bigger.A }, []Test{{1}, {3}, {-2}, {5}}..., ) fmt.Println(result1) fmt.Println(result2) }
Output: -1 {-2}
func Reduce ¶
func Reduce[A, I any](fn func(acc A, item I) A, iterable []I, init A) A
Reduce applies function fn of two args cumulatively to the items of iterable, from left to right, to reduce the iterable to a single value, starting from init value.
Example ¶
package main import ( "fmt" "math" fun "github.com/kirilldd2/go-no-fun" ) func main() { result := fun.Reduce( func(acc float64, item int) float64 { return acc + math.Pow(float64(item), 2) }, []int{1, 2, 3, 4}, 0, ) fmt.Println(result) }
Output: 30
func Reverse ¶
func Reverse[T any](slice []T)
Reverse reverses slice in-place.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { slice := []string{"!", "gentlemen", "Hello"} fun.Reverse(slice) fmt.Println(slice) }
Output: [Hello gentlemen !]
func Reversed ¶
func Reversed[T any](slice []T) []T
Reversed returns reversed slice, original slice is not mutated.
Example ¶
package main import ( "fmt" fun "github.com/kirilldd2/go-no-fun" ) func main() { slice := []string{"!", "gentlemen", "Hello"} newSlice := fun.Reversed(slice) fmt.Println(slice) fmt.Println(newSlice) }
Output: [! gentlemen Hello] [Hello gentlemen !]
Types ¶
This section is empty.