Update
This commit is contained in:
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func exercise1() func(int) int {
|
||||
fmt.Println("=== 练习1:基础闭包 ===")
|
||||
|
||||
// 创建一个闭包函数,它捕获外部变量multiplier
|
||||
// 该闭包接收一个int参数并返回乘以multiplier的结果
|
||||
|
||||
multiplier := 3
|
||||
____ := func(num int) ____ {
|
||||
return ____
|
||||
}
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise2() func() int {
|
||||
fmt.Println("=== 练习2:闭包状态保持 ===")
|
||||
|
||||
// 创建一个递增计数器闭包
|
||||
// 每次调用返回的函数时,计数器加1
|
||||
// 提示:使用匿名函数捕获外部变量
|
||||
|
||||
count := ____
|
||||
|
||||
____ := func() ____ {
|
||||
____
|
||||
return ____
|
||||
}
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise3() func() int {
|
||||
fmt.Println("=== 练习3:斐波那契数列闭包 ===")
|
||||
|
||||
// 创建一个生成斐波那契数列的闭包
|
||||
// 每次调用返回下一个斐波那契数
|
||||
// 数列: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34...
|
||||
|
||||
a, b := ____, ____
|
||||
|
||||
____ := func() ____ {
|
||||
result := ____
|
||||
a, b = ____
|
||||
return ____
|
||||
}
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise4() func(int) bool {
|
||||
fmt.Println("=== 练习4:闭包配置 ===")
|
||||
|
||||
// 创建一个检查数字是否在范围内的闭包生成器
|
||||
// 该生成器接受min和max两个参数,返回一个检查函数
|
||||
// 检查函数判断输入数字是否在[min, max]范围内
|
||||
|
||||
min := 10
|
||||
max := ____
|
||||
|
||||
____ := func(num int) ____ {
|
||||
return num >= ____ && num <= ____
|
||||
}
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise5(start int) func(int) int {
|
||||
fmt.Println("=== 练习5:闭包累加器 ===")
|
||||
|
||||
// 创建一个累加器闭包
|
||||
// 从start开始,每次调用累加参数值
|
||||
// 提示:闭包应该能访问外部作用域的变量
|
||||
|
||||
total := ____
|
||||
|
||||
____ := func(n int) ____ {
|
||||
____
|
||||
return ____
|
||||
}
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("=== 闭包 练习 ===\n")
|
||||
|
||||
add3 := exercise1()
|
||||
fmt.Printf("练习1结果: add3(4) = %d\n", add3(4))
|
||||
fmt.Printf("练习1结果: add3(5) = %d\n", add3(5))
|
||||
|
||||
counter := exercise2()
|
||||
fmt.Printf("练习2结果: counter() = %d\n", counter())
|
||||
fmt.Printf("练习2结果: counter() = %d\n", counter())
|
||||
fmt.Printf("练习2结果: counter() = %d\n", counter())
|
||||
|
||||
fib := exercise3()
|
||||
fmt.Printf("练习3结果: fib() = %d\n", fib())
|
||||
fmt.Printf("练习3结果: fib() = %d\n", fib())
|
||||
fmt.Printf("练习3结果: fib() = %d\n", fib())
|
||||
fmt.Printf("练习3结果: fib() = %d\n", fib())
|
||||
fmt.Printf("练习3结果: fib() = %d\n", fib())
|
||||
|
||||
inRange := exercise4()
|
||||
fmt.Printf("练习4结果: inRange(15) = %t\n", inRange(15))
|
||||
fmt.Printf("练习4结果: inRange(5) = %t\n", inRange(5))
|
||||
fmt.Printf("练习4结果: inRange(20) = %t\n", inRange(20))
|
||||
|
||||
accumulator := exercise5(100)
|
||||
fmt.Printf("练习5结果: accumulator(10) = %d\n", accumulator(10))
|
||||
fmt.Printf("练习5结果: accumulator(20) = %d\n", accumulator(20))
|
||||
fmt.Printf("练习5结果: accumulator(30) = %d\n", accumulator(30))
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func captureOutput(f func()) string {
|
||||
old := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
f()
|
||||
|
||||
w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func TestExercise1(t *testing.T) {
|
||||
add3 := exercise1()
|
||||
result1 := add3(4)
|
||||
if result1 != 12 {
|
||||
t.Errorf("期望 12, 实际: %d", result1)
|
||||
}
|
||||
result2 := add3(5)
|
||||
if result2 != 15 {
|
||||
t.Errorf("期望 15, 实际: %d", result2)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise2(t *testing.T) {
|
||||
counter := exercise2()
|
||||
result1 := counter()
|
||||
if result1 != 1 {
|
||||
t.Errorf("期望 1, 实际: %d", result1)
|
||||
}
|
||||
result2 := counter()
|
||||
if result2 != 2 {
|
||||
t.Errorf("期望 2, 实际: %d", result2)
|
||||
}
|
||||
result3 := counter()
|
||||
if result3 != 3 {
|
||||
t.Errorf("期望 3, 实际: %d", result3)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise3(t *testing.T) {
|
||||
fib := exercise3()
|
||||
expected := []int{0, 1, 1, 2, 3}
|
||||
for i, exp := range expected {
|
||||
result := fib()
|
||||
if result != exp {
|
||||
t.Errorf("第%d个斐波那契数期望 %d, 实际: %d", i+1, exp, result)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise4(t *testing.T) {
|
||||
inRange := exercise4()
|
||||
if !inRange(15) {
|
||||
t.Errorf("15应该在范围内")
|
||||
}
|
||||
if inRange(5) {
|
||||
t.Errorf("5不应该在范围内")
|
||||
}
|
||||
if !inRange(20) {
|
||||
t.Errorf("20应该在范围内")
|
||||
}
|
||||
if inRange(21) {
|
||||
t.Errorf("21不应该在范围内")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise5(t *testing.T) {
|
||||
accumulator := exercise5(100)
|
||||
result1 := accumulator(10)
|
||||
if result1 != 110 {
|
||||
t.Errorf("期望 110, 实际: %d", result1)
|
||||
}
|
||||
result2 := accumulator(20)
|
||||
if result2 != 130 {
|
||||
t.Errorf("期望 130, 实际: %d", result2)
|
||||
}
|
||||
result3 := accumulator(30)
|
||||
if result3 != 160 {
|
||||
t.Errorf("期望 160, 实际: %d", result3)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainFunction(t *testing.T) {
|
||||
output := captureOutput(main)
|
||||
|
||||
requiredSections := []string{
|
||||
"=== 闭包 练习 ===",
|
||||
"练习1:基础闭包",
|
||||
"练习2:闭包状态保持",
|
||||
"练习3:斐波那契数列闭包",
|
||||
"练习4:闭包配置",
|
||||
"练习5:闭包累加器",
|
||||
}
|
||||
|
||||
for _, section := range requiredSections {
|
||||
if !strings.Contains(output, section) {
|
||||
t.Errorf("期望输出包含 '%s'", section)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func exercise1(apply func(int) int, value int) int {
|
||||
fmt.Println("=== 练习1:函数作为参数 ===")
|
||||
|
||||
// 接收一个函数apply和一个值value
|
||||
// 调用apply函数处理value并返回结果
|
||||
|
||||
result := ____
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise2() func(int) int {
|
||||
fmt.Println("=== 练习2:函数作为返回值 ===")
|
||||
|
||||
// 返回一个函数,该函数将输入值乘以2
|
||||
// 提示:返回一个匿名函数
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise3(vals []int, filter func(int) bool) []int {
|
||||
fmt.Println("=== 练习3:过滤器函数 ===")
|
||||
|
||||
// 接收一个int切片和一个过滤函数
|
||||
// 返回过滤后的切片(只保留满足条件的元素)
|
||||
|
||||
var result ____
|
||||
|
||||
for _, val := range ____ {
|
||||
if ____(val) {
|
||||
result = append(result, ____)
|
||||
}
|
||||
}
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise4(vals []int, mapper func(int) int) []int {
|
||||
fmt.Println("=== 练习4:映射函数 ===")
|
||||
|
||||
// 接收一个int切片和一个映射函数
|
||||
// 返回映射后的切片(每个元素都经过mapper函数处理)
|
||||
|
||||
result := make([]int, ____)
|
||||
|
||||
for i, val := range ____ {
|
||||
result[i] = ____(val)
|
||||
}
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise5(vals []int, reducer func(int, int) int, initial int) int {
|
||||
fmt.Println("=== 练习5:归约函数 ===")
|
||||
|
||||
// 接收一个int切片、一个归约函数和初始值
|
||||
// 返回归约结果
|
||||
// 例如:对切片求和、求积等
|
||||
|
||||
result := ____
|
||||
|
||||
for _, val := range ____ {
|
||||
result = ____(result, val)
|
||||
}
|
||||
|
||||
return ____
|
||||
}
|
||||
|
||||
func exercise6() int {
|
||||
fmt.Println("=== 练习6:函数组合 ===")
|
||||
|
||||
// 组合两个函数
|
||||
// square函数:计算平方
|
||||
// double函数:计算双倍
|
||||
// 返回 double(square(3)) 的结果
|
||||
// 即:先平方(9),再双倍(18)
|
||||
|
||||
square := func(n int) int { return ____ }
|
||||
double := func(n int) int { return ____ }
|
||||
|
||||
compose := func(f, g func(int) int, x int) int {
|
||||
return ____(____(x))
|
||||
}
|
||||
|
||||
return compose(____, ____, 3)
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("=== 高阶函数 练习 ===\n")
|
||||
|
||||
平方 := func(n int) int { return n * n }
|
||||
result1 := exercise1(平方, 4)
|
||||
fmt.Printf("练习1结果: %d\n", result1)
|
||||
|
||||
double := exercise2()
|
||||
result2 := double(5)
|
||||
fmt.Printf("练习2结果: %d\n", result2)
|
||||
|
||||
nums1 := []int{1, 2, 3, 4, 5, 6}
|
||||
isEven := func(n int) bool { return n%2 == 0 }
|
||||
result3 := exercise3(nums1, isEven)
|
||||
fmt.Printf("练习3结果: %v\n", result3)
|
||||
|
||||
nums2 := []int{1, 2, 3, 4, 5}
|
||||
multiplyBy3 := func(n int) int { return n * 3 }
|
||||
result4 := exercise4(nums2, multiplyBy3)
|
||||
fmt.Printf("练习4结果: %v\n", result4)
|
||||
|
||||
nums3 := []int{1, 2, 3, 4, 5}
|
||||
sum := func(acc, val int) int { return acc + val }
|
||||
result5 := exercise5(nums3, sum, 0)
|
||||
fmt.Printf("练习5结果: %d\n", result5)
|
||||
|
||||
result6 := exercise6()
|
||||
fmt.Printf("练习6结果: %d\n", result6)
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func captureOutput(f func()) string {
|
||||
old := os.Stdout
|
||||
r, w, _ := os.Pipe()
|
||||
os.Stdout = w
|
||||
|
||||
f()
|
||||
|
||||
w.Close()
|
||||
os.Stdout = old
|
||||
|
||||
var buf bytes.Buffer
|
||||
buf.ReadFrom(r)
|
||||
return buf.String()
|
||||
}
|
||||
|
||||
func TestExercise1(t *testing.T) {
|
||||
square := func(n int) int { return n * n }
|
||||
result := exercise1(square, 4)
|
||||
if result != 16 {
|
||||
t.Errorf("期望 16, 实际: %d", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise2(t *testing.T) {
|
||||
double := exercise2()
|
||||
result := double(5)
|
||||
if result != 10 {
|
||||
t.Errorf("期望 10, 实际: %d", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise3(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5, 6}
|
||||
isEven := func(n int) bool { return n%2 == 0 }
|
||||
result := exercise3(nums, isEven)
|
||||
|
||||
if len(result) != 3 {
|
||||
t.Errorf("期望长度 3, 实际: %d", len(result))
|
||||
}
|
||||
|
||||
expected := []int{2, 4, 6}
|
||||
for i, val := range expected {
|
||||
if result[i] != val {
|
||||
t.Errorf("索引 %d 期望 %d, 实际: %d", i, val, result[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise4(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
multiplyBy3 := func(n int) int { return n * 3 }
|
||||
result := exercise4(nums, multiplyBy3)
|
||||
|
||||
if len(result) != 5 {
|
||||
t.Errorf("期望长度 5, 实际: %d", len(result))
|
||||
}
|
||||
|
||||
expected := []int{3, 6, 9, 12, 15}
|
||||
for i, val := range expected {
|
||||
if result[i] != val {
|
||||
t.Errorf("索引 %d 期望 %d, 实际: %d", i, val, result[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise5(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5}
|
||||
sum := func(acc, val int) int { return acc + val }
|
||||
result := exercise5(nums, sum, 0)
|
||||
|
||||
if result != 15 {
|
||||
t.Errorf("期望 15, 实际: %d", result)
|
||||
}
|
||||
|
||||
product := func(acc, val int) int { return acc * val }
|
||||
result = exercise5(nums, product, 1)
|
||||
|
||||
if result != 120 {
|
||||
t.Errorf("期望 120, 实际: %d", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExercise6(t *testing.T) {
|
||||
result := exercise6()
|
||||
if result != 18 {
|
||||
t.Errorf("期望 18, 实际: %d", result)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMainFunction(t *testing.T) {
|
||||
output := captureOutput(main)
|
||||
|
||||
requiredSections := []string{
|
||||
"=== 高阶函数 练习 ===",
|
||||
"练习1:函数作为参数",
|
||||
"练习2:函数作为返回值",
|
||||
"练习3:过滤器函数",
|
||||
"练习4:映射函数",
|
||||
"练习5:归约函数",
|
||||
"练习6:函数组合",
|
||||
}
|
||||
|
||||
for _, section := range requiredSections {
|
||||
if !strings.Contains(output, section) {
|
||||
t.Errorf("期望输出包含 '%s'", section)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user