Currying and Partial application

深入理解代替单纯记忆

  • Currying和Partial application都是函数式编程范式中的概念
  • 尽管如此,在一些高级语言中(如Swift、Scala),在语法层面或者可以通过一些技术手段实现这些概念

本文将从概念和使用场景方面介绍这两个名词

概念

Currying则翻译为柯里化 ;Partial application可翻译为部分应用 看下各自的概念:

Partial application from Wiki: In computer science, partial application (or partial function application) refers to the process of fixing a number of arguments of a function, producing another function of smaller arity. Given a function f:(X×Y×Z)→N{}, we might fix (or 'bind') the first argument, producing a function of type partial(f):(Y×Z)→N{}. Evaluation of this function might be represented as fpartial(2,3){}.
Currying from Wiki: In mathematics and computer science, currying is the technique of translating a function that takes multiple arguments into a sequence of families of functions, each taking a single argument.

Partial application是指,先固定一个函数的部分参数,创建另一个函数,该函数只需接受剩余的参数

比如

swift 复制代码
// 原始函数
func multiply(_ x: Int, _ y: Int, _ z: Int) -> Int {
    return x * y * z
}

// 部分应用(固定 x)
func partialMultiply(by x: Int) -> (Int, Int) -> Int {
    return { y, z in
        return multiply(x, y, z)
    }
}

// 使用方式:
let multiplyBy2 = partialMultiply(by: 2)
let result = multiplyBy2(3, 4)  // 相当于 multiply(2, 3, 4)
print(result) // 24
  • 我们基于原函数multiply创建了partialMultiplypartialMultiply就是概念中所说的固定了x这个参数
  • 返回值仍是一个函数,类型是(Int, Int) -> Int,该函数可以接受yz参数

其实在上面Swift代码中,partialMultiply的返回值类型是Closure不是Function,但两者在这样的场景下使用是非常类似,也就是说虽然Swift中没有Partial application的语法,但可以通过Closure实现类似概念

在使用过程中能更清楚看到这一点:

  • 执行partialMultiply函数,并传入为2的x参数
  • 返回的新函数,命名为multiplyBy2
  • 后续通过执行multiplyBy2,并传入剩余的参数,就实现了让2与yz相乘

Currying指的是,将一个接受多个参数的函数,转换为一系列函数,每个函数仅接受一个参数

swift 复制代码
// 原始函数
func multiply(_ x: Int, _ y: Int, _ z: Int) -> Int {
    return x * y * z
}

func curryMultiply(_ x: Int) -> (Int) -> (Int) -> Int {
    return { y in
        return { z in
            return x * y * z
        }
    }
}

// 使用方式:
let step1 = curryMultiply(2)     // 返回 (Int) -> (Int) -> Int
let step2 = step1(3)             // 返回 (Int) -> Int
let result = step2(4)            // 返回 2 * 3 * 4 = 24

// 或者链式调用
let result2 = curryMultiply(2)(3)(4)
print(result2) // 24
  • 原函数是multiply
  • 通过curryMultiply,对原函数进行转换,返回值也是一个函数((Int) -> (Int) -> Int),其实是函数的嵌套(或者说函数的函数)
  • 在使用时能看出来,原multiply的执行,被拆分成了curryMultiplystep1step2三次执行,每次执行仅接受1个参数

能够看出来,Currying其实是Partial application的一种特殊情况

使用场景

比较明显的是,两个概念都可以用于代码的复用(或者说函数的复用),比如上面代码中的multiplyBy2函数就可以作为一个变量或者全局函数来复用

与高阶函数结合使用

swift 复制代码
func add(_ x: Int, _ y: Int) -> Int {
    return x + y
}

// Partial application:固定第一个参数
func add(_ x: Int) -> (Int) -> Int {
    return { y in x + y }
}

let add5 = add(5)
let result = [1, 2, 3].map(add5)  // [6, 7, 8]

预定义日志level级别

swift 复制代码
func log(level: String, message: String) {
    print("[\(level)] \(message)")
}

// 部分应用:绑定 log level
func logger(for level: String) -> (String) -> Void {
    return { message in log(level: level, message: message) }
}

let errorLog = logger(for: "ERROR")
let infoLog = logger(for: "INFO")

errorLog("Something went wrong")
infoLog("App started")
相关推荐
00后程序员张1 天前
iOS 开发全流程需要哪些工具?编码、调试、构建、发布阶段选择
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程
weixin_531670892 天前
Interlude起来:久坐提醒软件为什么需要登录?有没有完全本地运行的Mac休息提醒软件?
人工智能·macos·swift
2501_916008892 天前
Swift多环境配置:利用Scheme实现灵活的多环境管理
开发语言·ide·vscode·ios·个人开发·swift·敏捷流程
iFlyCai3 天前
iOS中的单例模式详解
ios·单例模式·objective-c·多线程·swift
weixin_531670893 天前
Interlude起来:Swift原生Mac软件有什么优势?原生Mac应用和Electron应用有什么区别?
人工智能·macos·mac·swift
k4m7v2pz3 天前
Swift Package Manager 在 macOS 26 上的三个编译错误排查指南
macos·spm·ai编程·xcode·swift·命令行工具
冯汉栩4 天前
Swift Control Calendar(日历)
开发语言·ios·swift
冯汉栩4 天前
Swift Control DateSelection(日期选择框)
ios·cocoa·swift
冯汉栩4 天前
Swift Control DashLineView(虚线)
开发语言·ios·swift
2501_915918414 天前
iOS 真机调试工具推荐,安装运行、性能分析有哪些工具
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程