Исходники и презентации
This commit is contained in:
43
lessons/functions/pure_function/main.go
Normal file
43
lessons/functions/pure_function/main.go
Normal file
@ -0,0 +1,43 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"os"
|
||||
)
|
||||
|
||||
var cache = map[int]struct{}{}
|
||||
|
||||
func dirtySum(x, y *int) int {
|
||||
result := *x + *y
|
||||
|
||||
// mutations of data (1)
|
||||
*x = 0
|
||||
*y = 0
|
||||
|
||||
// logging or files/network (2)
|
||||
fmt.Println(result)
|
||||
|
||||
// global variables (3)
|
||||
cache[result] = struct{}{}
|
||||
|
||||
// dependencies on parameters (4)
|
||||
os.Getenv("parameter")
|
||||
|
||||
// unpredictable result (5)
|
||||
result += rand.Intn(100)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func pureSum(x, y int) int {
|
||||
return x + y
|
||||
}
|
||||
|
||||
func main() {
|
||||
x := 13
|
||||
y := 12
|
||||
fmt.Println(dirtySum(&x, &y))
|
||||
|
||||
fmt.Println(pureSum(13, 12))
|
||||
}
|
||||
Reference in New Issue
Block a user