functionality

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: Unlicense Imports: 0 Imported by: 0

README

Functionality

Go Report Card Go Reference

Introduction

Well generics in Go are here and I thought it will be really cool if we could apply the functional programming aspects like map, filter and reduce to the slices in our Go apps.

Functionality lets you stop writing those old, boring for loops to do operations on slices

Stop talking and tell me how it works

Let me show you how 😉

Lets say you want to filter all even numbers from 1 to 10, square the result and find the sum

The old way to do that is

func Example() {
	numbers := []int{1,2,3,4,5,6,7,8,9,10}
	
	even := make([]int, 0, 0)
	for _, num := range numbers {
		if num % 2 == 0 {
			even = append(even, num)
		}
	}
	
	for idx, num := range even {
		even[idx] = num * num
	}
	
	sum := 0

	for _, num := range even {
		sum += num
	}
}

This is how it will look when you use Functionality

func Functionality() {
	numbers := NewIterator[int]() // Create a new iterator
	
	for i := 1; i <= 10; i++ {
		numbers = append(numbers, i) // Seed some data
	}
	
	sum := numbers.Filter(func(num int) bool {
		return num % 2 == 0
	}).Map(func(i int) int {
		return i * i
	}).Reduce(func(accumulator, element int) int {
		return accumulator + element
	}, 0)
	
	fmt.Println("SUM IS ", sum)
}

Clean ain't it :D. Well the possibilities are endless and please get creative

Looks cool, do you use reflection?

Nope, we use generics, and you would need to specify the type when you define the iterator

What is next?

Well next step is to create some set related operations like, union, intersection and some other methods

Looks cool, how can I get involved

Simple, fork the repo, create a change and submit a PR. If you find a bug, please use the issues tab to submit bugs or feature requests

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FilterFunc

type FilterFunc[T any] func(T) bool

FilterFunc is a custom func that will be applied to items on an iterator

type Iterator

type Iterator[T any] []T

Iterator is the central backbone that used within this project

func NewIterator

func NewIterator[T any]() Iterator[T]

NewIterator creates a new iterator without any len or cap

func NewIteratorFromExistingSlice added in v0.1.2

func NewIteratorFromExistingSlice[T any](collection []T) Iterator[T]

func NewIteratorWithCapacity

func NewIteratorWithCapacity[T any](cap uint) Iterator[T]

NewIteratorWithCapacity Creates a new iterator with capacity of type T

func NewIteratorWithLength

func NewIteratorWithLength[T any](len uint) Iterator[T]

NewIteratorWithLength Creates a new iterator with length of type T with 0 value types of type T

func (Iterator[T]) Collect

func (iter Iterator[T]) Collect() []T

Collect method is used to return the slice of type T is the users do not want an iterator type

func (Iterator[T]) Filter

func (iter Iterator[T]) Filter(filterFunc FilterFunc[T]) Iterator[T]

Filter is a receiver method on the Iterator that takes in a FilterFunc and applies on the slice elements and returns a new Iterator with the filtered elements

func (Iterator[T]) Map

func (iter Iterator[T]) Map(mapFunc MapFunc[T]) Iterator[T]

Map is a receiver method on the Iterator that takes in a MapFunc and applies on the slice elements and returns a new Iterator with the filtered elements

func (Iterator[T]) Reduce

func (iter Iterator[T]) Reduce(reducerFunc ReducerFunc[T], initialValue T) T

Reduce is a receiver method on the Iterator that takes in a ReducerFunc and applies on the slice elements and returns a reduced value of type T

type MapFunc

type MapFunc[T any] func(T) T

MapFunc is a custom func that will be applied to items on an iterator

type ReducerFunc

type ReducerFunc[T any] func(accumulator, element T) T

ReducerFunc is a custom func that will be applied to items on an iterator

Jump to

Keyboard shortcuts

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