float_array.go 764 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package app
  2. import (
  3. "fmt"
  4. "log"
  5. "sort"
  6. "strconv"
  7. "strings"
  8. )
  9. type FloatArray []float64
  10. func (a *FloatArray) Get() interface{} { return []float64(*a) }
  11. func (a *FloatArray) Set(param string) error {
  12. for _, s := range strings.Split(param, ",") {
  13. v, err := strconv.ParseFloat(s, 64)
  14. if err != nil {
  15. log.Fatalf("Could not parse: %s", s)
  16. return nil
  17. }
  18. *a = append(*a, v)
  19. }
  20. sort.Sort(*a)
  21. return nil
  22. }
  23. func (a FloatArray) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
  24. func (a FloatArray) Less(i, j int) bool { return a[i] > a[j] }
  25. func (a FloatArray) Len() int { return len(a) }
  26. func (a *FloatArray) String() string {
  27. var s []string
  28. for _, v := range *a {
  29. s = append(s, fmt.Sprintf("%f", v))
  30. }
  31. return strings.Join(s, ",")
  32. }