GithubHelp home page GithubHelp logo

flow's Introduction

Flow

Simple framework to design workflows based on processes connected through channels.

Each process has an input channel and an output channel,the output channel is the input channel of the next process.

Each process is responsible for closing the output channel when no more information will be sent.

Each process will process the data received from its input channel until the end of the data.

Each created process can be reused in other flows

Creating a Process:

type Summarizer struct {
}

func (p Summarizer) Process(input flow.Chan, output flow.Chan, ctx flow.Context) {
    //Cast input and output channels
    in := input.(chan int)
    out := output.(chan int)

    //I close the output channel, when no more data to send
    defer close(out)


    //I add the input values ​​and accumulate the total
    tot:=0
    for {
        num, ok := <-in
        if !ok {
            break
        }
        tot += nuk
    }

    //Send the total to the output channel
    out <- tot

}

Creating a Flow

    // I create the flow with an instance of Process.
    // The input channel of the flow.
    // The output channel of the flow
    fl := flow.New(Summarizer{}, make(chan int, 0))

    // I create the flow data input channel
    input := make(chan string, 1)

    ctx := flow.Context{}

    //Start the flow
    outChan := fl.Start(input, ctx)

Using the Flow

    //Send data to the flow
    input <- 10
    input <- 20
    input <- 30
    input <- 40

    //No more data to send, I close the channel
    close(input)

    //Cast the flow outflow and get the result
    out := outChan.(chan int)
    total := <-out
    fmt.Println(total)

    //Printed Result is
    100

Adding Processes to the Flow

    // I create the flow with an instance of Process.
    // The input channel of the flow.
    // The output channel of the flow
    fl := flow.New(Summarizer{}, input, make(chan int, 0))

    // I add a new process to the flow ToString
    // This process has as input channel the output channel of Summarizer
    // The output channel of the ToString process is of type string
    // This process takes the Summarize total and transforms it into a string
    fl.Add(ToString{}, make(chan string, 0))

    // I create the flow data input channel
    input := make(chan string, 1)

    ctx := flow.Context{}

   //Start the flow
    outChan := fl.Start(input, ctx)

    // I execute the flow and now the total 100 number is transformed into a string as shown below

    //Cast the flow outflow and get the result
    out := outChan.(chan string)
    total := <-out
    fmt.Println(total)

    //Printed Result is
    "The Total is : 100"

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.