Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sergio16t/funky-go
Functional Programming Package for Go Development.
https://github.com/sergio16t/funky-go
functional-programming go golang utilities
Last synced: 4 months ago
JSON representation
Functional Programming Package for Go Development.
- Host: GitHub
- URL: https://github.com/sergio16t/funky-go
- Owner: Sergio16T
- License: mit
- Created: 2022-09-16T22:38:54.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-10T19:59:45.000Z (7 months ago)
- Last Synced: 2024-10-22T11:05:07.743Z (4 months ago)
- Topics: functional-programming, go, golang, utilities
- Language: Go
- Homepage: https://pkg.go.dev/github.com/Sergio16T/funky_go
- Size: 33.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
## Summary
Funky Go includes Higher Order Functions in addition to other
Declarative utility Functions. The utils package provides utilities
for working with slices & arrays in Go.## Installation
```
go get github.com/Sergio16T/[email protected]
```
## Table of Contents- [Examples](#examples)
## Examples
Reduce the given source array to a new array with duplicates removed
```go
sourceArray := []int{1, 1, 2, 3, 4, 5, 4}
var initialValue []intreduced := utils.Reduce(sourceArray, func(previousValue []int, element int) []int {
if !utils.Contains(previousValue, element) {
previousValue = append(previousValue, element)
}
return previousValue
}, initialValue)// reduced ~ [1, 2, 3, 4, 5]
```Filter list of Disney characters to characters 30 or younger
```go
type TestPerson struct {
name string
age int
}sourceArray := []TestPerson{{name: "Mickey", age: 30}, {name: "Minnie", age: 27}, {name: "Goofy", age: 22}, {name: "Donald", age: 32}}
filtered := utils.Filter(sourceArray, func(person TestPerson, index int) bool {
return person.age <= 30
})// filtered ~ []TestPerson{{name: "Mickey", age: 30}, {name: "Minnie", age: 27}, {name: "Goofy", age: 22}}
```
ForEach person in the list add 5 years to their age
```go
type TestPerson struct {
name string
age int
}sampleList := []TestPerson{{name: "Mickey", age: 30}, {name: "Minnie", age: 27}, {name: "Goofy", age: 22}, {name: "Donald", age: 32}}
utils.ForEach(sampleList, func(person TestPerson, index int) {
sampleList[index].age = person.age + 5
})// sampleList ~ []TestPerson{{name: "Mickey", age: 35}, {name: "Minnie", age: 32}, {name: "Goofy", age: 27}, {name: "Donald", age: 37}}
```
Map over each person in the list and return a new list containing the ages of all the characters + 5
```go
type TestPerson struct {
name string
age int
}sampleList := []TestPerson{{name: "Mickey", age: 30}, {name: "Minnie", age: 27}, {name: "Goofy", age: 22}, {name: "Donald", age: 32}}
mapped := utils.Map(sampleList, func(person TestPerson, index int) int {
agePlusFive := person.age + 5
return agePlusFive
})// mapped ~ []int{35, 32, 27, 37}
```Find the first value that passes the given predicate.
Returns a pointer and the index of value in the given array
(Nil pointer, index -1 if not found).
```go
sampleList := []int{1, 2, 3, 4, 5}found, i := utils.Find(sampleList, func(num int, index int) bool {
return num == 2
})// found ~ *int((*int)(0xc00001aba8))
// i ~ 1
if i == -1 {
log.Printf("No match was found")
} else {
log.Printf("Match %+v found at index %+v\n", *found, i)
}
```FindIndex of the first value in the array that passes the given predicate
```go
sampleList := []int{1, 2, 3, 4, 5}index := utils.FindIndex(sampleList, func(num int, index int) bool {
return num == 2
})// index ~ 1
```Find Index Of Element in Source Array
```go
sampleList := []int{1, 2, 3, 4, 11, 5, 1, 2, 3, 2, 1, 0, 9}index := utils.IndexOf(sampleList, 11)
// index ~ 4
```