-
Notifications
You must be signed in to change notification settings - Fork 4
/
gospark.go
57 lines (50 loc) · 1.07 KB
/
gospark.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"bytes"
"math"
"os"
"fmt"
"strconv"
)
// Parse the command line arguments
func main() {
argsWithoutProg := os.Args[1:]
var vals []float64
for _, s := range argsWithoutProg {
f, err := strconv.ParseFloat(s, 64)
if err == nil {
vals = append(vals, f)
}
}
fmt.Println(Signals(vals))
}
var ticks = []rune("▁▂▃▄▅▆▇█")
// Generate the normalised signal chart
func Signals(nums []float64) string {
if len(nums) == 0 {
return ""
}
min, max := bounds(nums)
var sparks bytes.Buffer
if math.Abs(max - min) < 0.0000001 {
for c := 0; c < len(nums); c++ {
sparks.WriteRune(ticks[0])
}
} else {
scale := 7.0 / (max - min)
for _, n := range nums {
tick := int((n - min) * scale + 0.5) // always positive
sparks.WriteRune(ticks[tick])
}
}
return sparks.String()
}
// Compute the min and max bounds of the dataset
func bounds(nums []float64) (float64, float64) {
min, max := math.MaxFloat64, math.SmallestNonzeroFloat64
for _, n := range nums {
if (n < min) { min = n }
if (n > max) { max = n }
}
return min, max
}