GO Language Function Programming-Closure
concept
The main support of go language for function programming is mainly embodied in closure functions.
- Functions are first-class citizens: parameters, variables, and return values can all be functions
- Higher order function
- Function Closure
Orthodox functional programming requires: - Invariability: There can be no states, only constants and functions
- A function has only one parameter
go is a common language, it's not so strict.
GO function closure
Function body has local variables and free variables. Free variables connect a line. Free variables can be a structure. The structure connects a tree. Find connections and connect them to each other, and finally connect all the things we need to connect to. When it's all connected, it's called a closure. So when the function returns, it returns a closure function.
Sample code:
package main import "fmt" //Define a Closure Function to Accumulate Functions func adder() func (num int) int { sum := 0 //sum is a free variable. return func(num int) int{ sum += num //num is a local variable return sum } //Returns a closure function } func main(){ a := adder(); for i:=0;i<10;i++ { anum := a(i) fmt.Printf("0+...+%d = %d\n",i,anum) } }
Functional programming without free variables (orthodox functional programming)
package main import "fmt" type iAdder func(v int) (int,iAdder) func Adder(base int) iAdder { //Function programming without free variables return func(v int) (int,iAdder){ return base+v,Adder(base+v) } } func main(){ a := Adder(0) var sum int for i := 1 ; i<10 ;i++ { sum , a = a(i) fmt.Printf("0+...+%d = %d\n",i,sum) } }
Functional Programming for Fibonacci Sequences
The first two numbers of 1, 1, 2, 3, 5 are fixed at 1. The law of the latter is the sum of the first two numbers of the current number.
package main import "fmt" //Using Closure Function to Realize Fibonacci Sequence func fibonacci() func() int{ a,b := 0,1 return func()int{ //The latter number replaces the former one a,b = b,a+b return a } } //The current number of Fibonacci series is the sum of the first two numbers. func main(){ f := fibonacci() for i := 1 ; i < 11 ; i++ { fmt.Println(f()) } }
F is like the generator of fibonacci number. We can encapsulate F as a file reader and read it all the time. If f implements the reader interface, we can use bufio to read the file. Here is the optimized code:
package main import ( "bufio" "fmt" "io" "strings" ) //Custom function type type Fnc func() int //Implementing reader interface function types can also be the recipient of methods func (f Fnc)Read(p []byte)(n int,err error){ num := f() if num < 10000 { s := fmt.Sprintf("%d\n",num) //TODO: What if num is big? p can't hold any more, only read part of it. return strings.NewReader(s).Read(p) } return 0,io.EOF } //Using Closure Function to Realize Fibonacci Sequence func fibonacci() Fnc{ a,b := 0,1 return func()int{ //The latter number replaces the former one a,b = b,a+b return a } } //The current number of Fibonacci series is the sum of the first two numbers. func main(){ f := fibonacci() PrintContent(f) } func PrintContent(reader io.Reader){ scanner := bufio.NewScanner(reader) for scanner.Scan() { fmt.Println(scanner.Text()) } }