gamda

package module
Version: v0.9.0 Latest Latest
Warning

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

Go to latest
Published: May 4, 2022 License: MIT Imports: 4 Imported by: 0

README

gamda

Pratical functional programming library in go.

Go Reference Go Report Card

Installation

To install Testify, use go get:

go get github.com/jonatan5524/gamda

Staying up to date

To update Testify to the latest version, use go get -u github.com/jonatan5524/gamda.


Contributing

Thanks for contributing!

make sure to read our contributing guidelines

Acknowledgements

This project is inspired by ramda library


License

This project is licensed under the terms of the MIT license.

Documentation

Overview

Package gamda provides functions to implement functional programming in go Inspired by Ramdajs

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add added in v0.8.0

func Add[T math.Number](a T, b T) T

Add function adds 2 numbers, can be integer of float.

Example
result := Add(2, 3)

fmt.Println(result)
Output:

5

func Adjust added in v0.5.0

func Adjust[T any](changeIndex int, changeElem slice.ChangeElemFunc[T, T], arr []T) []T

Adjust function receives an index, change function and a slice and returns a new slice with the changed element. The index is the element position you want to change. If the index is negative the position is relative to the end, if the index is out of bounds the function returns the original slice The changeElemFunc function receives an element and returns new element The slice type needs to be the same type of the changeElem parameter

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

increaseElem := func(elem int) int {
	return elem + 1
}

newArr := Adjust(1, increaseElem, arr)

fmt.Println(newArr)
Output:

[1 3 3 4]

func All added in v0.6.0

func All[T any](matchBy slice.MatchElemFunc[T], arr []T) bool

All function receives a matchBy function and a slice and returns true if all of the elements matches the matchBy. The matchBy function receives an element and returns boolean The slice type needs to be the same type of the matchBy parameter

Example
arr := []int{2, 4, 6, 8}

evenElem := func(elem int) bool {
	return elem%2 == 0
}

mappedArr := All(evenElem, arr)

fmt.Println(mappedArr)
Output:

true

func AllPass added in v0.8.0

func AllPass[T any](checks []logic.CheckPass[T]) func(...T) bool

AllPass function receives list of check functions and returns a function. The checks function receive an element and return boolean The function return a function that receive one element, or many and the function iterate over the elements and for each element check if it passes all the check functions

Example
checkOdd := func(x int) bool { return x%2 != 0 }
checkGreaterThanFive := func(x int) bool { return x > 5 }
checkLessThanTwenty := func(x int) bool { return x < 20 }

checkAllPass := AllPass([]logic.CheckPass[int]{checkOdd, checkGreaterThanFive, checkLessThanTwenty})

fmt.Println(checkAllPass(7, 9, 11))
Output:

true

func Always added in v0.9.0

func Always[T any](val T) func() T

Always recieves a value and returns a function that always return that value

Example
ten := Always(10)

fmt.Println(ten())
Output:

10

func And added in v0.9.0

func And(a bool, b bool) bool

And function recevie 2 boolean arguments and return the && output of those 2 values

Example
ret := And(true, false)
ret2 := And(true, true)

fmt.Println(ret, ret2)
Output:

false true

func Any added in v0.6.0

func Any[T any](matchBy slice.MatchElemFunc[T], arr []T) bool

Any function receives a matchBy function and a slice and returns true if one of the elements matches the matchBy. The matchBy function receives an element and returns boolean The slice type needs to be the same type of the matchBy parameter

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

evenElem := func(elem int) bool {
	return elem%2 == 0
}

mappedArr := Any(evenElem, arr)

fmt.Println(mappedArr)
Output:

true

func AnyPass added in v0.9.0

func AnyPass[T any](checks []logic.CheckPass[T]) func(...T) bool

AnyPass function receives list of check functions and returns a function. The checks function receive an element and return boolean The function return a function that receive one element, or many and the function iterate over the elements and for each element check if it passes one of the check functions

Example
checkOdd := func(x int) bool { return x%2 != 0 }
checkLessThanTwenty := func(x int) bool { return x < 20 }

