<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Lambda on 老张开工了</title>
    <link>/lambda/</link>
    <description>Recent content in Lambda on 老张开工了</description>
    <generator>Hugo</generator>
    <language>zh-cn</language>
    <lastBuildDate>Mon, 11 May 2026 00:00:00 +0000</lastBuildDate>
    <atom:link href="/lambda/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>高阶函数与 Lambda 表达式</title>
      <link>/posts/2026/05/higher-order-functions/</link>
      <pubDate>Mon, 11 May 2026 00:00:00 +0000</pubDate>
      <guid>/posts/2026/05/higher-order-functions/</guid>
      <description>在上一章我们学到，Python 的函数是一等公民——可以作为参数传递、作为返回值返回。在这个基础上，**高阶函数（Higher-Order Function）**就是把函数当成普通值来操作的函数：要么接收函数作为参数，要么返回一个函数，或者两者兼备。&#xA;本章我们将学习 Python 中常见的高阶函数模式，包括 map、filter、reduce、sorted 的 key 函数、Lambda 表达式、偏函数，以及函数组合的技巧。&#xA;函数作为参数 这是最直观的高阶函数形式——一个函数接收另一个函数作为参数：&#xA;def apply_twice(func, value): &amp;#34;&amp;#34;&amp;#34;对 value 应用两次 func&amp;#34;&amp;#34;&amp;#34; return func(func(value)) def add_one(x): return x + 1 print(apply_twice(add_one, 5)) # 7（5→6→7） def square(x): return x ** 2 print(apply_twice(square, 2)) # 16（2→4→16） 这种模式在 Python 标准库中无处不在。比如 sorted() 的 key 参数：&#xA;words = [&amp;#34;python&amp;#34;, &amp;#34;Java&amp;#34;, &amp;#34;C&amp;#34;, &amp;#34;javascript&amp;#34;, &amp;#34;Go&amp;#34;] # 按照字符串长度排序 sorted_by_len = sorted(words, key=len) print(sorted_by_len) # [&amp;#39;C&amp;#39;, &amp;#39;Go&amp;#39;, &amp;#39;Java&amp;#39;, &amp;#39;python&amp;#39;, &amp;#39;javascript&amp;#39;] # 自定义排序逻辑 sorted_by_last_char = sorted(words, key=lambda s: s[-1]) print(sorted_by_last_char) # [&amp;#39;Java&amp;#39;, &amp;#39;C&amp;#39;, &amp;#39;python&amp;#39;, &amp;#39;Go&amp;#39;, &amp;#39;javascript&amp;#39;] Lambda 表达式 Lambda 表达式是创建匿名函数的快捷方式。当需要一个简单的、只使用一次的函数时，可以用 lambda 替代完整的 def 定义：</description>
    </item>
  </channel>
</rss>
