### 匿名函数

• 头等函数（first-class function）
• 闭包（closure）
• 柯里化（currying）
• 高阶函数（higher-order function）

### 头等函数

``````package main

import (
"fmt"
)

func main() {
sayHi()()

// anonymous function: a function definition without name
cmp := func(a, b int) int {
return b - a
}

x := 4
y := 21
// pass function `cmp` as arguments to function `max`
m := max(x, y, cmp)
fmt.Printf("max(%d, %d) = %d\n", x, y, m)

// output:
// Hi!
// max(4, 21) = 4
}

// return function as value from other function
func sayHi() func() {
return func() {
fmt.Println("Hi!")
}
}

// pass function as argument to other function
func max(x, y int, cmp func(a, b int) int) int {
if cmp(x, y) > 0 {
return x
}

return y
}
``````

### 闭包

``````package main

import (
"fmt"
)

func main() {
sq := squares()

fmt.Println(sq())
fmt.Println(sq())
fmt.Println(sq())

// Output:
// 1
// 4
// 9
}

// Closures: function values are not just code but can have state
func squares() func() int {
var x int
return func() int {
// the local variable `x` was captured
x++
return x * x
}
}
``````
``````package main

import (
"fmt"
"time"
)

// A pitfall of Go's lexical scope rule
// that can cause surprising results.
//
// All goroutine function values created by `for` loop
// `capture` and share the same variable that is an
// addressable storage location, not its value at that
// particular moment.
func main() {
list := []int{2019, 4, 21}

for _, v := range list {
go func() {
fmt.Println(v)
}()
}

time.Sleep(100 * time.Millisecond)

for _, v := range list {
// pass `v` as an argument
go func(x int) {
fmt.Println(x)
}(v)
}

time.Sleep(100 * time.Millisecond)

for _, v := range list {
v := v // declares inner `v`, intialized to outer `v`
go func() {
fmt.Println(v)
}()
}

time.Sleep(100 * time.Millisecond)

// Output:
// 21
// 21
// 21
// 2019
// 21
// 4
// 2019
// 4
// 21
}
``````

### 柯里化

``````package main

import (
"fmt"
)

func main() {
add4 := func(x int) int {
}

// Output:
// 5
// 6
// 7
}

func add(x, y int) int {
return x + y
}
``````

### 高阶函数

``````package main

import (
"fmt"
"strings"
)

func main() {
s := strings.Map(
func(r rune) rune {
return r + 1
},
"HAL-9000")
fmt.Println(s)

// Output:
// IBM.:111
}
``````

``````#!/usr/bin/env python
# -*- coding: utf-8 -*-

def main():
s = [0, 1, 2, 3]
s1 = map(lambda x: x * x, s)
print (s)
print (s1)
# Output:
# [0, 1, 2, 3]
# [0, 1, 4, 9]

if __name__ == '__main__':
main()
``````

### 参考

• https://en.wikipedia.org/wiki/Anonymous_function