github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/lists/sum.go (about)

     1  package lists
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/lmorg/murex/lang/types"
     7  )
     8  
     9  func SumInt(dst, src map[string]int) {
    10  	for key, i := range src {
    11  		dst[key] += i
    12  	}
    13  }
    14  
    15  func SumFloat64(dst, src map[string]float64) {
    16  	for key, i := range src {
    17  		dst[key] += i
    18  	}
    19  }
    20  
    21  func SumInterface(dst, src map[string]interface{}) error {
    22  	for key, v := range src {
    23  		f1, err := types.ConvertGoType(dst[key], types.Float)
    24  		if err != nil {
    25  			return fmt.Errorf("cannot convert '%v' in destination to `number` (float64)", dst[key])
    26  		}
    27  
    28  		f2, err := types.ConvertGoType(v, types.Float)
    29  		if err != nil {
    30  			return fmt.Errorf("cannot convert '%v' in source to `number` (float64)", v)
    31  		}
    32  
    33  		dst[key] = f1.(float64) + f2.(float64)
    34  	}
    35  
    36  	return nil
    37  }