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

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!

主站蜘蛛池模板: 甘孜县| 阳西县| 怀集县| 射阳县| 通海县| 安西县| 翁牛特旗| 天峻县| 丹巴县| 依兰县| 开阳县| 繁峙县| 姚安县| 新昌县| 六枝特区| 乌拉特中旗| 平乡县| 九龙县| 米脂县| 基隆市| 安徽省| 钟祥市| 广元市| 澄城县| 太原市| 淄博市| 泾川县| 镇远县| 华蓥市| 晋宁县| 开平市| 治县。| 荆州市| 西峡县| 武城县| 海阳市| 梨树县| 天镇县| 从江县| 宜兴市| 龙胜|