lists

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Example (Using_lists)

This an example of using a List

emptyList := Of[string]()

wordList := Of("hello", "world")

newList := emptyList.Append("a value")

completeList := newList.Concat(wordList)

fmt.Printf("total count of elements: %d", completeList.Count())

isLongWord := func(s string) bool { return len(s) > 4 }

longWords := completeList.FilterBy(isLongWord)

longWordCount1 := completeList.FilterBy(isLongWord).Count()
longWordCount2 := completeList.CountBy(isLongWord)

fmt.Printf("Long words: %v or %d or %d", longWords, longWordCount1, longWordCount2)

longWordsInUppercaseJoined := completeList.FilterBy(isLongWord).Map(strings.ToUpper).Join(",")

fmt.Println("Long words in uppercase and joined", longWordsInUppercaseJoined)
Output:

Example (Using_map_to_different_type)

Shows how to convert list to a different element type

words := Of("one", "ring", "to", "rule", "them", "all")

wordSize := func(word string) int { return len(word) }

sizes := Map(words, wordSize)

addInts := func(sum, e int) int { return sum + e }

sumAllSizes := sizes.Reduce(addInts)

fmt.Println("Sum is", sumAllSizes)

oddNumber := func(n int) bool { return n%2 != 0 }

sumWordLengthHavingOddLetterCount := Map(words, wordSize).FilterBy(oddNumber).Reduce(addInts)

fmt.Println("Sum is", sumWordLengthHavingOddLetterCount)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fold added in v0.3.0

func Fold[S any, T any](src List[S], initial T, reducer func(accum T, element S) T) T

Fold convert this list in a single value of the same type This is implemented as a method in List[T] because GO generics does not support (yet) the use of type parameters in methods signatures.

func Generate added in v0.2.0

func Generate[T any](times int, generator func(i int) T) []T

Generate use provided generator to generate array of N elements

func Reduce added in v0.2.0

func Reduce[S any, T any](src List[S], reducer func(accum T, element S) T) T

Reduce convert this list in a single value of the same type. Starts with the zero value of the type This is implemented as a method in List[T] because GO generics does not support (yet) the use of type parameters in methods signatures.

Types

type List added in v0.3.0

type List[T any] interface {
	// Values returns a slice with the items of this list
	Values() []T
	// Append returns a list with all the elements of this and a new one to the end of the list
	Append(T) List[T]
	// AppendAll returns a list with all the elements of this and a all new ones to the end of the list
	AppendAll(...T) List[T]
	// Concat returns a list with the elements of this followed by all the elements of second list
	Concat(List[T]) List[T]
	// Count returns number of elements in list
	Count() int
	// CountBy returns number of elements in list that satisfies predicate
	CountBy(predicate types.Predicate[T]) int
	// At2 returns element at idx, or report empty element if idx <0 or >= List.Count
	At2(idx int) (T, bool)
	// At returns element at idx, or empty if idx <0 or >= List.Count
	At(idx int) T
	// Map converts a list of one type to another list of same type with possible different values
	Map(mapper func(T) T) List[T]
	// Reduce convert this list in a single value of the same type. Starts with the zero value of the type
	Reduce(reducer func(accum T, element T) T) T
	// Fold convert this list in a single value of the same type
	Fold(initial T, reducer func(accum T, element T) T) T

	// FilterBy returns list with all elements except the ones that satisfies predicate
	FilterBy(types.Predicate[T]) List[T]

	// Any returns true if at least one element satisfies predicate
	Any(types.Predicate[T]) bool
	// All returns true if all the elements satisfies predicate
	All(types.Predicate[T]) bool

	// Index returns the first position of value in list or -1
	Index(T) int

	// Index2 returns the first position of value or reports not found
	Index2(T) (int, bool)

	// IndexBy returns the first position of value that satisfies predicate or -1 if not found
	IndexBy(types.Predicate[T]) int

	// IndexBy2 returns the first position of value that satisfies predicate
	IndexBy2(types.Predicate[T]) (int, bool)
	// Join makes all elements a single string separting values by separator
	Join(separator string) string
}

func Empty added in v0.3.0

func Empty[T any]() List[T]

func Map

func Map[S any, T any](src List[S], m types.Mapper[S, T]) List[T]

Map computes a new list with the result of applying function mapper to the source list This is implemented as a method in List[T] because GO generics does not support (yet) the use of type parameters in methods signatures.

func Of

func Of[T any](e ...T) List[T]

Jump to

Keyboard shortcuts

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