enum

package
v0.37.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 3 Imported by: 53

Documentation

Overview

Package enum provides handy functional programming -style functions that operate on enumerable data types.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All[T any](items []T, f FilterFunc[T]) bool

All returns true if all f(item) calls return true.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(value int) bool { return value >= 0 }
	fmt.Println(enum.All(items, f))
}
Output:

false
Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"yo", "ho", "and", "barrel", "of", "rum"}
	f := func(value string) bool { return len(value) >= 2 }
	fmt.Println(enum.All(items, f))
}
Output:

true

func AllWithIndex

func AllWithIndex[T any](items []T, f FilterFuncWithIndex[T]) bool

AllWithIndex returns true if all f(index, item) calls return true.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(index, value int) bool { return index < 6 || value >= 0 }
	fmt.Println(enum.AllWithIndex(items, f))
}
Output:

true

func Any

func Any[T any](items []T, f FilterFunc[T]) bool

Any returns true if any f(item) call returns true.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(value int) bool { return value >= 0 }
	fmt.Println(enum.Any(items, f))
}
Output:

true
Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"yo", "ho", "and", "barrel", "of", "rum"}
	f := func(value string) bool { return len(value) >= 20 }
	fmt.Println(enum.Any(items, f))
}
Output:

false

func AnyWithIndex

func AnyWithIndex[T any](items []T, f FilterFuncWithIndex[T]) bool

AnyWithIndex returns true if any f(index, item) call returns true.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(index, value int) bool { return index < 6 || value >= 0 }
	fmt.Println(enum.AnyWithIndex(items, f))
}
Output:

true

func Average added in v0.0.27

func Average[T Number](values []T) (ret T)

Average returns the average of a slice of numbers.

Example (Float64)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	a := []float64{1, 2, 3, 4}
	fmt.Println(enum.Average(a))
}
Output:

2.5
Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	a := []int{1, 2, 3, 4}
	fmt.Println(enum.Average(a))
}
Output:

2

func ChunkEvery

func ChunkEvery[T any](items []T, n, step int) [][]T

ChunkEvery takes a slice of items and chunks them n-at-a-time with the given step size. It discards any left-over items.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{1, 2, 3, 4, 5, 6, 7}
	fmt.Println(enum.ChunkEvery(items, 2, 1))
}
Output:

[[1 2] [2 3] [3 4] [4 5] [5 6] [6 7]]

func Count

func Count[T any](items []T, f FilterFunc[T]) int

Count returns the count of items in the slice for which `f(item)` returns true.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(value int) bool { return value >= 0 }
	fmt.Println(enum.Count(items, f))
}
Output:

11

func CountWithIndex

func CountWithIndex[T any](items []T, f FilterFuncWithIndex[T]) int

CountWithIndex returns the count of items in the slice for which `f(index, item)` returns true.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(index, value int) bool { return index < 6 || value >= 0 }
	fmt.Println(enum.CountWithIndex(items, f))
}
Output:

12

func Dedup

func Dedup[T comparable](values []T) []T

Dedup returns a slice where all consecutive duplicated elements are collapsed to a single element.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 4, 4, 5, 6, 7, 7, 8, 9, 10, 10, 10}
	fmt.Println(enum.Dedup(items))
}
Output:

[0 1 2 3 4 5 6 7 8 9 10]

func Each

func Each[T any](items []T, f func(item T))

Each processes each item with the provided function.

Example (String)
package main

import (
	"fmt"
	"strings"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"a", "b", "c"}
	upcased := make([]string, 0, len(items))
	f := func(value string) { upcased = append(upcased, strings.ToUpper(value)) }
	enum.Each(items, f)
	fmt.Println(upcased)
}
Output:

[A B C]

func EachWithIndex

func EachWithIndex[T any](items []T, f func(i int, value T))

EachWithIndex iterates over a slice and calls the provided function with its index and value.

Example (String)
package main

import (
	"fmt"
	"strings"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"a", "b", "c"}
	upcased := make([]string, 0, len(items))
	f := func(index int, value string) {
		v := fmt.Sprintf("%v%v", strings.ToUpper(value), index)
		upcased = append(upcased, v)
	}
	enum.EachWithIndex(items, f)
	fmt.Println(upcased)
}
Output:

[A0 B1 C2]

func Filter

func Filter[T any](values []T, f FilterFunc[T]) []T

Filter filters a slice of values and keeps those for which f(value) returns true.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(value int) bool { return value >= 0 }
	fmt.Println(enum.Filter(items, f))
}
Output:

[0 1 2 3 4 5 6 7 8 9 10]

func FilterMap

func FilterMap[S any, T any](values []S, m func(S) T, f FilterFunc[T]) []S

