talgo

package module
v0.0.0-...-247d338 Latest Latest
Warning

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

Go to latest
Published: Sep 1, 2019 License: Apache-2.0 Imports: 1 Imported by: 1 module, 2 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

talgo

talgo provides a set of algorithms (map, reduce, predicate, find...) that do not use reflection or empty interfaces

Features

  • Strong typing
  • No overhead
  • Map
  • Reduce
  • Predicate
  • Find
  • ...

Dependencies

talgo uses Testify https://github.com/stretchr/testify

Examples

package talgo_test

import (
	"sort"
	"testing"

	"github.com/SebastienDorgan/talgo"
	"github.com/stretchr/testify/assert"
)

func GreaterThan(s sort.IntSlice, value int) talgo.Predicate {
	return func(i int) bool {
		return s[i] >= value
	}
}

type BankAccount struct {
	ID      string
	Balance int64
}

type BankAccounts []BankAccount

func (b BankAccounts) Len() int {
	return len(b)
}
func Test(t *testing.T) {
	s := sort.IntSlice{1, 2, 4}
	//Map Incr over s
	talgo.ForEach(s, func(i int) {
		s[i] += 3
	})
	assert.Equal(t, 4, s[0])
	assert.Equal(t, 5, s[1])
	assert.Equal(t, 7, s[2])

	//Find first element greater or equal to  5
	r := talgo.FindFirst(s, GreaterThan(s, 5))
	assert.Equal(t, 1, r)

	//Find last element greater or equal to 5
	r = talgo.FindLast(s, func(i int) bool {
		return s[i] >= 5
	})
	assert.Equal(t, 2, r)

	//Find last element greater or equal to 5
	l := talgo.FindAll(s, GreaterThan(s, 5))
	assert.Equal(t, 1, l[0])
	assert.Equal(t, 2, l[1])

	//Checks if at least one element is greater or equal to 5
	b := talgo.Any(s, GreaterThan(s, 5))
	assert.True(t, b)

	//Checks if at least one element is greater or equal to 0
	b = talgo.Any(s, talgo.Negate(GreaterThan(s, 0)))
	assert.False(t, b)

	//Checks if all elements are greater or equal to 5
	b = talgo.All(s, GreaterThan(s, 5))
	assert.False(t, b)

	//Reduce by adding all elements of the list to 3
	sum := 3
	talgo.ForEach(s, func(i int) {
		sum += s[i]
	})
	assert.Equal(t, 19, sum)

	//Multiply by 2 all elements of the list
	sf := sort.Float64Slice{2.5, 1.5, 3.5}
	talgo.ForEach(sf, func(i int) {
		sf[i] *= 2.
	})
	assert.Equal(t, 5., sf[0])
	assert.Equal(t, 3., sf[1])
	assert.Equal(t, 7., sf[2])

	//Find index of the min value
	min := func(i, j int) int {
		if sf[i] >= sf[j] {
			return j
		} else {
			return i
		}
	}

	minargs := talgo.Select(sf, min)

	assert.Equal(t, 1, minargs)

	//Find index of the max value
	max := func(i, j int) int {
		if sf[i] <= sf[j] {
			return j
		} else {
			return i
		}
	}

	maxargs := talgo.Select(sf, max)

	assert.Equal(t, 2, maxargs)

	//Separate odd and even elements
	ints := sort.IntSlice{1, 2, 3, 4, 5, 6, 7, 8, 9}
	even := []int{}
	odd := []int{}

	talgo.ForEach(ints, func(i int) {
		if ints[i]%2 == 0 {
			even = append(even, ints[i])
		} else {
			odd = append(odd, ints[i])
		}
	})

	//Find bank accounts in deficit
	bankAccounts := BankAccounts{
		BankAccount{ID: "0", Balance: 1500},
		BankAccount{ID: "1", Balance: -500},
		BankAccount{ID: "2", Balance: 0},
	}
	accountInDeficit := []string{}
	talgo.ForEach(bankAccounts, func(i int) {
		if bankAccounts[i].Balance < 0 {
			accountInDeficit = append(accountInDeficit, bankAccounts[i].ID)
		}
	})
	assert.Equal(t, "1", accountInDeficit[0])

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(len int, p Predicate) bool

All checks if all elements of the collection satisfy predicate p

func Any

func Any(len int, p Predicate) bool

Any checks if at least one element of the collection satisfies predicate p

func CountItems

func CountItems(len int, p Predicate) int

CountItems counts number of items that satisfies predicate p

func FindAll

func FindAll(len int, p Predicate) []int

FindAll find all the elements of a collections that satisfies predicate p

func FindFirst

func FindFirst(len int, p Predicate) int

FindFirst find the first element of a collections that satisfies predicate p

func FindLast

func FindLast(len int, p Predicate) int

FindLast find the last element of a collections that satisfies predicate p

func ForEach

func ForEach(len int, f Function)

ForEach apply m to each element of s

func IndexedForEach

func IndexedForEach(len int, index Index, f Function)

func None

func None(len int, p Predicate) bool

None checks if at none element of the collection satisfies predicate p

func ReverseForEach

func ReverseForEach(len int, f Function)

ReverseForEach apply m to each element of s in reverse order

func Select

func Select(len int, s Selector) int

Select select best element against selector s

Types

type Function

type Function func(i int)

Function defines a function that applies to the ith elements of a collection

type Index

type Index func(i int) int

Index use to reindex a collection

func ReverseIndex

func ReverseIndex(len int) Index

ReverseIndex inverse the index of a collection

type Predicate

type Predicate func(i int) bool

Predicate defines a predicate that applies to the ith elements of a collection

func And

func And(p1 Predicate, p2 Predicate) Predicate

And create a new predicate that is the 'and' of predicate p1 and p2

func EqualString

func EqualString(s []string, value string) Predicate

Equal check if s[i] is equal to value

func Float32Equal

func Float32Equal(s []float32, value float32) Predicate

Equal check if s[i] is equal to value

func Float32Greater

func Float32Greater(s []float32, value float32, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Float32Lesser

func Float32Lesser(s []float32, value float32, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Float64Equal

func Float64Equal(s []float64, value float64) Predicate

Equal check if s[i] is equal to value

func Float64Greater

func Float64Greater(s []float64, value float64, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Float64Lesser

func Float64Lesser(s []float64, value float64, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func GreaterString

func GreaterString(s []string, value string, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Int8Equal

func Int8Equal(s []int8, value int8) Predicate

Equal check if s[i] is equal to value

func Int8Greater

func Int8Greater(s []int8, value int8, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Int8Lesser

func Int8Lesser(s []int8, value int8, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Int16Equal

func Int16Equal(s []int16, value int16) Predicate

Equal check if s[i] is equal to value

func Int16Greater

func Int16Greater(s []int16, value int16, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Int16Lesser

func Int16Lesser(s []int16, value int16, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Int32Equal

func Int32Equal(s []int32, value int32) Predicate

Equal check if s[i] is equal to value

func Int32Greater

func Int32Greater(s []int32, value int32, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Int32Lesser

func Int32Lesser(s []int32, value int32, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Int64Equal

func Int64Equal(s []int64, value int64) Predicate

Equal check if s[i] is equal to value

func Int64Greater

func Int64Greater(s []int64, value int64, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Int64Lesser

func Int64Lesser(s []int64, value int64, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func IntEqual

func IntEqual(s []int, value int) Predicate

Equal check if s[i] is equal to value

func IntGreater

func IntGreater(s []int, value int, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func IntLesser

func IntLesser(s []int, value int, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func LesserString

func LesserString(s []string, value string, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Negate

func Negate(p Predicate) Predicate

Negate negates a predicate

func Or

func Or(p1 Predicate, p2 Predicate) Predicate

Or create a new predicate that is the 'or' of predicate p1 and p2

func Uint8Equal

func Uint8Equal(s []uint8, value uint8) Predicate

Equal check if s[i] is equal to value

func Uint8Greater

func Uint8Greater(s []uint8, value uint8, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Uint8Lesser

func Uint8Lesser(s []uint8, value uint8, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Uint16Equal

func Uint16Equal(s []uint16, value uint16) Predicate

Equal check if s[i] is equal to value

func Uint16Greater

func Uint16Greater(s []uint16, value uint16, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Uint16Lesser

func Uint16Lesser(s []uint16, value uint16, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Uint32Equal

func Uint32Equal(s []uint32, value uint32) Predicate

Equal check if s[i] is equal to value

func Uint32Greater

func Uint32Greater(s []uint32, value uint32, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Uint32Lesser

func Uint32Lesser(s []uint32, value uint32, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Uint64Equal

func Uint64Equal(s []uint64, value uint64) Predicate

Equal check if s[i] is equal to value

func Uint64Greater

func Uint64Greater(s []uint64, value uint64, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func Uint64Lesser

func Uint64Lesser(s []uint64, value uint64, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func UintEqual

func UintEqual(s []uint, value uint) Predicate

Equal check if s[i] is equal to value

func UintGreater

func UintGreater(s []uint, value uint, orEqual bool) Predicate

Greater check if s[i] is greater or greater or equal to value

func UintLesser

func UintLesser(s []uint, value uint, orEqual bool) Predicate

Lesser check if s[i] is lesser or lesser or equal to value

func Xor

func Xor(p1 Predicate, p2 Predicate) Predicate

Xor create a new predicate that is the 'xor' of predicate p1 and p2

type Selector

type Selector func(i, j int) int

Selector defines a selection function, a selector return i value or j value

Jump to

Keyboard shortcuts

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