the closure that is not clear
The concept of closures has been heard, but more often than not, the more you explain, the more you don't understand. Today, I used the Golang of the Fibonacci series to explain the closure of the package in seconds. Remember: Everything is a routine. Fibonacci Series
A Fibonacci sequence is a sequence of numbers in which a number in a position in a sequence is the sum of the digits in the first two positions of the position.
For example, to specify two starting digits, such as 0 and 1, the Fibonacci sequence would look like this: 0, 1, 1, 2, 3, 5, 8, 13 ... First up Code
package main import "FMT"//Simple two numbers added function func Add (x, y int) int {return x + y}//Fibon
The ACCI function returns a function that returns an int. Func Fibonacci (S1, S2 int) func () int {fmt. Println ("Fibonacci start with", S1, "and", S2)//Routine 3: Initialize the variables left and right that need to be remembered, this part will only be called once.: = S1 Right: = S2 return func () int {//This section begins to be called multiple times, and as long as the external caller calls the F function once, this is done once next: = Add (left, right)/routines
4: Update the variables that need to be remembered left and right left = right right = next//Routine 5: Returns the result of each F being invoked return next}}
Func Main () {//routine 1: Call a closure function to get the F function, which can also be called the initialization of the closure function f: = Fibonacci (0, 1) for I: = 0; i < i++ { Routine 2: Repeatedly call F to get results fmt. Println ("Next Value:", f ())}}
Copy the code to the Fibonacci.go file, and run the go run fibonacci.go to see the results.
The following are the results of the operation:
Fibonacci start with 0 and 1
Next Value:1
Next Value:2
Next Value:3
Next Value:5
Next Value:8
Next value:13
Next value:21
Next value:34
Next value:55
Next value:89 closure of the call concept
1, it will be called one time to initialize
2, return a function f to the caller
3, the caller will be able to repeat the call F to achieve the purpose of the closure of the programming routines
1, in the closure function and in the return function , define the variables that need to be memorized for each next calculation, such as left and right
2, in the body of the return function to implement each update of the memory variable
3, returns a summary of the results that the caller expects in the body of the return function
The entire process and machine learning within the neural network of the RNN algorithm is very similar if there is a rnn background of the reunion very fast understanding.