FilterMap filters a slice of mapped values and keeps those for which f(value) returns true.

Example (String2int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
	"github.com/gmlewis/advent-of-code-2021/must"
)

func main() {
	items := []string{"0", "1", "2", "3", "4", "-1", "5", "6", "7", "8", "9", "10"}
	mapFunc := func(value string) int { return must.Atoi(value) }
	f := func(value int) bool { return value >= 0 }
	fmt.Println(enum.FilterMap(items, mapFunc, f))
}
Output:

[0 1 2 3 4 5 6 7 8 9 10]

func FilterWithIndex

func FilterWithIndex[T any](values []T, f FilterFuncWithIndex[T]) []T

FilterWithIndex filters a slice of values and keeps those for which f(index, value) returns true.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(index, value int) bool { return index < 5 || value >= 0 }
	fmt.Println(enum.FilterWithIndex(items, f))
}
Output:

[0 1 2 3 4 5 6 7 8 9 10]

func Find

func Find[T any](values []T, f FilterFunc[T]) (ret T, ok bool)

Find returns the first element for which f(value) returns true along with a boolean indicating a value was found.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(value int) bool { return value >= 0 }
	fmt.Println(enum.Find(items, f))
}
Output:

0 true

func FindFirst added in v0.0.31

func FindFirst[T any](values []T, f FilterFunc[T]) int

FindFirst returns the index of the first element for which f(value) returns true or -1 if none found.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(value int) bool { return value < 0 }
	fmt.Println(enum.FindFirst(items, f))
}
Output:

5

func FindLast added in v0.0.31

func FindLast[T any](values []T, f FilterFunc[T]) int

FindLast returns the index of the last element for which f(value) returns true or -1 if none found.

Example (Int)

FindLast returns the index of the last element for which f(value) returns true or -1 if none found.

package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(value int) bool { return value >= 0 }
	fmt.Println(enum.FindLast(items, f))
}
Output:

11

func FindOr

func FindOr[T any](values []T, f FilterFunc[T], defValue T) T

FindOr returns the first element for which f(value) returns true. If no element is found, defValue is returned.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(value int) bool { return value >= 20 }
	fmt.Println(enum.FindOr(items, f, 42))
}
Output:

42

func FindOrWithIndex

func FindOrWithIndex[T any](values []T, f FilterFuncWithIndex[T], defValue T) T

FindOrWithIndex returns the first element for which f(index, value) returns true. If no element is found, defValue is returned.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(index, value int) bool { return index > 4 && value >= 0 }
	fmt.Println(enum.FindOrWithIndex(items, f, 42))
}
Output:

5

func FindWithIndex

func FindWithIndex[T any](values []T, f FilterFuncWithIndex[T]) (ret T, ok bool)

FindWithIndex returns the first element for which f(index, value) returns true along with a boolean indicating a value was found.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{0, 1, 2, 3, 4, -1, 5, 6, 7, 8, 9, 10}
	f := func(index, value int) bool { return index > 4 && value >= 0 }
	fmt.Println(enum.FindWithIndex(items, f))
}
Output:

5 true

func First

func First[T any](items []T) (ret T)

First returns the first item of the provided slice or its zero value.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	var empty []string
	items := []string{"a", "b", "c"}
	fmt.Printf("%q %q", enum.First(empty), enum.First(items))
}
Output:

"" "a"

func FlatMap

func FlatMap[S any, T any](values []S, f func(S) []T) []T

FlatMap maps the given f(value) and flattens the result.

Example (Int)
package main

import (
	"fmt"
	"strconv"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{1, 2, 3}
	f := func(value int) []string {
		s := strconv.Itoa(value)
		return []string{s, s}
	}
	fmt.Println(enum.FlatMap(items, f))
}
Output:

[1 1 2 2 3 3]

func FlatMapWithIndex

func FlatMapWithIndex[S any, T any](values []S, f func(int, S) []T) []T

FlatMapWithIndex maps the given f(index, value) and flattens the result.

Example (Int)
package main

import (
	"fmt"
	"strconv"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{10, 20, 30}
	f := func(index, value int) []string {
		s := strconv.Itoa(value)
		msg := fmt.Sprintf("%v=%v", index, s)
		return []string{msg, msg}
	}
	fmt.Println(enum.FlatMapWithIndex(items, f))
}
Output:

[0=10 0=10 1=20 1=20 2=30 2=30]

func Frequencies

func Frequencies[T comparable](items []T) map[T]int

Frequencies returns a map with keys as unique elements of the provided items and the values as the count of every item.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"0", "1", "2", "0", "1", "0"}
	fmt.Printf("%#v", enum.Frequencies(items))
}
Output:

map[string]int{"0":3, "1":2, "2":1}