checkAnyPass := AnyPass([]logic.CheckPass[int]{checkOdd, checkLessThanTwenty})

fmt.Println(checkAnyPass(4, 20, 22))
Output:

true

func Ap added in v0.9.0

func Ap[T any](funcs []func(x T) T, arr []T) []T

Ap recieves a ist of functions and a slice and return a new slice each function applies on each element and added to a new slice the returns

Example
arr := []int{1, 2, 3}
multiply := func(x int) int { return x * 2 }
plus := func(x int) int { return x + 3 }

fmt.Println(Ap([]func(int) int{multiply, plus}, arr))
Output:

[2 4 6 4 5 6]

func Aperture added in v0.9.0

func Aperture[T any](tuple_len int, arr []T) [][]T

Aperture function receives tuple size and a slice, returns new slice composed of n-tuples with tuple size

Example
arr := []int{1, 2, 3, 4, 5, 6, 7}

fmt.Println(Aperture(3, arr))
Output:

[[1 2 3] [2 3 4] [3 4 5] [4 5 6] [5 6 7]]

func Append added in v0.9.0

func Append[T any](element T, arr []T) []T

Append Funcion receives an element and a slice, append to the end of the slice an element

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

fmt.Println(Append(4, arr))
Output:

[1 2 3 4]

func Concat added in v0.7.0

func Concat[T any](arr []T, secondArr []T) []T

Concat function receives 2 slices and combine them together, the function returns the combined slice

Example
arr := []int{1, 2, 3}
secondArr := []int{3, 5, 6}

combinedArr := Concat(arr, secondArr)

fmt.Println(combinedArr)
Output:

[1 2 3 3 5 6]

func Filter

func Filter[T any](filterBy slice.MatchElemFunc[T], arr []T) []T

Filter function receives a filterBy function and a slice and returns filtered slice by filterBy. The filterBy function receives an element and returns boolean The slice type needs to be the same type of the filterBy parameter

Example
arr := []int{1, 2, 3, 4, 5, 6}

filterByEven := func(elem int) bool {
	return elem%2 == 0
}

filteredArr := Filter(filterByEven, arr)

fmt.Println(filteredArr)
Output:

[2 4 6]

func Find added in v0.5.0

func Find[T any](findBy slice.MatchElemFunc[T], arr []T) T

Find function receives a findBy function and a slice and returns the matching element. The findBy function receives an element and returns boolean The slice type needs to be the same type of the findBy parameter If the element is not exists the function returns the 'zero' value of the generic type, for example int is 0, string is "" etc.

Example
type twoNums struct {
	numA int
	numB int
}

arr := []twoNums{
	{
		numA: 1,
		numB: 2,
	},
	{
		numA: 3,
		numB: 4,
	},
	{
		numA: 5,
		numB: 6,
	},
}

numBFour := func(elem twoNums) bool {
	return elem.numB == 4
}

objFind := Find(numBFour, arr)

fmt.Println(objFind)
Output:

{3 4}

func Foreach added in v0.6.0

func Foreach[T any](action func(T), arr []T) []T

Foreach function receives a function and a slice and interate over the elements and preform the function over them, the function returns the original slice. The action function receives a single element The slice type needs to be the same type of the action parameter

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

newArr := []int{}
doubleSlice := func(elem int) {
	newArr = append(newArr, elem*2)
}

Foreach(doubleSlice, arr)

fmt.Println(newArr)
Output:

[2 4 6 8]

func Map

func Map[T any, S any](mapBy slice.ChangeElemFunc[T, S], arr []T) []S

Map function receives a mapBy function and a slice and returns the mapped slice by mapBy. The mapBy function receives an element and returns a new element by mapBy The slice type needs to be the same type of the mapBy parameter

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

doubleElems := func(elem int) int {
	return elem * 2
}

mappedArr := Map(doubleElems, arr)

fmt.Println(mappedArr)
Output:

[2 4 6 8]

Types

This section is empty.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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