-
-
Notifications
You must be signed in to change notification settings - Fork 828
/
math_example_test.go
86 lines (69 loc) · 1.4 KB
/
math_example_test.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package lo
import (
"fmt"
)
func ExampleRange() {
result1 := Range(4)
result2 := Range(-4)
result3 := RangeFrom(1, 5)
result4 := RangeFrom(1.0, 5)
result5 := RangeWithSteps(0, 20, 5)
result6 := RangeWithSteps[float32](-1.0, -4.0, -1.0)
result7 := RangeWithSteps(1, 4, -1)
result8 := Range(0)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
fmt.Printf("%v\n", result4)
fmt.Printf("%v\n", result5)
fmt.Printf("%v\n", result6)
fmt.Printf("%v\n", result7)
fmt.Printf("%v\n", result8)
// Output:
// [0 1 2 3]
// [0 -1 -2 -3]
// [1 2 3 4 5]
// [1 2 3 4 5]
// [0 5 10 15]
// [-1 -2 -3]
// []
// []
}
func ExampleClamp() {
result1 := Clamp(0, -10, 10)
result2 := Clamp(-42, -10, 10)
result3 := Clamp(42, -10, 10)
fmt.Printf("%v\n", result1)
fmt.Printf("%v\n", result2)
fmt.Printf("%v\n", result3)
// Output:
// 0
// -10
// 10
}
func ExampleSum() {
list := []int{1, 2, 3, 4, 5}
sum := Sum(list)
fmt.Printf("%v", sum)
// Output: 15
}
func ExampleSumBy() {
list := []string{"foo", "bar"}
result := SumBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
// Output: 6
}
func ExampleMean() {
list := []int{1, 2, 3, 4, 5}
result := Mean(list)
fmt.Printf("%v", result)
}
func ExampleMeanBy() {
list := []string{"foo", "bar"}
result := MeanBy(list, func(item string) int {
return len(item)
})
fmt.Printf("%v", result)
}