高阶函数 #

一、map函数 #

1.1 基本用法 #

julia
map(x -> x^2, [1, 2, 3, 4, 5])
map(uppercase, ["hello", "world"])

1.2 多参数map #

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

1.3 广播 vs map #

julia
arr = [1, 2, 3, 4, 5]
map(x -> x^2, arr)
arr .^ 2

map(sqrt, arr)
sqrt.(arr)

1.4 map! 原地修改 #

julia
arr = [1, 2, 3, 4, 5]
map!(x -> x^2, arr, arr)
arr

二、filter函数 #

2.1 基本用法 #

julia
filter(x -> x > 2, [1, 2, 3, 4, 5])
filter(iseven, 1:20)
filter(x -> length(x) > 3, ["a", "ab", "abc", "abcd"])

2.2 字典过滤 #

julia
d = Dict("a" => 1, "b" => 2, "c" => 3, "d" => 4)
filter(p -> p.second > 2, d)
filter(p -> p.first in ["a", "b"], d)

2.3 filter! 原地修改 #

julia
arr = [1, 2, 3, 4, 5]
filter!(x -> x > 2, arr)
arr

三、reduce函数 #

3.1 基本用法 #

julia
reduce(+, [1, 2, 3, 4, 5])
reduce(*, [1, 2, 3, 4, 5])
reduce(max, [3, 1, 4, 1, 5, 9, 2, 6])

3.2 初始值 #

julia
reduce(+, [1, 2, 3], init=100)
reduce(*, [], init=1)

3.3 复杂归约 #

julia
reduce((acc, x) -> acc * x + 1, [1, 2, 3, 4])
reduce((acc, x) -> push!(acc, x^2), [1, 2, 3, 4], init=Int[])

四、foldl和foldr #

4.1 foldl(左折叠) #

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

4.2 foldr(右折叠) #

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

4.3 对比 #

julia
foldl(/, [1, 2, 3, 4])
foldr(/, [1, 2, 3, 4])

五、scan函数 #

5.1 cumsum #

julia
cumsum([1, 2, 3, 4, 5])

5.2 cumprod #

julia
cumprod([1, 2, 3, 4, 5])

5.3 accumulate #

julia
accumulate(+, [1, 2, 3, 4, 5])
accumulate(*, [1, 2, 3, 4, 5])
accumulate(max, [3, 1, 4, 1, 5, 9, 2, 6])

六、其他高阶函数 #

6.1 any和all #

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

6.2 findfirst和findall #

julia
findfirst(x -> x > 2, [1, 2, 3, 4, 5])
findall(x -> x % 2 == 0, [1, 2, 3, 4, 5, 6])

6.3 count #

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

6.4 foreach #

julia
foreach(println, [1, 2, 3, 4, 5])
foreach(x -> println(x^2), [1, 2, 3, 4, 5])

七、函数组合 #

7.1 compose #

julia
double(x) = 2x
increment(x) = x + 1

double_then_increment = increment ∘ double
double_then_increment(5)

7.2 管道操作 #

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

7.3 链式操作 #

julia
using Pipe

@pipe [1, 2, 3, 4, 5] |> filter(iseven, _) |> map(x -> x^2, _)

八、实践练习 #

8.1 练习1:数据处理管道 #

julia
function process_data(data)
    data |>
    (x -> filter(y -> y > 0, x)) |>
    (x -> map(y -> y^2, x)) |>
    (x -> reduce(+, x))
end

process_data([1, -2, 3, -4, 5])

8.2 练习2:单词统计 #

julia
function word_stats(text)
    words = split(lowercase(text))
    (
        total = length(words),
        unique = length(unique(words)),
        avg_length = reduce(+, length.(words)) / length(words)
    )
end

word_stats("Hello world hello Julia")

8.3 练习3:矩阵操作 #

julia
function matrix_stats(mat)
    rows = mapslices(sum, mat, dims=2)
    cols = mapslices(sum, mat, dims=1)
    (row_sums=vec(rows), col_sums=vec(cols), total=sum(mat))
end

mat = [1 2 3; 4 5 6; 7 8 9]
matrix_stats(mat)

8.4 练习4:分组归约 #

julia
function group_reduce(arr, key_func, value_func, reduce_func)
    groups = Dict()
    for x in arr
        key = key_func(x)
        value = value_func(x)
        if !haskey(groups, key)
            groups[key] = []
        end
        push!(groups[key], value)
    end
    Dict(k => reduce_func(v) for (k, v) in groups)
end

data = [(type="A", value=1), (type="A", value=2), (type="B", value=3)]
group_reduce(data, x -> x.type, x -> x.value, sum)

九、总结 #

本章我们学习了:

  1. map:映射函数
  2. filter:过滤函数
  3. reduce:归约函数
  4. foldl/foldr:折叠函数
  5. accumulate:累积函数
  6. 函数组合:∘运算符和管道

接下来让我们学习Julia的闭包!

最后更新:2026-03-27