Giter Site home page Giter Site logo

siggimoo / fngo Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 12 KB

Sometimes all you need is a simple functional-style so that you can stop messing around and just f'n go!

License: Apache License 2.0

Go 100.00%
functional-programming golang pipelines generics

fngo's Introduction

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

fngo's People

Contributors

siggimoo avatar

Watchers

 avatar

fngo's Issues

Add SinkParallel function

A parallel version of Sink would be helpful for cases where the terminal stage of a pipeline does not need to process values in sequence.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.