funky_go

module
v0.1.8-beta Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2022 License: MIT

README

Funky Go

A Functional Programming package for Go development

Go Reference 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/funky_go@v0.1.7-beta

Table of Contents

Examples

Reduce the given source array to a new array with duplicates removed

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

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

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

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).

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

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

sampleList := []int{1, 2, 3, 4, 11, 5, 1, 2, 3, 2, 1, 0, 9}

index := utils.IndexOf(sampleList, 11)
// index ~ 4

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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