Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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.

Awesome Lists containing this project

README

        


Funky Go



A Functional Programming package for Go development




Go Reference


go report A+


License


Go Pilot

## 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 []int

reduced := 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
```