fngo

package module
v0.0.0-...-9d89dda Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: Apache-2.0 Imports: 2 Imported by: 0

README

f'n Go!

This Golang package provides support for functional-style processing pipelines using generics. Each stage of a pipeline runs in a goroutine and is connected by channels. The failure of any stage aborts the chain and returns an error. An abort may also be triggered manually using a Context.

Usage

Every pipeline begins with a Source producing a sequence of typed values and ends with a Reduce or Sink consuming a sequence of values. In between these can be any combination of processing functions, of which the following are presently supported:

  • Filter -- Removes values according to a rule
  • Flatten -- Collapses a slice-of-slices into a simple slice (e.g. [[1,2,3],[4,5,6]] becomes [1,2,3,4,5,6])
  • Map -- Converts values into something new according to a rule

Parallelized versions of Filter and Map also exist but do not guarantee the sequence of values is maintained.

Example

In this demonstration a series of names (SliceSource) are reduced to only those containing the letter A (Filter). The remaining values are converted to their corresponding lengths (Map), and the resulting sequence of numbers is printed to standard-out (Sink).

func main() {
    names := functional.SliceSource(context.Background(),
        []string{"alice", "bob", "charlie", "david", "erin"})

    namesWithA := functional.Filter(names, onlyWithA)
    lengths := functional.Map(namesWithA, nameLength)

    err := functional.Sink(lengths, printLength)
    if err != nil {
        log.Fatal(err)
    }
}

func nameLength(ctx context.Context, name string) (int, error) {
    return len(name), nil
}

func onlyWithA(ctx context.Context, name string) (bool, error) {
    return strings.HasRune(name, 'a'), nil
}

func printLength(ctx context.Context, length int) error {
    fmt.Println(length)
    return nil
}

func someNames(ctx context.Context, output chan<- string) error {
    for name := range  {
        output <- name
    }
    return nil
}

The final output is as follows:

5
7
5

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reduce

func Reduce[I, O any](input Pipeline[I], reducer func(context.Context, I, O) (O, error), initialState O) (O, error)

Reduce is a terminal processing stage that consumes values of type I and reduces them down to a single value of type O using the given reducer function, beginning with the given initial state.

func Sink

func Sink[T any](input Pipeline[T], sink func(context.Context, T) error) error

Sink is a terminal processing stage that consumes values of type T using the given sink function. Any error generated by the Pipeline's errgroup will be returned here.

Types

type Pipeline

type Pipeline[T any] struct {
	// contains filtered or unexported fields
}

Pipeline is a connection between two processing stages working on type T.

func Filter

func Filter[T any](input Pipeline[T], filter func(context.Context, T) (bool, error)) Pipeline[T]

Filter is a processing stage that passes or blocks values of type T according to whether the given filter function returns true or false, respectively.

func Flatten

func Flatten[T any](input Pipeline[[]T]) Pipeline[T]

Flatten is a processing stage that collapses a sequence of slices of type T into a single slice of the same type.

func Map

func Map[I, O any](input Pipeline[I], mapper func(context.Context, I) (O, error)) Pipeline[O]

Map is a processing stage that converts values of type I into values of type O using the given mapper function.

func ParallelFilter

func ParallelFilter[T any](input Pipeline[T], filter func(context.Context, T) (bool, error)) Pipeline[T]

ParallelFilter is identical to Filter except the filtering operations are performed in parallel. This process is not guaranteed to maintain the order of the values.

func ParallelMap

func ParallelMap[I, O any](input Pipeline[I], mapper func(context.Context, I) (O, error)) Pipeline[O]

ParallelMap is identical to Map except the mapping operations are performed in parallel. This process is not guaranteed to maintain the order of the values.

func SliceSource

func SliceSource[T any](ctx context.Context, slice []T) Pipeline[T]

SliceSource is a helper function around Source that generates values from the given slice.

func Source

func Source[T any](ctx context.Context, source func(context.Context, func(T) error) error) Pipeline[T]

Source is a processing stage that generates values of type T using the given source function. This and all subsequent stages will run within an errgroup created from the given Context.

The channel passed to the generator function is automatically closed when the function returns.

Jump to

Keyboard shortcuts

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