匿名函数 #

一、基本语法 #

1.1 箭头语法 #

julia
x -> x^2
(x, y) -> x + y
() -> println("Hello")

1.2 多行匿名函数 #

julia
x -> begin
    y = x * 2
    y + 1
end

1.3 调用匿名函数 #

julia
f = x -> x^2
f(5)

((x, y) -> x + y)(1, 2)

二、使用场景 #

2.1 作为参数传递 #

julia
arr = [1, 2, 3, 4, 5]

map(x -> x^2, arr)
filter(x -> x > 2, arr)
reduce((a, b) -> a + b, arr)

2.2 排序 #

julia
arr = ["apple", "banana", "cherry"]
sort(arr, by = x -> length(x))
sort(arr, by = x -> x[1])

2.3 条件判断 #

julia
arr = [1, 2, 3, 4, 5]
any(x -> x > 3, arr)
all(x -> x > 0, arr)
count(x -> x % 2 == 0, arr)

2.4 字典操作 #

julia
d = Dict("a" => 1, "b" => 2, "c" => 3)
filter(p -> p.second > 1, d)
map(p -> p.first => p.second * 2, d)

三、do块语法 #

3.1 基本语法 #

julia
map(arr) do x
    x^2
end

等价于:

julia
map(x -> x^2, arr)

3.2 多行do块 #

julia
map(arr) do x
    if x > 0
        x * 2
    else
        0
    end
end

3.3 文件处理 #

julia
open("file.txt", "r") do f
    read(f, String)
end

3.4 自定义函数 #

julia
function with_resource(f)
    resource = acquire_resource()
    try
        return f(resource)
    finally
        release_resource(resource)
    end
end

with_resource() do r
    process(r)
end

四、闭包 #

4.1 捕获变量 #

julia
function make_multiplier(n)
    x -> x * n
end

times_3 = make_multiplier(3)
times_3(5)

times_10 = make_multiplier(10)
times_10(5)

4.2 计数器 #

julia
function make_counter()
    count = 0
    () -> (count += 1)
end

counter = make_counter()
counter()
counter()
counter()

4.3 累加器 #

julia
function make_accumulator()
    total = 0
    x -> (total += x)
end

acc = make_accumulator()
acc(10)
acc(5)
acc(3)

4.4 配置函数 #

julia
function make_greeter(greeting)
    name -> println("$greeting, $name!")
end

say_hello = make_greeter("Hello")
say_hi = make_greeter("Hi")

say_hello("Julia")
say_hi("Julia")

五、高阶函数 #

5.1 map #

julia
map(x -> x^2, [1, 2, 3, 4, 5])
map((x, y) -> x + y, [1, 2, 3], [10, 20, 30])

5.2 filter #

julia
filter(x -> x > 2, [1, 2, 3, 4, 5])
filter(x -> iseven(x), 1:20)

5.3 reduce #

julia
reduce((a, b) -> a + b, [1, 2, 3, 4, 5])
reduce((a, b) -> max(a, b), [3, 1, 4, 1, 5])

5.4 foldl 和 foldr #

julia
foldl((a, b) -> a - b, [1, 2, 3])
foldr((a, b) -> a - b, [1, 2, 3])

六、实践练习 #

6.1 练习1:自定义map #

julia
function my_map(f, arr)
    result = similar(arr, Any)
    for (i, x) in enumerate(arr)
        result[i] = f(x)
    end
    return result
end

my_map(x -> x^2, [1, 2, 3, 4, 5])

6.2 练习2:管道操作 #

julia
arr = [1, 2, 3, 4, 5]
arr |> (x -> filter(iseven, x)) |> (x -> map(y -> y^2, x))

6.3 练习3:函数组合 #

julia
compose(f, g) = x -> f(g(x))

double = x -> 2x
increment = x -> x + 1

double_then_increment = compose(increment, double)
increment_then_double = compose(double, increment)

double_then_increment(5)
increment_then_double(5)

6.4 练习4:回调函数 #

julia
function process_with_callback(data, callback)
    result = []
    for item in data
        push!(result, callback(item))
    end
    return result
end

process_with_callback([1, 2, 3, 4, 5], x -> x^2)
process_with_callback([1, 2, 3, 4, 5], x -> x > 2 ? x : nothing)

七、总结 #

本章我们学习了:

  1. 匿名函数语法:箭头语法和多行语法
  2. 使用场景:map、filter、sort等
  3. do块语法:更清晰的多行匿名函数
  4. 闭包:捕获外部变量
  5. 高阶函数:map、filter、reduce

接下来让我们学习Julia的高阶函数!

最后更新:2026-03-27