fn

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: May 6, 2024 License: MIT Imports: 2 Imported by: 0

README

fn

fn is a Go package that provides a set of functional programming tools for Go. It is inspired by the functional programming features of languages like Haskell, Scala, Rust and JavaScript.

CI on main

Go Report Card Go Reference

Installation

go get github.com/abiiranathan/fn

API

Functions:

  • Filter: Returns a new slice containing only the elements that satisfy the predicate.
  • Map: Returns a new slice containing the results of applying a function to each element.
  • Reduce: Accumulates the elements of a slice by applying a function.
  • Concat: Concatenates two slices.
  • IndexOf: Returns the index of the first occurrence of an element.
  • Distinct: Returns a new slice containing only the unique elements of a slice.
  • DistinctFunc: Returns a new slice containing only the unique elements of a slice based on a key function.
  • Chunk: Returns a new slice containing slices of a specified size.
  • Shuffle: Randomizes the order of elements in a slice.
  • Partition: Partitions a slice into two based on a predicate function.
  • Flatten: Flattens a slice of slices into a single slice.
  • Reverse: Reverses the elements of a slice in place.
  • Take: Returns the first n elements of a slice.
  • TakeWhile: Returns elements from the beginning of a slice as long as a condition is true.
  • Drop: Returns a new slice without the first n elements.
  • Count: Returns the number of elements in a slice that satisfy a condition.
  • All: Returns true if all elements in a slice satisfy a condition.
  • Any: Returns true if any element in a slice satisfies a condition.
  • Zip: Applies a function to pairs of elements from two slices.
  • ZipShortest: Applies a function to pairs of elements from two slices, stopping at the shorter slice.
  • ZipWithIndex: Applies a function to elements of a slice and their index.
  • RotateLeft: Rotates the elements of a slice to the left.
  • RotateRight: Rotates the elements of a slice to the right.

Usage

  1. Filter
s := []int{1, 2, 3, 4, 5}
is_even := func(v int) bool { return v%2 == 0 }
got := fn.Filter(s, is_even)
fmt.Println(got) // [2 4]
  1. Map
s := []int{1, 2, 3, 4, 5}
double := func(v int) int { return v * 2 }
got := fn.Map(s, double)
fmt.Println(got) // [2 4 6 8 10]
  1. Reduce
type person struct {
    Name string
    Age  int
}

s2 := []person{
    {"Alice", 25},
    {"Bob", 30},
    {"Charlie", 35},
}

fn2 := func(acc int, p person) int { return acc + p.Age }
got2 := fn.Reduce(s2, 0, f2)
fmt.Println(got2) // 90


// More advanced example
fn3 := func(acc map[string]int, p person) map[string]int {
    acc[p.Name] = p.Age
    return acc
}
got3 := fn.Reduce(s2, make(map[string]int), f3)

fmt.Println(got3) // map[Alice:25 Bob:30 Charlie:35]
  1. Concat
s1 := []int{1, 2, 3}
s2 := []int{4, 5, 6}
got := fn.Concat(s1, s2)
fmt.Println(got) // [1 2 3 4 5 6]
  1. IndexOf
s := []int{1, 2, 3, 4, 5}
got := fn.IndexOf(s, 3)
fmt.Println(got) // 2
  1. Distinct
s := []int{1, 2, 2, 3, 3, 3}
got := fn.Distinct(s)
fmt.Println(got) // [1 2 3]
  1. DistinctFunc
type person struct {
    Name string
    Age  int
}

s := []person{
    {"Bob", 30},
    {"Alice", 25},
    {"Bob", 30},
    {"Alice", 35},
    {"Jane", 12},
}

key := func(p person) string { return p.Name }
got := fn.DistinctFunc(s, key)
fmt.Println(got) // [{Bob 30} {Alice 25} {Jane 12}]
  1. Chunk
s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
got := fn.Chunk(s, 3)
fmt.Println(got) // [[1 2 3] [4 5 6] [7 8 9]]
  1. Shuffle
s := []int{1, 2, 3, 4, 5}
got := fn.Shuffle(s)
// Output is non-deterministic
fmt.Println(got)
  1. Partition
s := []int{1, 2, 3, 4, 5}
is_even := func(v int) bool { return v%2 == 0 }
even, odd := fn.Partition(s, is_even)
fmt.Println(got) // [2 4], [1 3 5]
  1. Flatten
s := [][]int{{1, 2}, {3, 4}, {5, 6}}
got := fn.Flatten(s)
fmt.Println(got) // [1 2 3 4 5 6]
  1. Reverse
s := []int{1, 2, 3, 4, 5}
fn.Reverse(s)
fmt.Println(s) // [5 4 3 2 1]
  1. Take
s := []int{1, 2, 3, 4, 5}
got := fn.Take(s, 3)
fmt.Println(got) // [1 2 3]
  1. TakeWhile
s := []int{1, 2, 3, 4, 5}
is_less_than_4 := func(v int) bool { return v < 4 }
got := fn.TakeWhile(s, is_less_than_4)
fmt.Println(got) // [1 2 3]
  1. Drop
s := []int{1, 2, 3, 4, 5}
got := fn.Drop(s, 3)
fmt.Println(got) // [4 5]
  1. Count
s := []int{1, 2, 3, 4, 5}
is_even := func(v int) bool { return v%2 == 0 }
got := fn.Count(s, is_even)
fmt.Println(got) // 2
  1. All
s := []int{1, 2, 3, 4, 5}
is_less_than_10 := func(v int) bool { return v < 10 }
got := fn.All(s, is_less_than_10)
fmt.Println(got) // true
  1. Any
