parmap

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2024 License: MIT Imports: 4 Imported by: 0

README

parmap

parmap offers a simple generic implementation of parallel map functionality.

Features:

  • Parallel execution
  • Generic
  • Deterministic
  • Verbose error handling
  • No race conditions

TODO:

  • Implement a way to set max goroutines for the library to start

Installation

go get github.com/fcutting/parmap

Usage

Example:

import (
  "fmt"
  "github.com/fcutting/parmap"
)

func main() {
  inputs := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
  do := func(i int) (int, error) {
    return i * 5, nil
  }

  result, err := parmap.Do(inputs, do)

  if err != nil {
    panic(fmt.Sprintf("Unepxected error: %s", err))
  }

  fmt.Println(result)
}

Output:

[5 10 15 20 25 30 35 40 45 50]

Contributing

I'm always open to constructive criticism and help, so if you do wish to contribute to this repo, please abide by the following process:

  • Create an issue that describes the bug/feature
  • If you wish to fix/implement this yourself, please create a PR and link to the issue you created (ensure that all code you update/add is well tested)

Please remember that I will not always accept new ideas or code, especially since this repo is intended to be minimalistic and only implement one piece of functionality.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrMap

type ErrMap map[int]error

ErrMap is a map[int]error where the key is the index of the input that failed. ErrMap implements Stringer. ErrMap implements Error.

func Do

func Do[IN, OUT any](inputs []IN, do doFunc[IN, OUT]) (result []OUT, err ErrMap)

Do runs the do func for each input in the inputs slice in parallel and returns a slice of results and an error. The length of the result slice will always be the same length as the inputs slice. If no errors occurred in the execution of the do funcs, the returned err will be nil. If errors occurred in the execution of the do funcs, the result slice will have the zero value of the OUT type at the indexes of the failed inputs.

func (ErrMap) ErrJoin

func (e ErrMap) ErrJoin() error

ErrJoin joins all the errors in the ErrMap in a deterministic way. Each error is of the form "<key>: <error>".

func (ErrMap) Error

func (e ErrMap) Error() string

Error joins all the errors in the ErrMap and returns it as a string.

func (ErrMap) String

func (e ErrMap) String() string

String joins all the errors in the ErrMap and returns it as a string.

Jump to

Keyboard shortcuts

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