github.com/nevalang/neva@v0.23.1-0.20240507185603-7696a9bb8dda/internal/runtime/funcs/int_mul.go (about)

     1  package funcs
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/nevalang/neva/internal/runtime"
     7  )
     8  
     9  type intMul struct{}
    10  
    11  func (intMul) Create(io runtime.FuncIO, _ runtime.Msg) (func(ctx context.Context), error) {
    12  	seqIn, err := io.In.Port("seq")
    13  	if err != nil {
    14  		return nil, err
    15  	}
    16  
    17  	resOut, err := io.Out.Port("res")
    18  	if err != nil {
    19  		return nil, err
    20  	}
    21  
    22  	return func(ctx context.Context) {
    23  		var acc int64 = 1
    24  
    25  		for {
    26  			var item map[string]runtime.Msg
    27  			select {
    28  			case <-ctx.Done():
    29  				return
    30  			case msg := <-seqIn:
    31  				item = msg.Map()
    32  			}
    33  
    34  			acc *= item["data"].Int()
    35  
    36  			if item["last"].Bool() {
    37  				select {
    38  				case <-ctx.Done():
    39  					return
    40  				case resOut <- runtime.NewIntMsg(acc):
    41  					acc = 1 // reset
    42  					continue
    43  				}
    44  			}
    45  		}
    46  	}, nil
    47  }