티스토리 뷰

  • Functional Programming TerminologyThe functions that take a function as an argument, or return a function as a return value are called higher orderfunctions.
  • When the functions are passed in to another function or returned from another function, then those functions which gets passed in or returned can be called a lambda.
  • Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value, are called first classfunctions. In JavaScript, all functions are first classfunctions.

 

  • The Hazards of Using Imperative Code잘못된 부분 -> tabClose에서 splice 쓰는 부분. 일단 앞 부분을 splice했을 때에 tab array가 mutable된다는 것에 주의. this.tabs.splice(1)로 바꿔야 한다.
  •  
  • // tabs is an array of titles of each site open within the window var Window = function(tabs) { this.tabs = tabs; // we keep a record of the array inside the object }; // When you join two windows into one window Window.prototype.join = function (otherWindow) { this.tabs = this.tabs.concat(otherWindow.tabs); return this; }; // When you open a new tab at the end Window.prototype.tabOpen = function (tab) { this.tabs.push('new tab'); // let's open a new tab for now return this; }; // When you close a tab Window.prototype.tabClose = function (index) { var tabsBeforeIndex = this.tabs.splice(0, index); // get the tabs before the tab var tabsAfterIndex = this.tabs.splice(index); // get the tabs after the tab this.tabs = tabsBeforeIndex.concat(tabsAfterIndex); // join them together return this; }; // Let's create three browser windows var workWindow = new Window(['GMail', 'Inbox', 'Work mail', 'Docs', 'freeCodeCamp']); var socialWindow = new Window(['FB', 'Gitter', 'Reddit', 'Twitter', 'Medium']); var videoWindow = new Window(['Netflix', 'YouTube', 'Vimeo', 'Vine']); // Now perform the tab opening, closing, and other operations var finalTabs = socialWindow .tabOpen().join(videoWindow.tabClose(2)).join(workWindow.tabClose(1).tabOpen());
  • Avoid Mutations and Side Effects Using Functional ProgrammingRecall that in functional programming, changing or altering things is called mutation, and the outcome is called a side effect. A function, ideally, should be a pure function, meaning that it does not cause any side effects.
  •  
  • One of the core principle of functional programming is to not change things. Changes lead to bugs. It's easier to prevent bugs knowing that your functions don't change anything, including the function arguments or any global variable.
  • Pass Args to Avoid External Dependence in a Function
    // the global variable var fixedValue = 4;  function incrementer (num) {   num += 1;   return num; }  var newValue = incrementer(fixedValue); // Should equal 5 console.log(fixedValue); // Should print 4 
  •  
  • Another principle of functional programming is to always declare your dependencies explicitly. This means if a function depends on a variable or object being present, then pass that variable or object directly into the function as an argument.

 

'공부일지(TIL) > Others' 카테고리의 다른 글

HTML5로 카드 만들기 (with Canvas API)  (0) 2019.06.11
Design Patterns Intro  (0) 2019.05.17
Node File System Module  (0) 2019.04.28
Functional Programming 3  (0) 2019.03.18
Functional Programming 2  (0) 2019.03.13
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함