5. Functional Programming
Keywords:
Programming
Go
I. Functional Programming
Functional Programming VS Function Pointer
- Functions are first-class citizens: parameters, variables, and return values can all be functions
- Higher order function
- Function - > Closure
Orthodox Functional Programming
- Invariability: There can be no states, only constants and functions
- A function can only have one parameter
- When programming in go language, the above strict rules are not necessary
Closure implementation accumulator
package main
import "fmt"
// closure
func adder() func(int) int {
sum := 0
return func(v int) int {
sum += v
return sum
}
}
// Orthodox Functional Programming Closure
type iAdder func(int) (int, iAdder) // recursion
func adder2(base int) iAdder {
return func(v int) (int, iAdder) {
return base + v, adder2(base + v)
}
}
func main() {
a1 := adder()
for i := 0; i < 10; i++ {
fmt.Printf("0 + 1 + ... + %d = %d\n", i, a1(i))
}
a := adder2(0)
for i := 0; i < 10; i++ {
var s int
s, a = a(i)
fmt.Printf("0 + 1 + ... + %d = %d\n",
i, s)
}
}
2. Examples of Functional Programming
Fibonacci sequence
package main
import "fmt"
// 1, 1, 2, 3, 5, 8, 13, ...
func Fibonacci() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
func main() {
f := Fibonacci()
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
}
Generate interfaces for functions
- Make Fibonacci function, realize reader interface. So you can print it like you read a file.
package main
import (
"bufio"
"fmt"
"io"
"strings"
"learngo/lang/functional/fib"
)
type intGen func() int // Fibonacci function interface
func (g intGen) Read(
p []byte) (n int, err error) {
next := g()
if next > 10000 { // More than 10,000 lets the function end, that is, the file reads to the end.
return 0, io.EOF
}
s := fmt.Sprintf("%d\n", next)
// TODO: incorrect if p is too small!
return strings.NewReader(s).Read(p)
}
func printFileContents(reader io.Reader) {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
func main() {
var f intGen = fib.Fibonacci()
printFileContents(f)
}
Posted by stevenm187 on Thu, 24 Jan 2019 07:45:13 -0800