github.com/hairyhenderson/gomplate/v4@v4.0.0-pre-2.0.20240520121557-362f058f0c93/math/math.go (about)

     1  // Package math contains set of basic math functions to be able to perform simple arithmetic operations
     2  package math
     3  
     4  // AddInt -
     5  func AddInt(n ...int64) int64 {
     6  	x := int64(0)
     7  	for _, i := range n {
     8  		x += i
     9  	}
    10  	return x
    11  }
    12  
    13  // MulInt -
    14  func MulInt(n ...int64) int64 {
    15  	var x int64 = 1
    16  	for _, i := range n {
    17  		x *= i
    18  	}
    19  	return x
    20  }
    21  
    22  // Seq - return a sequence from `start` to `end`, in steps of `step`.
    23  func Seq(start, end, step int64) []int64 {
    24  	// a step of 0 just returns an empty sequence
    25  	if step == 0 {
    26  		return []int64{}
    27  	}
    28  
    29  	// handle cases where step has wrong sign
    30  	if end < start && step > 0 {
    31  		step = -step
    32  	}
    33  	if end > start && step < 0 {
    34  		step = -step
    35  	}
    36  
    37  	// adjust the end so it aligns exactly (avoids infinite loop!)
    38  	end -= (end - start) % step
    39  
    40  	seq := []int64{start}
    41  	last := start
    42  	for last != end {
    43  		last = seq[len(seq)-1] + step
    44  		seq = append(seq, last)
    45  	}
    46  	return seq
    47  }