forked from hybridgroup/gobot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
66 lines (56 loc) · 1.61 KB
/
utils.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
58
59
60
61
62
63
64
65
66
package gobot
import (
"crypto/rand"
"math"
"math/big"
"time"
)
// Every triggers f every t time until the end of days. It does not wait for the
// previous execution of f to finish before it fires the next f.
func Every(t time.Duration, f func()) {
c := time.Tick(t)
go func() {
for {
<-c
go f()
}
}()
}
// After triggers f after t duration.
func After(t time.Duration, f func()) {
time.AfterFunc(t, f)
}
// Publish emits val to all subscribers of e.
func Publish(e *Event, val interface{}) {
e.Write(val)
}
// On executes f when e is Published to.
func On(e *Event, f func(s interface{})) {
e.Callbacks = append(e.Callbacks, callback{f, false})
}
// Once is similar to On except that it only executes f one time.
func Once(e *Event, f func(s interface{})) {
e.Callbacks = append(e.Callbacks, callback{f, true})
}
// Rand returns a positive random int up to max
func Rand(max int) int {
i, _ := rand.Int(rand.Reader, big.NewInt(int64(max)))
return int(i.Int64())
}
// FromScale returns a converted input from min, max to 0.0...1.0.
func FromScale(input, min, max float64) float64 {
return (input - math.Min(min, max)) / (math.Max(min, max) - math.Min(min, max))
}
// ToScale returns a converted input from 0...1 to min...max scale.
// If input is less than min then ToScale returns min.
// If input is greater than max then ToScale returns max
func ToScale(input, min, max float64) float64 {
i := input*(math.Max(min, max)-math.Min(min, max)) + math.Min(min, max)
if i < math.Min(min, max) {
return math.Min(min, max)
} else if i > math.Max(min, max) {
return math.Max(min, max)
} else {
return i
}
}