slicendice

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2024 License: BSD-3-Clause Imports: 1 Imported by: 0

README

slice and dice

A collection of commonly used higher order functions and data structures in go, like itertools.

  • Map
  • Filter
  • Reduce
  • Some
  • Every
  • Take
  • Permute
  • NextPermute
  • PermuteOrdered
  • Combine
  • Zip
  • ZipPairs
  • Batch

Lazy:

  • Iter
  • Map
  • Filter
  • Reduce

Extra datastrcutures:

  • Set

Example

Although most of them are covered in _test.go, here is brief rundown.


package main

import (
    "github.com/go-batteries/slicendice"
    "github.com/go-batteries/slicendice/lazy"
)

func main() {
    inputs := []int{1, 2, 3, 5}
    inputIter := lazy.ToSliceIter(inputs)

    adder := func(el int, _ int) int {
        return el + 1
    }

    evening := func(el int, _ int) bool {
        return el % 2 == 0
    }

    increments := slicendice.Map(inputs, adder)
    // returns []int{2,3,4,6}

    evens := slicendice.Filter(increments, evening)
    // return []int{2, 4, 6}

    iter := lazy.MapIntoIter(inputs, adder)
    // returns an iterator with Next()
    // or you could also do

    // iter := lazy.Map(inputIter, adder)

    // iterate
    for ok, res := iter.Next(); ok; ok, res = iter.Next() {
        // do something with result
    }

    // for a chained usecase
    // add one and filter evens
    // and take first 5
    inputs = []int{1,2,3,4,5,6,7,8,9}
    
    mapper := lazy.Map(lazy.ToSliceIter(inputs), adder)
    filterr := lazy.Filter(mapper, evening)

    iter2 := lazy.Take(5, filterr)
    // or
    iter2 = lazy.Take(5, lazy.Filter(lazy.Map(inputIter, added), evening))
}

Documentation

Index

Constants

View Source
const MAX_COUNT = 10000000

Variables

This section is empty.

Functions

func Batch

func Batch(total int64, groups int) [][]int

Given total length of array and batch size returns tuple of [startIndex, endIndex] Handles both odd and even Golang truncated division https://go.dev/ref/spec#Arithmetic_operators total = 12 groups = 5 totalGroup = 12/5 + (12 % 5 != 0 ? 1 : 0) = 2 + 1 [0, 4], [5, 9], [10, 11]

total = 12, groups = 3 totalGroups = (12 / 3) + (12 % 3) = 4 [0, 2], [3,5], [6, 8], [9, 11]

func Combine

func Combine[E any](elements []E) [][]E

func CombineK

func CombineK[E any](elements []E, k int) [][]E

func Every

func Every[E any](elements []E, predicate PredicateFunc[E]) bool

Every checks if all elements in the slice meet the condition defined by the provided predicate function. It takes two arguments: - elements: a slice of elements of type E to be checked. - predicate: a function that defines the condition to check for each element. It returns true if all elements satisfy the condition; otherwise, false.

func Factorial

func Factorial(n int) int64

Factorial calculates the factorial for negative integers default to 0!

func Filter

func Filter[E any](elements []E, filter FilterFunc[E]) []E

Filter applies a filter function to each element in a slice and returns a new slice of elements that meet the condition. It takes two arguments: - elements: a slice of elements of type E to be filtered. - filter: a function that defines the condition for including elements. - It returns a new slice containing the filtered elements.

func Map

func Map[E, V any](elements []E, mapper MapperFunc[E, V]) []V

Map applies a mapping function to each element in a slice and returns a new slice of transformed elements. It takes three arguments: - elements: a slice of elements of type E to be transformed. - mapper: a function that defines how each element will be transformed. - It returns a new slice of type V containing the transformed elements.

func Max

func Max[E cmp.Ordered](values ...E) (e E)

func Min

func Min[E cmp.Ordered](values ...E) (e E)

func NextPermute

func NextPermute[E cmp.Ordered](elements []E) (bool, []E)

NextPermute find the next permuted if possible Returns ok, elements. If next permutation is exhausted ok is false The algorithm goes like: - Find the largest index of k such that a[k] < a[k+1] - Find the largest index l such that a[l] > a[k] - Swap the value of a[l] and a[k], and - Reverse the list

func Permutations

func Permutations[E cmp.Ordered](elements []E) (bool, [][]E)

Permutations gets all the possible permutations This version doesn't gurrantee lexical ordered

func PermutationsOrdered

func PermutationsOrdered[E cmp.Ordered](elements []E) (bool, [][]E)

PermutationsOrdered same as Permutations but lexically ordered

func Reduce

func Reduce[E, V any](elements []E, reducer ReducerFunc[E, V], accumulator V) V

Reduce applies a reducer function to each element in a slice and accumulates the results. It takes three arguments:

  • elements: a slice of elements of type E that you want to reduce.
  • reducer: a function that defines how the reduction will proceed. It takes the current accumulator value (V), the next element (E), and the index (int) of the element being processed.
  • accumulator: the initial value of the accumulator, of type V, which is also the final result type after the reduction.

The function returns the final accumulator value after processing all elements.

func Repeat

func Repeat[E any](item E, times int) []E

func Reverse

func Reverse[E any](elements []E) []E

func Some

func Some[E any](elements []E, predicate PredicateFunc[E]) bool

Some checks if at least one element in the slice meets the condition defined by the provided predicate function. It takes two arguments: - elements: a slice of elements of type E to be checked. - predicate: a function that defines the condition to check for each element. It returns true if at least one element satisfies the condition; otherwise, false.

func Take

func Take[E any, T IntTypes](elements []E, takeN int) []E

func Zip

func Zip[E any](left, right []E) (bool, [][]E)

Zips two arrays of equal length and same type into a single array, such that f(left, right) = [left[i], right[i]]

Types

type FilterFunc

type FilterFunc[E any] func(element E, index int) bool

FilterFunc is a generic function type that defines the condition for including an element in the filtered result. It takes two parameters: - element: the current element of type E being processed. - index: the index of the current element in the collection. It returns true if the element should be included in the filtered result.

type IntTypes

type IntTypes interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type MapperFunc

type MapperFunc[E, V any] func(element E, index int) V

MapperFunc is a generic function type that defines how each element in a collection is transformed. It takes two parameters: - element: the current element of type E being processed. - index: the index of the current element in the collection. It returns the transformed element of type V.

type Pair

type Pair[E, V any] struct {
	First E
	Last  V
}

func ZipPairs

func ZipPairs[E, V any](left []E, right []V) (bool, []Pair[E, V])

ZipPairs is same as Zip but works on heterogeneous types liek []string{}, []int{}

type PredicateFunc

type PredicateFunc[E any] func(element E, index int) bool

PredicateFunc is a generic function type that defines the condition for determining if at least one or all elements in a collection meets the criteria. It takes two parameters: - element: the current element of type E being processed. - index: the index of the current element in the collection. It returns true if the element meets the criteria.

type ReducerFunc

type ReducerFunc[E, V any] func(accumulator V, next E, index int) V

ReducerFunc is a generic function type that defines the operation to be applied to each element in a collection during reduction. It takes three arguments: - accumulator: the cumulative result (of type V) that is carried forward. - next: the next element (of type E) in the collection to be processed. - index: the index of the current element in the collection. It returns the updated accumulator (of type V).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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