github.com/MontFerret/ferret@v0.18.0/pkg/runtime/core/helpers.go (about) 1 package core 2 3 import ( 4 "context" 5 "math" 6 "math/rand" 7 "reflect" 8 "time" 9 ) 10 11 func IsNil(input interface{}) bool { 12 val := reflect.ValueOf(input) 13 kind := val.Kind() 14 15 switch kind { 16 case reflect.Ptr, 17 reflect.Array, 18 reflect.Slice, 19 reflect.Map, 20 reflect.Func, 21 reflect.Interface, 22 reflect.Chan: 23 return val.IsNil() 24 case reflect.Struct, 25 reflect.UnsafePointer: 26 return false 27 case reflect.Invalid: 28 return true 29 default: 30 return false 31 } 32 } 33 34 func NumberBoundaries(input float64) (max float64, min float64) { 35 min = input / 2 36 max = input * 2 37 38 return 39 } 40 41 func NumberUpperBoundary(input float64) float64 { 42 return input * 2 43 } 44 45 func NumberLowerBoundary(input float64) float64 { 46 return input / 2 47 } 48 49 func RandomDefault() float64 { 50 rand.Seed(time.Now().UnixNano()) 51 52 return rand.Float64() 53 } 54 55 func Random(max float64, min float64) float64 { 56 r := RandomDefault() 57 i := r * (max - min + 1) 58 out := math.Floor(i) + min 59 60 return out 61 } 62 63 func Random2(mid float64) float64 { 64 max, min := NumberBoundaries(mid) 65 66 return Random(max, min) 67 } 68 69 func ForEach(ctx context.Context, iter Iterator, predicate func(value Value, key Value) bool) error { 70 for { 71 value, key, err := iter.Next(ctx) 72 73 if err != nil { 74 if IsNoMoreData(err) { 75 return nil 76 } 77 78 return err 79 } 80 81 if !predicate(value, key) { 82 return nil 83 } 84 } 85 }