func FrequenciesBy

func FrequenciesBy[S any, T comparable](items []S, keyFunc func(S) T) map[T]int

FrequenciesBy returns a map with keys as unique elements of keyFunc(item) and the values as the count of every item.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
	"github.com/gmlewis/advent-of-code-2021/must"
)

func main() {
	items := []string{"0", "1", "2", "0", "1", "0"}
	fmt.Printf("%#v", enum.FrequenciesBy(items, must.Atoi))
}
Output:

map[int]int{0:3, 1:2, 2:1}

func GroupBy

func GroupBy[K comparable, V any, T any](items []T, keyFunc func(T) K, valueFunc func(T) V) map[K][]V

GroupBy splits the items into groups based on keyFunc and valueFunc.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"ant", "buffalo", "cat", "dingo"}
	strLength := func(s string) int { return len(s) }
	fmt.Println(enum.GroupBy(items, strLength, enum.Identity))
}
Output:

map[3:[ant cat] 5:[dingo] 7:[buffalo]]

func Identity

func Identity[T any](value T) T

Identity returns the value passed to it.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	fmt.Println(enum.Identity("a"), enum.Identity("A"))
}
Output:

a A

func Length

func Length[T any](items []T) int

Length returns the length of the provided slice.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"1", "2", "3"}
	fmt.Println(enum.Length(items))
}
Output:

3

func Longer

func Longer[T any](a, b []T) []T

Longer returns the longer slice. If len(a)==len(b), then a is preferred.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	a, b := []int{1, 2, 3, 4}, []int{1, 2, 3, 4, 5}
	fmt.Println(enum.Longer(a, b))
}
Output:

[1 2 3 4 5]

func Map

func Map[S any, T any](values []T, f func(value T) S) []S

Map maps a slice of values from one type to another using the provided func f.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
	"github.com/gmlewis/advent-of-code-2021/must"
)

func main() {
	items := []string{"0", "1", "2", "3", "4", "5", "6"}
	fmt.Printf("%#v", enum.Map(items, must.Atoi))
}
Output:

[]int{0, 1, 2, 3, 4, 5, 6}

func MapWithIndex

func MapWithIndex[S any, T any](values []T, f func(index int, value T) S) []S

MapWithIndex maps a slice of values from one type to another using the provided func f.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"a", "b", "c", "d", "e", "f"}
	f := func(index int, value string) string { return fmt.Sprintf("%v:%v", index, value) }
	fmt.Printf("%#v", enum.MapWithIndex(items, f))
}
Output:

[]string{"0:a", "1:b", "2:c", "3:d", "4:e", "5:f"}

func Max deprecated

func Max[T constraints.Ordered](values []T) (ret T)

Max returns the maximal element in the slice (or the zero value for an empty slice).

Deprecated: As of Go 1.21, max is builtin: https://go.dev/ref/spec#Min_and_max

func MaxFunc

func MaxFunc[T any](values []T, lessFunc func(a, b T) bool) (ret T)

MaxFunc returns the maximal element in the slice (or the zero value for an empty slice) using the provided lessFunc.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	type keyT [2]int
	items := []keyT{{-1, -2}, {3, 4}, {2, 10}, {10, 1}, {1, 10}}
	lessFunc := func(a, b keyT) bool {
		if a[1] == b[1] {
			return a[0] < b[0]
		}
		return a[1] < b[1]
	}
	fmt.Println(enum.MaxFunc(items, lessFunc))
}
Output:

[2 10]

func Member

func Member[T comparable](values []T, elem T) bool

Member checks if elem exists within values.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []string{"a", "b", "c", "d", "e", "f"}
	fmt.Printf("%#v", enum.Member(items, "yo"))
}
Output:

false

func Min deprecated

func Min[T constraints.Ordered](values []T) (ret T)

Min returns the minimal element in the slice (or the zero value for an empty slice).

Deprecated: As of Go 1.21, min is builtin: https://go.dev/ref/spec#Min_and_max

func MinFunc

func MinFunc[T any](values []T, lessFunc func(a, b T) bool) (ret T)

MinFunc returns the minimal element in the slice (or the zero value for an empty slice) using the provided lessFunc.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	type keyT [2]int
	items := []keyT{{-1, -2}, {3, 4}, {2, 10}, {10, 1}, {1, 10}}
	lessFunc := func(a, b keyT) bool {
		if a[1] == b[1] {
			return a[0] < b[0]
		}
		return a[1] < b[1]
	}
	fmt.Println(enum.MinFunc(items, lessFunc))
}
Output:

[-1 -2]

func Product

func Product[T Number](values []T) (ret T)

