fn

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2022 License: MIT Imports: 1 Imported by: 0

README

test codecov

fn

fn is a collection of go functional operators with generics

Getting Started

Prerequisites

Go 1.18

go install golang.org/dl/go1.181@latest
go1.18 download
Installation
go get github.com/birwin93/fn

Usage

import "github.com/birwin93/fn"

func main() {
  nums := []int{1, 2, 3}
  
  sum := fn.Sum(nums)
  
  filteredNums := fn.Filter(nums, func(i int) bool {
    return i > 1
  })
  
  doubleNums := fn.Map(nums, func(i int) int {
    return i * 2
  })
  
  if fn.Contains(nums, 2) {
    fmt.Println("nums contains 2!")
  }
}

Supported Functions

Arrays / Slices
Maps

See the open issues for a full list of proposed features (and known issues).

Contributing

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/newfunction)
  3. Commit your Changes (git commit -m 'Adds fn.NewFunction')
  4. Push to the Branch (git push origin feature/newfunction)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE for more information.

Contact

Twitter - @billy_the_kid Project Link: https://github.com/birwin93/fn

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[T comparable](arr []T, lookup T) bool

Checks if lookup exists in arr

Example
package main

import (
	"fmt"

	"github.com/birwin93/fn"
)

func main() {
	fmt.Println(fn.Contains([]int{1, 2, 3}, 1))
}
Output:

true

func Dedupe

func Dedupe[T comparable](arr []T) []T

Removes duplicates from array

arr := []int{1, 2, 3, 3}
arr = fn.Dedupe(arr) // outputs [1, 2, 3]
Example
package main

import (
	"fmt"

	"github.com/birwin93/fn"
)

func main() {
	arr := []int{1, 2, 3, 4, 1, 3}
	fmt.Println(fn.Dedupe(arr))
}
Output:

[1 2 3 4]

func Equal

func Equal[T comparable](a []T, b []T) bool

Checks if both slices have the same elements in the same order

Example
package main

import (
	"fmt"

	"github.com/birwin93/fn"
)

func main() {
	a := []int{1, 2, 3}
	b := []int{1, 2, 3}
	fmt.Println(fn.Equal(a, b))
}
Output:

true

func Filter

func Filter[T any](arr []T, predicate func(T) bool) []T

Removes items from array that does not pass predicate

Example
package main

import (
	"fmt"

	"github.com/birwin93/fn"
)

func main() {
	arr := []int{1, 2, 3}
	filteredArr := fn.Filter(arr, func(i int) bool {
		return i > 1
	})
	fmt.Println(filteredArr)
}
Output:

[2 3]

func FlatMap

func FlatMap[T any, N any](arr []T, f func(T) []N) []N

Maps items to an array of another type and then flattens into one array

Example
package main

import (
	"fmt"
	"strconv"

	"github.com/birwin93/fn"
)

func main() {
	arr := []int{1, 2, 3}
	mappedArr := fn.FlatMap(arr, func(i int) []string {
		return []string{strconv.Itoa(i), strconv.Itoa(i * 2)}
	})
	fmt.Println(mappedArr)
}
Output:

[1 2 2 4 3 6]

func GetOr

func GetOr[K comparable, V any](dict map[K]V, key K, fallback V) V

Gets the value for the key in the map, or returns provided fallback

Example
package main

import (
	"fmt"

	"github.com/birwin93/fn"
)

func main() {
	dict := map[int]string{
		1: "1",
		2: "2",
	}
	fmt.Println(fn.GetOr(dict, 3, "3"))
}
Output:

3

func Group

func Group[T any, N comparable](arr []T, f func(T) N) map[N][]T
Example
package main

import (
	"fmt"

	"github.com/birwin93/fn"
)

func main() {
	type member struct {
		name string
		age  int
	}

	billy := member{name: "Billy", age: 1}
	bob := member{name: "Bob", age: 1}
	chris := member{name: "Chris", age: 2}
	members := []member{billy, bob, chris}

	groupedMembers := fn.Group(members, func(m member) string {
		return string(m.name[0])
	})
	fmt.Println(groupedMembers)
}
Output:

map[B:[{Billy 1} {Bob 1}] C:[{Chris 2}]]

func Intersect

func Intersect[T comparable](arr1 []T, arr2 []T) []T

Returns shared items between two arrays

func Last

func Last[T any](arr []T) (T, bool)

Grabs last item in array Will return empty value and false if array is empty

func Map

func Map[T any, N any](arr []T, f func(T) N) []N

Converts all items in array to specific output of func

func Reduce

func Reduce[T any, N any](arr []T, initial N, f func(N, T) N) N

Consolidates all contents in array into custom specific object

func Sum

func Sum[T Num](arr []T) T

Adds up all the contents of array

Example
package main

import (
	"fmt"

	"github.com/birwin93/fn"
)

func main() {
	arr := []int{1, 2, 3}
	fmt.Println(fn.Sum(arr))
}
Output:

6

Types

type EmptyArrayError

type EmptyArrayError struct{}

Error returned when an operator requires a non empty array

func (*EmptyArrayError) Error

func (e *EmptyArrayError) Error() string

EmptyArrayError conformance to error

type Num

Jump to

Keyboard shortcuts

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