有必要学习函数式编程吗_学习函数式编程使我的开发人员素质提高了10倍
有必要学习函数式编程吗
Learning functional programming made me a 10x better developer. It helped me learn how write code that is clean, easy to maintain, and scalable.
学习函数式编程使我的开发人员提高了10倍。 它帮助我学习了如何编写干净,易于维护和可扩展的代码。
This is especially important in this day and age where software applications keep getting more complicated. The days of building and maintaining a simple web app are over.
在当今这个软件应用程序变得越来越复杂的时代,这一点尤其重要。 构建和维护简单的Web应用程序的时代已经结束。
As a developer, the expectations set upon you are higher than ever. It now falls on our shoulders to build, test, maintain, and scale complex applications that impact millions of people daily. This can be especially daunting as a beginner because we’re just getting the hang of writing code that actually works, let alone writing code that is easy to understand, write, debug, reuse, and maintain.
作为开发人员,对您的期望比以往更高。 现在,构建,测试,维护和扩展每天影响数百万人的复杂应用程序,就落在了我们的肩上。 这可能是特别艰巨作为一个初学者,因为我们刚开始写一些实际的工作 ,更别说写代码很容易理解,编写,调试,重复使用和维护代码的窍门。
This is where Functional programming made a difference for me—it helped me learn how to code that is easy to understand, write, debug, reuse, and maintain. As a result, I feel much more confident in my coding abilities.
在这里,函数式编程为我带来了不同-它帮助我学习了如何编写易于理解,编写,调试,重用和维护的代码。 结果,我对自己的编码能力更加自信。
Even if you are not using a functional programming language at work or on your side projects, knowing the basics of functional programming equips you with a powerful set of tools to write better code.
即使您不在工作中或在副项目中使用功能性编程语言,了解功能性编程的基础知识也会为您提供一套功能强大的工具,以编写更好的代码。
In my new e-book, I’ll teach you the basics of functional programming so that you have all the foundational knowledge you need to apply the principles at work, in your next job interview, or on your next side project.
在我的新电子书中 ,我将教您函数式编程的基础知识,以便您掌握在工作中,下一次工作面试中或在下一个副项目中应用这些原理所需的全部基础知识。
The rest of the post will give you a simple explanation of what functional programming is, which you’ll need to know before diving into the e-book. ????
文章的其余部分将为您简要介绍什么是函数式编程,在深入阅读电子书之前,您需要了解这些内容 。 ????
Let’s get right into it! ????
让我们开始吧! ????
什么是函数式编程? (What is functional programming?)
So. What is “functional programming,” exactly?
所以。 到底什么是“函数式编程”?
Functional programming isn’t a framework or a tool, but a way of writing code. In functional programming, we place a major emphasis on writing code using functions as “building blocks.”
函数式编程不是框架或工具,而是一种编写代码的方式 。 在函数式编程中,我们主要强调使用函数作为“构建块”来编写代码。
Your program is defined in terms of one main function. This main function is defined in terms of other functions, which are in turn defined in terms of still more functions — until at the bottom level the functions are just language primitives like “number” or “string.”
您的程序是根据一项主要功能定义的。 主要功能是根据其他功能定义的,其他功能又定义了其他功能-直到最底层的功能都是诸如“数字”或“字符串”之类的语言原语。
If you’re reading this thinking, “Hmm, but wait? Doesn’t every language use functions to write code?” then good ????. It means you’re paying attention.You’re right — every programming language has functions. But functional programming takes it to a whole ‘nother level ????
如果您正在阅读这种想法, “嗯,还等什么? 并非每种语言都使用函数编写代码吗?” 那就好????。 这意味着您正在关注。您是对的-每种编程语言都有功能。 但是函数式编程将其带到了一个全新的高度
To understand what I mean, let’s rewind and start with the basics.Every software program has two things:
为了理解我的意思,让我们回顾一下基础知识。每个软件程序都有两件事:
-
Behavior (what the program does)
行为 (程序做什么)
-
Data (data, is well, data)
数据 (数据,很好,数据)
When we’re learning about a programming paradigm — like functional programming — it’s often helpful to consider how the paradigm approaches behavior and data respectively.Behavior, in functional programming, is handled purely using functions in functional programming. Functions are “self contained” pieces of code that accomplish a specific task. They define a relationship between a set of possible inputs and a set of possible outputs — they usually take in data, process it, and return a result. Once a function is written, it can be used over and over and over again.Data, in functional programming, is immutable — meaning it can’t be changed. Rather than changing data they take in, functions in functional programming take in data as input and produce new values as output. Always.Functions and immutable data are the only two things you need to ever deal with in functional programming. To make it even simpler, functions are treated no differently than data.
当我们学习编程范例(如函数式编程)时,考虑范例如何分别处理行为和数据通常会很有帮助。 行为 ,在函数式编程,纯粹是利用函数式编程函数进行处理。 功能 是完成特定任务的“自包含”代码段。 它们定义了一组可能的输入和一组可能的输出之间的关系,它们通常是获取数据,处理数据并返回结果。 编写函数后,就可以一次又一次地使用它。 数据显示,在函数式编程,是不可改变的-这意味着它不能改变。 功能编程中的功能不是更改其接收的数据,而是将数据作为输入,并产生新的值作为输出。 总是。 函数和不可变 数据是函数编程中仅需处理的两件事。 为了使其更简单,对函数的对待与对数据的对待没有区别。
Put another way, functions in functional programming can be passed around as easily as data. You can refer to them from constants and variables, pass them as parameters to other functions, and return them as results from other functions.This is the most important thing to understand when approaching functional programming.
换句话说, 函数式编程中的函数可以像数据一样容易地传递。 您可以从常量和变量中引用它们,将它们作为参数传递给其他函数,并将它们作为其他函数的结果返回。这是进行函数式编程时了解的最重要的事情。
By treating functions as nothing more special than a piece of data and by only using data that is immutable, we are given a lot more freedom in terms of how we can use functions.
通过将函数视为仅比一条数据更特殊的事物,并且仅使用不可变的数据,就如何使用函数而言,我们被赋予了更多的自由。
Namely, it allows us to create small, independent functions that can be reused and combined together to build up increasingly complex logic. We can break any complex problem down into smaller sub-problems, solve them using functions, and finally combine them together to solve the bigger problem.Considering the ever-growing complexity of software applications, this kind of “building-block” approach makes a huge difference in keeping programs simple, modular, and understandable. This is also why developers strive to make their functions as general-purpose as possible, so that they can be combined to solve large, complex problems and reused to speed up development time for subsequent programs.
即,它使我们能够创建小的独立功能,这些功能可以重复使用并组合在一起以建立越来越复杂的逻辑。 我们可以将任何复杂的问题分解为较小的子问题,使用函数解决它们,最后将它们组合在一起以解决更大的问题。 考虑到软件应用程序的日益复杂性,这种“构建模块”方法在使程序保持简单,模块化和易于理解方面产生了巨大的差异。 这也是开发人员努力使它们的功能尽可能通用的原因,以便可以将它们组合起来解决大而复杂的问题,并可以重用它们以加快后续程序的开发时间。
Ultimately, the reason that functions are so powerful in functional programming is because the functions follow certain core tenets. Those tenets will be the subject of my email course:
最终,功能之所以在功能编程中如此强大是因为这些功能遵循某些核心原则。 这些原则将成为我的电子邮件课程的主题:
- Functions are pure 功能纯
- Functions use immutable data 函数使用不可变数据
- Functions guarantee referential transparency 功能保证参照透明
- Functions are first-class entities 功能是一流的实体
After that, I’ll briefly touch on how functional programming applies these tenets to encourage us to think carefully about our data and the functions that interact with it.
之后,我将简要介绍函数式编程如何应用这些原则,以鼓励我们仔细考虑数据以及与之交互的函数。
By the end, you’ll be able to understand how this approach leads to code that is:
到最后,您将能够了解这种方法如何导致代码如下:
- Easier to understand (that is, “expressive”) 易于理解(即“表达”)
- Easier to reuse 易于重用
- Easier to test 更容易测试
- Easier to maintain 易于维护
- Easier to refactor 易于重构
- Easier to optimize 易于优化
- Easier to reason about 容易推理
Sound exciting? If so, you'll love the new e-book. ????
听起来令人兴奋吗? 如果是这样,您会爱上这本新的电子书。 ????
The e-book will be released on December 13th. You can pre-order the e-book now for just $49! And as special offer to the free FreeCodeCamp community, I am offering $10 off with the discount code "freecodecamp".
该电子书将于12月13日发行。 您现在只需49美元即可 预订电子书 ! 作为对FreeFreeCamp社区的特别优惠,我提供10美元的折扣,折扣代码为“ freecodecamp ”。
See you in there! ????????✍️
在那里见! ????????✍️
翻译自: https://www.freecodecamp.org/news/learn-the-fundamentals-of-functional-programming/
有必要学习函数式编程吗