|
Post by account_disabled on Jan 31, 2024 6:44:16 GMT
Punkie是一个具有 PHP 函数结构的库。在本教程中,Phunkie 创建者、Inviqa 培训主管 Marcello Duarte 解释了如何使用函数库创建解析器组合器。这篇文章首先出现在 Inviqa 博客上,并在他们的许可下在这里重新发布。 Play Next Unmute Current Time 0:05 / Duration 2:00 Fullscreen Backward Skip 10s Play Video Forward Skip 10s 芬奇标志 学习函数式编程 函数式编程确实很重要。纯函数(或没有副作用的函数)是完美的行为单位,因为我们可以依靠它们来构建更大、更复杂的系统。函数式编程的伟大之处依赖于这种可组合性的力量。 这是我第一次相信这一点是在我获得管理计算学士学位的时候,当时我正在用 Lisp 学习人工智能一个学期 的那本)。 我参加了 Martin Odersky 的所有 Coursera 课程,周末花了几个小时观看视频和阅读论文。 我给自己设定了一个挑战:用函数式编程来完成我在学习 TDD 和创建PHPSpec(BDD测试和设计工具)来帮助我学习;我决定用 PHP 编写自己的函数式编程方言。就这样,Punkie 诞生了! 现在交给你了。学习函 WhatsApp 号码数据 数式编程意味着让自己沉浸在新的范式中,这可能具有挑战性。它需要一种完全不同的思维方式来解决问题。 因此,当您可以使用函数式编程来解决现实世界的问题时,您将花费数小时来掌握新思维或掌握理论。 在本教程中,我的目标是通过回顾我对 Hutton 和 Meijer 的 Monadic 解析器组合器的实现来帮助您快速完成旅程。希望您能够看到函数式编程的思想和用处。 而且,通过使用 Phunkie,您无需学习 Haskell 即可理解该主题。您所需要的只是熟悉 PHP。 那么让我们开始吧! 解析器 根据特伦斯·帕尔 (Terence Parr) 的说法,解析是识别短语的计算机行为。解析策略有很多种。 我们将使用递归下降解析,这是最基本的解析之一,但它的功能足以实现一些有用的语法结构,例如排序、选择和重复。 组合器 如果问函数式编程是什么,我会回答:组合。函数是完美的行为单元,您可以组合它们来创建更大的系统。组合器是一种组合模式,早在编写任何类或对象之前就已经存在于函数式编程中。它们也非常优雅。 我们将解析器实现为函数。只需三个非常基本的解析器就足以帮助我们创建本文中的所有其他解析器。 使用类型来表示函数 让我们考虑一下解析器的作用。我们给它一个字符串。然后它尝试找出该字符串是否与任何语法定义匹配。如果我们有匹配项,我们将保留结果以及字符串的剩余片段。如果我们失败了,我们就会停下来。 那么这个函数声明会是什么样子呢? 但一个函数只能返回一个结果。那么我们如何返回匹配项和剩余的字符串呢?我们将使用 Punkie 类型:Pair。
|
|