slices

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: MIT Imports: 3 Imported by: 2

README

Slices

What is Slices

The slices package implements functions to handle slices that are commonly found in functional languages Including:

  • map
  • filter
  • reduce
  • some
  • every
  • flat

Of note: The official Slices project provides other common slice functions like search,index, replace and sort.

How does it work

To realize the above mentioned functions the (newly) added generics were used. As this is a go package concurrency is also used where applicable. It is not possible to directly assign methods to slices and go reservers some keywords like map. For now we circumvent this problem by using a (Method)Slice naming convention.

Usage

For now the intended user is only me. Purpose of this package is to reduce the boilerplate while working with slices in go in my other projects. Secondary purpose for this project is to practice the usage of generics and concurrency.

How to

Develop

Feel free to fork the project! For now I can not guarantee further work on this project

Use

This package is currently highly experimental and should not be used in a production environment. The new version 0.2 implements errors.

Test

Right now no testing is implemented, but I intend to add testing at a later point.

Plans

  • more extensive documentation
  • example project (see tests for now) Right now I am busy with my master thesis.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RoutineCap int = 5

Functions

func EverySlice

func EverySlice[T any](slice []T, isXFunc func(element T) (bool, error)) (bool, error)
Test if Every item fulfils isXFunc criteria.

If isXFunc returns an error the item is skipped for evaluation

func FilterSlice

func FilterSlice[T any](slice []T, filterFunc func(T) (bool, error)) ([]T, error)
Filter every item of the slice using the filterFunc.

If filterFunc returns an error the item is not included in the filtered slice.

func FlatSlice

func FlatSlice[a any](slices [][]a) (result []a)

Flat function :: [[]]a -> []a

func MapSlice

func MapSlice[T, R any](slice []T, mapFunc func(T) (R, error)) ([]R, error)

Map every item of the slice using the mapFunc. If the mapFunc returns a err for position i the new slice returns an empty value instead of the result at position i.

func OrderedReduceSlice

func OrderedReduceSlice[T, R any](slice []T, reduceFunc func(element T, basis R) (R, error), zero R) (R, error)

Reduce slice to single value using the reduceFunc. Use this if order of reduction matters. If reduceFunc returns an error the result of reduceFunc is not included in the result.

func SomeSlice

func SomeSlice[T any](slice []T, isXFunc func(element T) (bool, error)) (bool, error)

Test if Some items fulfil isXFunc criteria. If isXFunc returns an error the item is skipped for evaluation

func UnorderedReduceSlice

func UnorderedReduceSlice[T, R any](slice []T, reduceFunc func(element T, basis R) (R, error), zero R) (R, error)
Reduce slice to single value using the reduceFunc.

Use this order of reduction does not matter. If reduceFunc returns an error the result of reduceFunc is not included in the result.

Types

This section is empty.

Jump to

Keyboard shortcuts

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