官术网_书友最值得收藏!

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!

主站蜘蛛池模板: 绥棱县| 越西县| 扶沟县| 万州区| 嫩江县| 平陆县| 蒙城县| 莱芜市| 红安县| 蓝山县| 邵阳县| 江门市| 德保县| 龙门县| 恩施市| 濮阳县| 汉中市| 定陶县| 永康市| 黄大仙区| 南阳市| 沙河市| 马龙县| 丰镇市| 黄冈市| 巴马| 阳春市| 卢龙县| 光山县| 金阳县| 翁牛特旗| 临湘市| 怀柔区| 常州市| 崇州市| 大英县| 蒙阴县| 上蔡县| 临桂县| 双桥区| 扶绥县|