Product multiples a slice of numbers together.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	a := []int{1, 2, 3, 4}
	fmt.Println(enum.Product(a))
}
Output:

24

func Range

func Range[T Number](start, end T) (ret []T)

Range returns a slice of numbers that run from start to end.

Example (Down)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	fmt.Println(enum.Range(2, 0))
}
Output:

[2 1 0]
Example (Up)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	fmt.Println(enum.Range(0, 2))
}
Output:

[0 1 2]

func Ranges

func Ranges[T int](start, end []T) (ret [][]T)

Ranges returns a slice of slices-of-integers that increment all values from the start to the end.

Note that one of the ranges might overshoot if the distances are not identical.

Example
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	start, end := []int{0, 3, 0}, []int{2, 1, 0}
	fmt.Println(enum.Ranges(start, end))
}
Output:

[[0 3 0] [1 2 0] [2 1 0]]

func Reduce

func Reduce[S any, T any](items []S, acc T, f func(S, T) T) T

Reduce reduces a slice using an accumulator and `f(item, acc)`.

Example
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	f := func(value, acc int) int { return acc + value*value }
	items := []int{1, 2, 3}
	fmt.Println(enum.Reduce(items, 0, f))
}
Output:

14

func ReduceWithIndex

func ReduceWithIndex[S any, T any](items []S, acc T, f func(int, S, T) T) T

ReduceWithIndex reduces a slice using an accumulator and `f(index, item, acc)`.

Example
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	f := func(i, v, acc int) int { return acc + v*v + i*i*i }
	items := []int{1, 2, 3}
	fmt.Println(enum.ReduceWithIndex(items, 0, f))
}
Output:

23

func Scan

func Scan[T any](items []T, acc T, f func(a, b T) T) (ret []T)

Scan applies the given function to each element, emits the result and uses the same result as the accumulator for the next computation. It uses the given acc as the starting value.

For example:

Scan(Range(1,5), 0, func(a, b int) int { return a + b })

returns:

[]int{1, 3, 6, 10, 15}
Example
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	f := func(a, b int) int { return a + b }
	fmt.Println(enum.Scan(enum.Range(1, 5), 0, f))
}
Output:

[1 3 6 10 15]

func Shorter

func Shorter[T any](a, b []T) []T

Shorter returns the shorter slice. If len(a)==len(b), then b is preferred.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	a, b := []int{1, 2, 3, 4}, []int{1, 2, 3, 4, 5}
	fmt.Println(enum.Shorter(a, b))
}
Output:

[1 2 3 4]

func Sum

func Sum[T Number](values []T) (ret T)

Sum sums up a slice of numbers.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	a := []int{1, 2, 3, 4}
	fmt.Println(enum.Sum(a))
}
Output:

10

func Uniq

func Uniq[T comparable](items []T) (ret []T)

Uniq removes all duplicated elements.

Example (Int)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := []int{1, 2, 3, 3, 2, 1}
	fmt.Println(enum.Uniq(items))
}
Output:

[1 2 3]

func Zip

func Zip[T any](lists [][]T) (ret [][]T)

Zip zips corresponding elements from slice of slices.

The zipping finishes as soon as any slice ends.

Example
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items := [][]int{{1, 2}, {3, 4}, {5, 6}}
	fmt.Println(enum.Zip(items))
}
Output:

[[1 3 5] [2 4 6]]

func Zip2

func Zip2[S any, T any, KV any](sList []S, tList []T, f func(S, T) KV) (ret []KV)

Zip2 zips corresponding elements from different types into a slice of structs.

The zipping finishes as soon as either slice ends.

Example
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	items1 := []int{1, 2, 3, 4, 5, 6}
	items2 := []string{"a", "b", "c"}
	type ns struct {
		N int
		S string
	}
	f := func(n int, s string) ns { return ns{n, s} }
	fmt.Println(enum.Zip2(items1, items2, f))
}
Output:

[{1 a} {2 b} {3 c}]

Types

type FilterFunc

type FilterFunc[T any] func(T) bool

FilterFunc takes a value and returns true if the value is to be kept.

func Equals

func Equals[T comparable](value T) FilterFunc[T]

Equals returns a function that checks if a value is equal to a given value.

Example (String)
package main

import (
	"fmt"

	"github.com/gmlewis/advent-of-code-2021/enum"
)

func main() {
	f := enum.Equals("A")
	fmt.Println(f("a"), f("A"))
}
Output:

false true

type FilterFuncWithIndex

type FilterFuncWithIndex[T any] func(int, T) bool

FilterFuncWithIndex takes an index and value and returns true if the value is to be kept.

type Number

type Number interface {
	constraints.Integer | constraints.Float
}

Number has the "+" operator.

Jump to

Keyboard shortcuts

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