fslice

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: May 11, 2023 License: MIT Imports: 1 Imported by: 1

Documentation

Overview

Package fslice implements a set of methods for operating on slices in a Functional Programming way.

Example
fib := generateFibonacci(25)

// of the first 25 fibonacci numbers, which are not primes?
fibNonPrimes := fslice.From(fib).Filter(func(n int, _ int, _ []int) bool {
	r := false
	if n > 1 {
		r = !primes.IsPrime(n)
	}
	return r
})

// among those, which are co-prime with all others?
coPrimes := fibNonPrimes.Filter(func(n int, i int, nonPrimes []int) bool {

	return fslice.From(nonPrimes).Every(func(m int, j int, _ []int) bool {
		return (i == j) || primes.Coprime(n, m)
	})

})

// Fslice is generic, accepting any comparable type
lowerBase := []string{"all", "your", "base", "are", "belong", "to", "us"}
shoutCaseEveryOther := func(word string, i int, _ []string) string {
	if i%2 == 1 {
		word = strings.ToUpper(word)
	}
	return word
}
shoutyBase := fslice.From(lowerBase).Map(shoutCaseEveryOther)

fmt.Println(fibNonPrimes)
fmt.Println(coPrimes)
fmt.Println(shoutyBase)
Output:

[8 21 34 55 144 377 610 987 2584 4181 6765 10946 17711 46368]
[4181 17711]
[all YOUR base ARE belong TO us]

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EveryFunction

type EveryFunction[T comparable] func(v T, i int, arr []T) bool

EveryFunction operates on an element of a slice and returns true or false

type FilterFunction

type FilterFunction[T any] func(v T, i int, arr []T) bool

type Fslice

type Fslice[T comparable] []T

Fslice is a slice with helpful methods in the Functional Programming style. It is generic, accepting any comparable type

func (Fslice[T]) Every

func (fs Fslice[T]) Every(fn EveryFunction[T]) bool

Every returns true if its EveryFunction returns true for every element

Example
package main

import (
	"fmt"

	"github.com/sean9999/GoFunctional/fslice"
)

func main() {

	//	is every element of this slice even?
	inputSlice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
	isEven := func(v int, _ int, _ []int) bool {
		return (v%2 == 0)
	}
	answer := fslice.From(inputSlice).Every(isEven)

	fmt.Println(answer)
}
Output:

false

func (Fslice[T]) Filter

func (fs Fslice[T]) Filter(fn FilterFunction[T]) Fslice[T]

Filter takes a function that operates on every element of the slice and returns only those elements of the slice for which the function returned true

Example
package main

import (
	"fmt"

	"strings"

	"github.com/sean9999/GoFunctional/fslice"
)

func main() {

	//	only give me the even numbers
	numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
	onlyEven := func(n int, i int, arr []int) bool {
		return (n%2 == 0)
	}
	filteredNumbers := fslice.From(numbers).Filter(onlyEven).ToSlice()

	// remove SHOUTCASE words
	inputSlice := []string{"all", "your", "BASE", "are", "belong", "to", "US"}
	noShouting := func(word string, _ int, _ []string) bool {
		upperCaseWord := strings.ToUpper(word)
		return (word != upperCaseWord)
	}
	wordsWithoutShouting := fslice.From(inputSlice).Filter(noShouting).ToSlice()

	fmt.Println(filteredNumbers)
	fmt.Println(wordsWithoutShouting)
}
Output:

[2 4 6 8]
[all your are belong to]

func (Fslice[T]) Includes

func (fs Fslice[T]) Includes(x T) bool

func (Fslice[T]) Map

func (fs Fslice[T]) Map(fn MapFunction[T]) Fslice[T]

Map takes a MapFunction and returns a new Slice with the same number of elements, those elements being of the same type

Example
package main

import (
	"fmt"
	"strings"

	"github.com/sean9999/GoFunctional/fslice"
)

func main() {

	// square the numbers
	squared := func(n int, _ int, _ []int) int {
		return n * n
	}
	squares := fslice.From([]int{1, 2, 3, 4, 5}).Map(squared).ToSlice()

	// convert every other word to SHOUTCASE
	lowerBase := []string{"all", "your", "base", "are", "belong", "to", "us"}
	shoutCaseEveryOther := func(word string, i int, _ []string) string {
		if i%2 == 1 {
			word = strings.ToUpper(word)
		}
		return word
	}
	shoutyBase := fslice.From(lowerBase).Map(shoutCaseEveryOther).ToSlice()

	//	have you ever wondered

	fmt.Println(squares)
	fmt.Println(shoutyBase)

}
Output:

[1 4 9 16 25]
[all YOUR base ARE belong TO us]

func (Fslice[T]) Reduce

func (fs Fslice[T]) Reduce(fn ReduceFunction[T], seed T) T

Reduce takes a ReduceFunction and a seed, which is the initial value fed into the user-supplied ReduceFunction

// BUG(sean9999): Seed value should be optional and should not be strictly typed
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce#edge_cases

func (Fslice[T]) Some

func (fs Fslice[T]) Some(fn SomeFunction[T]) bool

Some takes a function that operates on as many elements of the slice as it takes to get a return value of true. It returns true in that case and false otherwise

Example
package main

import (
	"fmt"

	"github.com/sean9999/GoFunctional/fslice"
)

func main() {

	// at least one of these numbers is a multiple of 3
	inputSlice := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}
	isMultipleOf3 := func(v int, _ int, _ []int) bool {
		return (v%3 == 0 && v >= 3)
	}
	result := fslice.From(inputSlice).Some(isMultipleOf3)

	fmt.Println(result)
}
Output:

true

func (Fslice[T]) Sort

func (fs Fslice[T]) Sort(fn SortFunction[T]) Fslice[T]

Sort takes a SortFunction and returns a slice, sorted

func (Fslice[T]) ToSlice

func (fs Fslice[T]) ToSlice() []T

ToSlice returns the underlying slice

type MapFunction

type MapFunction[T comparable] func(v T, i int, arr []T) T

MapFunction operates on every element of a Slice and returns an element of the same type. It is passed into Fslice.Map

type MethodSet

type MethodSet[T comparable] interface {
	Map(MapFunction[T]) Fslice[T]
	Filter(FilterFunction[T]) Fslice[T]
	Some(SomeFunction[T]) bool
	Every(EveryFunction[T]) bool
	Includes(T) bool
	Reduce(ReduceFunction[T], T) T
	Sort(SortFunction[T]) Fslice[T]
	ToSlice() []T
}

MethodSet is the interface Fslice implements

func From

func From[T comparable](inputSlice []T) MethodSet[T]

From is a constructor. It wraps an existing slice returning an Fslice[T] satisfying MethodSet[T]

func New

func New[T comparable](size, capacity int) MethodSet[T]

New is an initializer. It pre-allocates a zeroed slice returning an Fslice satisfying MethodSet

type ReduceFunction

type ReduceFunction[T comparable] func(a T, b T) T

ReduceFunction operates on all elements, feeding the value of one iteration into the next

type SomeFunction

type SomeFunction[T any] func(v T, i int, arr []T) bool

type SortFunction

type SortFunction[T comparable] func(a, b T) int

SortFunction compares two values from the slice and returns a negative value if (a > b), else positive or zero. Note that while Javascript makes a distinction between positive and zero values, Go does not. Go only asks if (a > b), and never asks if (a == b).

see [https://pkg.go.dev/sort#IntSlice.Less]

Jump to

Keyboard shortcuts

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