s := []int{1, 2, 3, 4, 5}
is_even := func(v int) bool { return v%2 == 0 }
got := fn.Any(s, is_even)
fmt.Println(got) // true
  1. Zip
s1 := []int{1, 2, 3}
s2 := []string{"a", "b", "c"}
f := func(a int, b string) string { return fmt.Sprintf("%d%s", a, b) }
got := fn.Zip(s1, s2, f)
fmt.Println(got) // [1a 2b 3c]
  1. ZipShortest
s1 := []int{1, 2, 3}
s2 := []string{"a", "b"}
f := func(a int, b string) string { return fmt.Sprintf("%d%s", a, b) }
got := fn.ZipShortest(s1, s2, f)
fmt.Println(got) // [1a 2b]
  1. ZipWithIndex
s := []string{"a", "b", "c"}

f := func(v string, i int) string { return fmt.Sprintf("%d%s", i, v) }
got := fn.ZipWithIndex(s, f)
fmt.Println(got) // [0a 1b 2c]
  1. RotateLeft
s := []int{1, 2, 3, 4, 5}
got := fn.RotateLeft(s, 2)
fmt.Println(got) // [3 4 5 1 2]
  1. RotateRight
s := []int{1, 2, 3, 4, 5}
got := fn.RotateRight(s, 2)
fmt.Println(got) // [4 5 1 2 3]

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](s []T, fn func(T) bool) bool

All returns true if all elements in s satisfy the predicate fn.

func Any

func Any[T any](s []T, fn func(T) bool) bool

Any returns true if at least one element in s satisfies the predicate fn.

func Chunk

func Chunk[T any](s []T, chunkSize int) [][]T

Chunk returns a new slice containing slices of size chunkSize. The last slice may have fewer than chunkSize elements.

func Concat

func Concat[T any](s1, s2 []T) []T

Concat returns a new slice containing all the elements of s1 followed by all the elements of s2.

func Count

func Count[T any](s []T, fn func(T) bool) int

Count returns the number of elements in s that satisfy the predicate fn.

func Distinct

func Distinct[T comparable](s []T) []T

Distinct returns a new slice containing only the unique elements of s.

func DistinctFunc

func DistinctFunc[T any, U comparable](s []T, fn func(T) U) []T

DistinctFunc returns a new slice containing only the unique elements of s. The function fn is used to determine the uniqueness of each element. The function fn should return a value that can be used as a key in a map.

func Drop

func Drop[T any](s []T, n int) []T

Drop returns a new slice containing all but the first n elements of s. This is the opposite of Take. If n is greater than the length of s, Drop returns nil.

func Filter

func Filter[T any](s []T, fn func(T) bool) []T

Filter returns a new slice containing only the elements that satisfy the predicate fn.

func Flatten

func Flatten[T any](s [][]T) []T

Flatten returns a new slice containing all the elements of the sub-slices in s.

func ForEach added in v1.0.1

func ForEach[T any](s []T, fn func(T))

ForEach applies the function fn to each element of s.

func IndexOf

func IndexOf[T comparable](s []T, elem T) int

IndexOf returns the index of the first occurrence of elem in s. If elem is not in s, IndexOf returns -1.

func Map

func Map[T, U any](s []T, fn func(T) U) []U

Map returns a new slice containing the results of applying the function fn to each element of the original slice.

func Partition

func Partition[T any](s []T, fn func(T) bool) (yes, no []T)

Partition returns two slices, the first containing the elements of s that satisfy the predicate fn, and the second containing the rest.

func Reduce

func Reduce[T, U any](s []T, fn func(U, T) U, initial U) U

Reduce applies the function fn to each element of the slice, accumulating the result. The accumulated value is initialized to initial.

func Reverse

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

Reverse reverses the elements of s in place.

func RotateLeft

func RotateLeft[T any](s []T, positions int)

RotateLeft rotates the elements of s to the left by positions. It does nothing if s has fewer than 2 elements. This is useful for working with circular buffers.

func RotateRight

func RotateRight[T any](s []T, positions int)

RotateRight rotates the elements of s to the right by positions. It does nothing if s has fewer than 2 elements. This is useful for working with circular buffers.

func Shuffle

func Shuffle[T any](s []T)

Shuffle randomizes the order of elements in s.

func Take

func Take[T any](s []T, n int) []T

Take returns the first n elements of s. If n is greater than the length of s, Take returns s unchanged.

func TakeWhile

func TakeWhile[T any](s []T, fn func(T) bool) []T

TakeWhile returns a new slice containing the elements that satisfy the predicate fn. The predicate is evaluated until the first element that does not satisfy the predicate.

func Zip

func Zip[T, U, V any](s1 []T, s2 []U, fn func(T, U) V) []V

Zip returns a new slice containing the result of applying the function fn to the elements of s1 and s2. The function fn should take two arguments, one from each slice, and return a value. s1 and s2 must have the same length.

func ZipShortest

func ZipShortest[T, U, V any](s1 []T, s2 []U, fn func(T, U) V) []V

ZipLongest returns a new slice containing the result of applying the function fn to the elements of s1 and s2. The function fn should take two arguments, one from each slice, and return a value. If the slices have different lengths, the result will have the length of the shorter slice.

func ZipWithIndex

func ZipWithIndex[T, U any](s []T, fn func(int, T) U) []U

ZipWithIndex returns a new slice containing the result of applying the function fn to the elements of s and their index. The function fn should take two arguments, the index and the element, and return a value.

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