- Mastering JavaScript Functional Programming
- Federico Kereki
- 172字
- 2021-07-02 22:41:11
A functional solution
Let's try to be more general: after all, requiring that some function or other be executed only once, isn't that outlandish, and may be required elsewhere! Let's lay down some principles:
- The original function (the one that may be called only once) should do that thing, and no other
- We don't want to modify the original function in any way
- We need to have a new function that will call the original one only once
- We want a general solution that we can apply to any number of original functions
The first principle listed previously is the single responsibility principle (the S in S.O.L.I.D.), which states that every function should be responsible over a single functionality. For more on S.O.L.I.D., check the article by Uncle Bob (Robert C. Martin, who wrote the five principles) at http://butunclebob.com/ArticleS.UncleBob.PrinciplesOfOod.
Can we do it? Yes; and we'll write a higher-order function, which we'll be able to apply to any function, to produce a new function that will work only once. Let's see how!
推薦閱讀
- MySQL 8從入門到精通(視頻教學版)
- Java虛擬機字節碼:從入門到實戰
- Learning Neo4j 3.x(Second Edition)
- 零基礎學MQL:基于EA的自動化交易編程
- 深度強化學習算法與實踐:基于PyTorch的實現
- Linux:Embedded Development
- Learning Concurrent Programming in Scala
- 運用后端技術處理業務邏輯(藍橋杯軟件大賽培訓教材-Java方向)
- 軟件測試實用教程
- Spring Boot+MVC實戰指南
- Buildbox 2.x Game Development
- Java圖像處理:基于OpenCV與JVM
- Python Interviews
- PHP與MySQL權威指南
- 從零開始學Android開發