函數式編程基礎
by Preethi Kasireddy
通過Preethi Kasireddy
在收件箱中免費學習函數式編程的基礎 (Learn the fundamentals of functional programming — for free, in your inbox)
If you’re a software developer, you’ve probably noticed a growing trend: software applications keep getting more complicated.
如果您是軟件開發人員,則可能已經注意到了一種不斷增長的趨勢: 軟件應用程序越來越復雜。
It falls on our shoulders as developers to build, test, maintain, and scale these complex systems. To do so, we have to create well-structured code that is easy to understand, write, debug, reuse, and maintain.
當開發人員構建,測試,維護和擴展這些復雜的系統時,它就落在我們肩上。 為此,我們必須創建易于理解,編寫,調試,重用和維護的結構良好的代碼。
But actually writing programs like this requires much more than just practice and patience.
但是實際上編寫這樣的程序不僅僅需要練習和耐心。
In my upcoming course, Learning Functional JavaScript the Right Way, I’ll teach you how to use functional programming to create well-structured code.
在我即將開設的課程“正確學習功能JavaScript”中 , 我將教你如何使用函數式編程來創建結構良好的代碼。
But before jumping into that course (and I hope you will!), there’s an important prerequisite: building a strong foundation in the underlying principles of functional programming.
但是在進入那門課程之前(我希望您會!),有一個重要的先決條件:在函數式編程的基本原理中打下堅實的基礎 。
So I’ve created a new free email course that will take you on a fun and exploratory journey into understanding some of these core principles. Let’s take a look at what the email course will cover, so you can decide how it fits into your programming education.
因此,我創建了一個新的免費電子郵件課程 ,它將帶您進行有趣的探索性旅程,以了解其中的一些核心原則。 讓我們看一下電子郵件課程的內容,以便您決定如何將其納入您的編程教育中。
什么是函數式編程? (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 行為
- 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, for example, is handled purely using functions in functional programming. Functions are “self contained” pieces of code that accomplish a specific task. It defines 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 is, well, data. In functional programming, data 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? Come along for the ride!
聽起來令人興奮嗎? 快來兜風吧!
Sign up for the free email course now. Then you’ll receive the first lesson in your inbox within 1–3 days of signing up ?
立即注冊免費的電子郵件課程 。 注冊后的1-3天內,您會在收件箱中收到第一堂課嗎?
翻譯自: https://www.freecodecamp.org/news/learning-the-fundamentals-of-functional-programming-425c9fd901c6/
函數式編程基礎